Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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
powershell中的Try方法_Powershell_Wmi - Fatal编程技术网

powershell中的Try方法

powershell中的Try方法,powershell,wmi,Powershell,Wmi,因此,我想在下面的powershell脚本中构建一个try方法。如果拒绝访问服务器,我希望它跳过该服务器。请帮忙 [code]$Computers = "server1", "server2" Get-WmiObject Win32_LogicalMemoryConfiguration -Computer $Computers | Select-Object ` @{n='Server';e={ $_.__SERVER }}, ` @{n='Physical Memory';e={ "$('{

因此,我想在下面的powershell脚本中构建一个try方法。如果拒绝访问服务器,我希望它跳过该服务器。请帮忙

[code]$Computers = "server1", "server2"

Get-WmiObject Win32_LogicalMemoryConfiguration -Computer $Computers | Select-Object `
@{n='Server';e={ $_.__SERVER }}, `
@{n='Physical Memory';e={ "$('{0:N2}' -f ($_.TotalPhysicalMemory / 1024))mb" }}, `
@{n='Virtual Memory';e={ "$('{0:N2}' -f ($_.TotalPageFileSpace / 1024))mb" }} | `
Export-CSV "output.csv"[/code]

您可以使用陷阱来复制Try/Catch,请参阅或,例如。

您可以使用陷阱来复制Try/Catch,请参阅或,例如。

PowerShell 2.0内置了Try/Catch功能,例如:

PS> try {$i = 0; 1/$i } catch { Write-Debug $_.Exception.Message }; 'moving on'
Attempted to divide by zero.
moving on

只需将脚本包装成类似的try/catch。注意:如果将$DebugPreference设置为“Continue”,您可以通过将catch块留空来完全忽略错误,但我建议您至少将错误信息吐出来。

Try/catch功能内置于PowerShell 2.0中,例如:

PS> try {$i = 0; 1/$i } catch { Write-Debug $_.Exception.Message }; 'moving on'
Attempted to divide by zero.
moving on

只需将脚本包装成类似的try/catch。注意:您可以通过将catch块留空来完全忽略错误
catch{}
,但如果$DebugPreference设置为“Continue”,我建议您至少吐出错误信息。

您可以使用ErrorAction参数简单地抑制错误:


获取WmiObject Win32_LogicalMemoryConfiguration-Computer$Computers-ErrorAction SilentlyContinue |…

您可以使用ErrorAction参数简单地抑制错误:

获取WmiObject Win32_LogicalMemoryConfiguration-Computer$Computers-ErrorAction SilentlyContinue |…

是否使用筛选函数

他将一个计算机列表传递给他的管道-首先它尝试ping每个计算机,然后只传递响应下一个命令(重新启动)的计算机。您可以根据需要定制任何实际功能。

是否使用筛选功能


他将一个计算机列表传递给他的管道-首先它尝试ping每个计算机,然后只传递响应下一个命令(重新启动)的计算机。您可以根据需要定制任何实际功能。

在V2中,内置了对try/catch/finally的支持。这些帖子已经过时了。在V2中,内置了对try/catch/finally的支持。帖子已过期。只需稍微限定该语句-您可以使用ErrorAction参数抑制“非终止”错误。:-)实际上,感谢Keith:)(Wmi的“拒绝访问”是一个“非终止”错误)只是为了稍微限定该语句-您可以使用ErrorAction参数抑制“非终止”错误。:-)的确,谢谢Keith:)(Wmi的“拒绝访问”是一个“非终止”错误)