Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 是否有方法检查脚本是否由PowerShell ISE运行?_.net_Powershell_Powershell 3.0 - Fatal编程技术网

.net 是否有方法检查脚本是否由PowerShell ISE运行?

.net 是否有方法检查脚本是否由PowerShell ISE运行?,.net,powershell,powershell-3.0,.net,Powershell,Powershell 3.0,如果PowerShell脚本是由PowerShell ISE运行的,我想跳过开始转录、停止转录行 这可能吗?我怎样才能做到这一点呢?您可以做到: if ($host.name -eq 'ConsoleHost') # or -notmatch 'ISE' { .. do something .. } else { .. do something else.. } 另一种选择 Try { Start-Transcript -Path <somepath> | Out

如果PowerShell脚本是由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
}
试试看{
开始转录本-路径|输出为空
}
Catch[System.Management.Automation.PSNotSupportedException]{
#当前PowerShell主机不支持转录
}

知道这是很久以前提出的问题,并且已经标记为已回答,但还有一种方法:

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


这里有一个方法,可以在不生成异常的情况下查找
$psISE
的存在:

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

这些都是很好的答案!下面是我使用$host.name的一种方法

我需要一种方法:

在我的脚本中使用ReadKey(在ISE中不起作用),但仍然能够使用(并运行)Powershell ISE的脚本。 让脚本知道它是在Console还是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