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 最后,请尝试接球_Powershell_Try Catch - Fatal编程技术网

Powershell 最后,请尝试接球

Powershell 最后,请尝试接球,powershell,try-catch,Powershell,Try Catch,我现在正在编写powershell脚本,以帮助加快捕获管理windows update的应用程序中的问题 我正在从数据库中提取正在管理的机器,我正在处理一个块来处理不同的输出。但我遇到了一个问题,它为一台机器抛出两条消息,而我只希望它抛出一条消息,并且可以用一只手 我意识到它仍然很粗糙,需要一些清理,我可能只是咖啡因摄入不足,但这是我一直坚持的障碍。或者至少在寻找创意 #Make sure the system is actually on before we try to access the

我现在正在编写powershell脚本,以帮助加快捕获管理windows update的应用程序中的问题

我正在从数据库中提取正在管理的机器,我正在处理一个块来处理不同的输出。但我遇到了一个问题,它为一台机器抛出两条消息,而我只希望它抛出一条消息,并且可以用一只手

我意识到它仍然很粗糙,需要一些清理,我可能只是咖啡因摄入不足,但这是我一直坚持的障碍。或者至少在寻找创意

#Make sure the system is actually on before we try to access the registry and wait for timeout or error
if (Test-Connection $Device.IPAddresses -Count 1 -ErrorAction SilentlyContinue)
    {
    Try { # test the registry connection, this will likely fail on workgroup systems
        if (Get-RemoteRegistryKey -RegKeyPath $RegKeyPath -RegValueName $RegValueName -ComputerName $Device.DNSname)
            {
            $SusClientID = $null # blank out the susclientid var so we don't unintentionally recycle it and put out bad data, because purple
            # now query the registry and put it into a var so we can fookin do stuff
            $SusClientID = Get-RemoteRegistryKey -RegKeyPath $RegKeyPath -RegValueName $RegValueName -ComputerName $Device.DNSname
            }
        else {Write-Output $Device.DNSname " is offline"}
        } # now catch errors, likely caused by credential issues or bad dns resolution
    Catch [UnauthorizedAccessException] {Write-Output ($Device.DNSname + " cannot access remote registry")
        }
    Finally { # finally lets compare the sysclient id on the machine to that in the database
        if ($SusClientID) # -match "\A[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\z")
            {
            if ((Compare-Object $SusClientID $Device.SusClientId) -eq $null)
                {Write-Output ($Device.DNSname +" SusClientId matches database.")}
            elseif ($SusClientID -eq $null -or $SusClientID -eq "") {} # this didn't work as I'd hoped
            else {
                # notmatching a SusClientId GUID will not catch systems with malformed ID's, commenting this out until a better method is found
                #if ($SusClientID -notmatch "\A[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\z")
                Write-Output ($Device.DNSname +" SusClientId does not match.")}
            }

        }
    }
现在的问题是,在我下面的输出中,我捕获了pc-001的一个异常,并声明它无法访问,这正是我想要的,但是我还抛出了一条消息,即susclientid与数据库中的不匹配,因为没有收集到匹配的susclientid。“不匹配”消息按照pc-005的要求工作,因为我故意在注册表中替换了它的susclientid

pc-001.sergeinc.org cannot access remote registry
pc-001.sergeinc.org SusClientId does not match.
pc-002.sergeinc.org SusClientId matches database.
pc-003.sergeinc.org SusClientId matches database.
srv-ad.sergeinc.org SusClientId matches database.
pc-xptest-001.sergeinc.org SusClientId matches database.
pc-004.sergeinc.org is offline
pc-005.sergeinc.org SusClientId does not match.

这可能是我个人的偏好,但我会尝试在任何try{…}块中放入尽可能少的命令。我也不确定是否值得拥有一个最终{…}块。在这种情况下,一旦您知道设备与测试连接语句联机,我会这样做:

$SusClientID = $null
...

try {
  $SusClientID = Get-RemoteRegistryKey -RegKeyPath $RegKeyPath -RegValueName $RegValueName -ComputerName $Device.DNSname
}
catch {
  ...
}

if($SusClientID) {
  ...
} else {
  ...
}
这可以更清楚地显示哪个语句正在为catch{…}块生成错误


请注意,如果您在设备列表中循环,在每个循环的开始处将$SusClientID设置为$null肯定是可行的,或者如果您发现错误,可以将其显式设置为$null。

我认为$SusClientID=$null应该在获取RemoteRegistryKey之前执行……您是对的。亚历山大·奥伯斯特把你的评论变成OP可以接受的答案