
powershell이 대화형으로 호출되었는지 여부를 powershell 프로필에서 감지할 수 있습니까? 일부 사람들이 사용하는 것을 보았지만 $host.name
항상 반환되는 것 같습니다 ConsoleHost
.
답변1
호출 프로세스가 -NonInteractive 매개변수를 전달하는 한 작동하는 $PROFILE을 넣는 매우 간단한 방법은 다음과 같습니다. 모든 것에 적용할 수는 없지만 위에서 언급한 Dallas처럼 다른 더 복잡한 방법이 있습니다.이 Stackoverflow 질문.
if ([Environment]::GetCommandLineArgs().Contains('-NonInteractive')) {
$Global:InteractiveMode=$false
} else {
$Global:InteractiveMode=$true
}
그런 다음 스크립트에서 해당 변수를 사용하여 다음과 같은 작업을 수행할 수 있습니다.색상 코드 제거일괄 작업 출력에서.
if (-not $InteractiveMode) {
# For PWSH 7.2+
$PSStyle.OutputRendering = [System.Management.Automation.OutputRendering]::PlainText
}