【发布时间】:2023-03-13 13:59:01
【问题描述】:
我正在运行以下脚本来确定服务器上是否有驱动器 Z:。它包含try/catch,但我仍然收到“RPC-server is not available”这样的错误:
对于在 AD 中列出但不存在的主机。为什么?最重要的是,对于这些主机,我还有“无法获取磁盘”错误消息,因此它们不会落入 catch 块中。
function Get-Moment {
Get-Date -Format 'MM/dd/yyyy HH:mm:ss'
}
#if (Test-Path -Path $logFile1) {Remove-Item -Path $logFile1 }
ipmo ActiveDirectory
$Servers = Get-ADComputer -Filter 'Name -like "*"' -SearchBase 'OU=Production,OU=Windows,OU=Servers,DC=contoso,DC=com'
foreach ($comp in $Servers) {
"INFO $(Get-Moment) Host:$($comp.DNSHostName)" | Write-Output
try {
$a = Get-WmiObject Win32_LogicalDisk -ComputerName $($comp.DNSHostName)
} catch {
"EROR $(Get-Moment) Host:$($comp.DNSHostName) Couldn't reach the host!" | Write-Output
continue
}
if ($a) {
$diskz = $false
foreach ($disk in $a) {
if ($disk.DeviceID -eq 'Z:') {$diskz = $true}
}
if (!$diskz) {
"EROR $(Get-Moment) Host:$($comp.DNSHostName) Disk Z: is absent." | Write-Output
}
} else {
"EROR $(Get-Moment) Host:$($comp.DNSHostName) Cannot get disks" | Write-Output
}
}
【问题讨论】:
标签: powershell error-handling try-catch