programing

PowerShell ISE에서 스크립트가 실행되고 있는지 확인할 수 있는 방법이 있습니까?

lastcode 2023. 9. 24. 12:54
반응형

PowerShell ISE에서 스크립트가 실행되고 있는지 확인할 수 있는 방법이 있습니까?

PowerShell ISE에서 PowerShell 스크립트가 실행 중인 경우 시작-변환, 중지-변환 줄을 건너뛸 수 있습니다.

가능한가요?그리고 어떻게 하면 이것을 이룰 수 있을까요?

다음을 수행할 수 있습니다.

if ($host.name -eq 'ConsoleHost') # or -notmatch 'ISE'
{
  .. do something .. 
}
else
{
  .. do something else..
}

이것은 꽤 오래 전에 질문을 받았고 이미 답변된 것으로 표시되었지만 한 가지 방법이 더 있습니다.

function Test-IsISE {
# try...catch accounts for:
# Set-StrictMode -Version latest
    try {    
        return $psISE -ne $null;
    }
    catch {
        return $false;
    }
}

$psISEISE: ISE 개체 모델 계층 구조에서 사용할 수 있습니다.

여기에 그 존재를 찾는 방법이 있습니다.$psISE예외를 생성하지 않고:

if (Test-Path variable:global:psISE)
{
...
}

다른 대안은...

Try {
    Start-Transcript -Path <somepath> | Out-Null
}

Catch [System.Management.Automation.PSNotSupportedException] {
    # The current PowerShell Host doesn't support transcribing
}

이것들은 훌륭한 답입니다!여기 제가 $host.name 을 사용하는 방법이 있습니다.

다음과 같은 방법이 필요했습니다.

ReadKey를 내 스크립트에서 사용(ISE에서는 작동하지 않음)하지만 Powershell ISE와 함께 스크립트를 사용(실행)할 수 있습니다.스크립트가 콘솔인지 ISE인지 알 수 있는 방법을 가지고 ReadKey 또는 Read-Host를 각각 활성화합니다.

이에 대한 코드는 다음과 같습니다.

IF ($host.name -eq 'Windows Powershell ISE Host') {$K = Read-Host "Please make your choice"} #Used when you are in ISE.  Will necessitate an ENTER Key.
IF ($host.name -eq 'ConsoleHost') {$KeyPress = [System.Console]::ReadKey();$K = $KeyPress.Key} #Used when running in Console. This will NOT necessitate an ENTER Key. BUT, it ## will NOT work ## in ISE

언급URL : https://stackoverflow.com/questions/13195406/is-there-a-way-to-check-if-the-script-is-running-by-powershell-ise

반응형