【发布时间】:2016-01-15 01:18:28
【问题描述】:
我有一个脚本,它列出了远程服务器列表中位于 C:\ 下的文件:
$Servers=Get-ADComputer -SearchBase $TerminalServersOU -Filter '*' | Sort-Object Name | Select -Exp Name
foreach ($Server in $Servers) {
$Server
#Check first if the terminal server is on
if (Test-Connection $Server -quiet) {
Invoke-Command -ComputerName $Server {
$output=dir C:\
}
}
else {
"`n$Server is disconnected"
}
该脚本在所有服务器上都运行良好,除了一个名为“UKBTH05CTX03”的服务器。当脚本在此服务器上运行时,它会崩溃并且永远不会完成:
UKBTH05CTX01
UKBTH05CTX01 is disconnected
UKBTH05CTX02
Directory: C:\
Mode LastWriteTime Length Name PSComputerName
---- ------------- ------ ---- --------------
d---- 11/09/2014 23:00 inetpub ukbth05ctx02
d---- 11/09/2014 23:00 log ukbth05ctx02
d---- 14/07/2009 04:20 PerfLogs ukbth05ctx02
d-r-- 16/07/2015 15:15 Program Files ukbth05ctx02
d-r-- 15/09/2015 13:01 Program Files (x86) ukbth05ctx02
UKBTH05CTX03
Provider load failure
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Provider load failure
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
d---- 05/10/2014 15:18 inetpub ukbth05ctx03
Provider load failure
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : Get WMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
我需要能够显示“脚本在此服务器上崩溃”之类的消息并完成其执行。完成它的执行并知道它为什么崩溃对我来说非常重要。我不知道如何在 PowerShell 中执行此操作,所以我正在寻找建议。
谢谢大家。
【问题讨论】:
-
您是要解决此错误,还是只是确保循环遍历整个
$Servers列表(一路输出错误)? -
我只是想确保循环遍历整个 $Servers 列表(一路输出错误):)
标签: powershell error-handling command remote-server