【问题标题】:force powershell script to stop and retrieve info强制 powershell 脚本停止并检索信息
【发布时间】: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 中执行此操作,所以我正在寻找建议。

谢谢大家。

【问题讨论】:

标签: powershell error-handling command remote-server


【解决方案1】:

很确定Test-Connection 使用 wmi 来获取 Win32_PingStatus。由于您看到很多 wmi 错误,请扩展 Try...Catch 以将其也包含在内。

如果所有其他方法都失败,请在开始时设置$ErrorActionPreference = 'SilentlyContinue',您至少可以了解正在发生的事情以帮助隔离问题。

【讨论】:

  • 我重新启动了服务器,但我无法再次复制错误,因为现在它工作正常。我很想测试你的建议。感谢您的回答。
【解决方案2】:

您应该使用 try/catch 块来捕获错误,如下所示:

$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) { 

    try 
    {
        Invoke-Command -ComputerName $Server { 
           $output=dir C:\
        }
    }
    catch
    {
        Write-Host "the script crashed on server: $server"
    }
}

else {
        "`n$Server is disconnected"
    }

如果需要,您可以获得更高级的错误处理并实际返回收到的错误,请查看此处了解更多信息:

Try Catch Finally and error handling in PowerShell

【讨论】:

  • 我在 Invoke-Command 中执行的代码块只是“dir C:\”而不是“$output=dir C:\”。我为这个错误道歉。 @steoleary 我试过了,脚本一直在崩溃。我把try and catch放在了正确的地方,什么也没有。它总是在同一个地方崩溃并显示相同的错误消息
  • 还有什么建议吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多