如果PowerShell脚本由Powersehll ISE运行,我想跳过Start-Transcript,Stop-Transcript行。有没有办法检查脚本是否由PowerShell ISE运行?
这可能吗?我怎样才能做到这一点?
如果PowerShell脚本由Powersehll ISE运行,我想跳过Start-Transcript,Stop-Transcript行。有没有办法检查脚本是否由PowerShell ISE运行?
这可能吗?我怎样才能做到这一点?
你可以这样做:
if ($host.name -eq 'ConsoleHost') # or -notmatch 'ISE'
{
.. do something ..
}
else
{
.. do something else..
}
另一种选择......
Try {
Start-Transcript -Path <somepath> | Out-Null
}
Catch [System.Management.Automation.PSNotSupportedException] {
# The current PowerShell Host doesn't support transcribing
}
知道这是问前一段,并标记为已回答,但多了一个办法:
function Test-IsISE {
# try...catch accounts for:
# Set-StrictMode -Version latest
try {
return $psISE -ne $null;
}
catch {
return $false;
}
}
$ psISE is available in ISE:
下面是查找的$psISE
的所有脑干,而不会产生异常的方法:
if (Test-Path variable:global:psISE)
{
...
}