错误处理 - PowerShell 脚本
我有以下 powershell 脚本,它循环浏览主机名列表并更改活动接口的 DNS 设置:
$servers = Get-Content C:\users\kevin.todd\desktop\serverlist.txt
foreach($server in $servers)
{
Write-Host "Connect to $server..."
$nics = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server -ErrorAction Inquire | Where{$_.IPEnabled -eq "TRUE"}
$newDNS = "10.100.10.81","10.100.10.82"
foreach($nic in $nics)
{
Write-Host "`tExisting DNS Servers " $nic.DNSServerSearchOrder
$x = $nic.SetDNSServerSearchOrder($newDNS)
if($x.ReturnValue -eq 0)
{
Write-Host "`tSuccessfully Changed DNS Servers on " $server
}
else
{
Write-Host "`tFailed to Change DNS Servers on " $server
}
}
}
问题出在某些主机上,我收到以下错误:
Get-WmiObject:RPC 服务器不可用。 (例外情况来自 HRESULT: 0x800706BA) 在 C:\Documents 和 设置\user1\桌面\changednsserver.ps1:20 字符:26 + $nics = Get-WmiObject <<<<< Win32_网络适配器配置 -ComputerName $server -ErrorAction 查询|其中{ $_.IPEnabled -eq “真的”} + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COM异常 + FullQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
现有的 DNS 服务器 您不能在空值服务器上调用方法 表达。在 C:\Documents 和 设置\user1\桌面\changednsserver.ps1:30 字符:42 + $x = $nic.SetDNSServerSearchOrder <<<< ($newDNS) + 类别信息:无效操作: (SetDNSServerSearchOrder:字符串)[],运行时异常 + FullQualifiedErrorId:InvokeMethodOnNull
Powershell 问我以下问题:
RPC 服务器不可用。 (HRESULT 异常:0x800706BA) [Y] 是 [A] 全部是 [H] 停止命令 [S] 暂停 [?] 帮助 (默认为“Y”):
我希望脚本只回答 A - 是,然后继续运行脚本。问题是它只是暂停脚本,直到我手动输入“A”。我怎样才能让它自动应答并继续?
I have the following powershell script that cycles through a list of hostnames and changes the DNS settings for the active interfaces:
$servers = Get-Content C:\users\kevin.todd\desktop\serverlist.txt
foreach($server in $servers)
{
Write-Host "Connect to $server..."
$nics = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server -ErrorAction Inquire | Where{$_.IPEnabled -eq "TRUE"}
$newDNS = "10.100.10.81","10.100.10.82"
foreach($nic in $nics)
{
Write-Host "`tExisting DNS Servers " $nic.DNSServerSearchOrder
$x = $nic.SetDNSServerSearchOrder($newDNS)
if($x.ReturnValue -eq 0)
{
Write-Host "`tSuccessfully Changed DNS Servers on " $server
}
else
{
Write-Host "`tFailed to Change DNS Servers on " $server
}
}
}
The problem is on some hosts I get the following error:
Get-WmiObject : The RPC server is unavailable. (Exception from
HRESULT: 0x800706BA) At C:\Documents and
Settings\user1\desktop\changednsserver.ps1:20 char:26
+ $nics = Get-WmiObject <<<< Win32_NetworkAdapterConfiguration
-ComputerName $server -ErrorAction Inquire | Where{ $_.IPEnabled -eq
"TRUE"}
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject],
COMException
+ FullyQualifiedErrorId :
GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Existing DNS Servers You cannot call a method on a null-valued
expression. At C:\Documents and
Settings\user1\desktop\changednsserver.ps1:30 char:42
+ $x = $nic.SetDNSServerSearchOrder <<<< ($newDNS)
+ CategoryInfo : InvalidOperation:
(SetDNSServerSearchOrder:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
And I'm asked the following question by Powershell:
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
[Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help
(default is "Y"):
I would like the script to just answer A - Yes to all and continue running the script. The problem is it just halts the script until I manually enter "A". How can I have it automatically answer and continue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么简短的答案是首先不要告诉它停止:
$nics = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server -ErrorAction Inquire |其中{$_.IPEnabled -eq "TRUE"}
尝试:
$nics = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server -ErrorAction SilentlyContinue |其中{$_.IPEnabled -eq "TRUE"}
Well the short answer is to not tell it to stop in the first place:
$nics = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server -ErrorAction Inquire | Where{$_.IPEnabled -eq "TRUE"}
Try:
$nics = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server -ErrorAction SilentlyContinue | Where{$_.IPEnabled -eq "TRUE"}
如果您需要查看错误发生的位置,您还可以使用开关定义错误变量
-ErrorVariable $
如果在 foreach 循环中发生错误,Error 变量将是一个错误消息数组,您稍后可以通过使用数组索引查询定义的错误变量来分析
echo $[]
类似于
echo $MyErrorArray[0]
来获取该数组中第一个发生的错误。一种优雅的方法是将所有内容添加到文本框/组合框以用于日志目的,但错误处理分析的权利是您的。
If you need to see where Errors occur, you have also a possibility to define an error variable using the switch
-ErrorVariable $<variable>
If errors occur in a foreach loop, the Error variable will be an array of error messages you can later analyze by querying the defined error variable with an array index
echo $<EVariable>[<index>]
it would be something like
echo $MyErrorArray[0]
to get the first occurred error in that array.An elegant way would be to have all that added to a textbox/combobox for log purposes but the coice of error handling ananalyzing is yours.