Я получаю эту ошибку.
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At F:\Code\powershell\network_shutdown\TurnNetworkOff.ps1:19 char:26
+ Get-WmiObject <<<< -computername $computer Win32_NetworkAdapter -filter "AdapterTypeId
=0" | % {
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Вот мой код
#Define variables used in this script
$namespace = "root\WMI"
$array = @()
$exceptionarray = @()
$computerlist = Get-Content F:\Code\powershell\network_shutdown\computer-list.csv
foreach ($computer in $computerlist)
{
# Main Processing Section
# Write-Host $computer
if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
{
Try
{
#Write-Host $computer
#Write-Host "Disable `"Allow the computer to turn off this device to save power`""
Get-WmiObject -computername $computer Win32_NetworkAdapter -filter "AdapterTypeId=0" | % {
$strNetworkAdapterID=$_.PNPDeviceID.ToUpper()
Get-WmiObject -class MSPower_DeviceEnable -computername $computer -Namespace $namespace | % {
if($_.InstanceName.ToUpper().startsWith($strNetworkAdapterID))
{
$_.Enable = $false
$_.Put() | Out-Null
}
}
}
#Write-Host "Disable `"Allow this device to wake the computer`""
Get-WmiObject -computername $computer Win32_NetworkAdapter -filter "AdapterTypeId=0" | % {
$strNetworkAdapterID=$_.PNPDeviceID.ToUpper()
Get-WmiObject -class MSPower_DeviceWakeEnable -computername $computer -Namespace $namespace | % {
if($_.InstanceName.ToUpper().startsWith($strNetworkAdapterID)){
$_.Enable = $false
$_.Put() | Out-Null
}
}
}
#Write-Host "Disable `"Only allow a magic packet to wake the computer`""
Get-WmiObject -computername $computer Win32_NetworkAdapter -filter "AdapterTypeId=0" | % {
$strNetworkAdapterID=$_.PNPDeviceID.ToUpper()
Get-WmiObject -class MSNdis_DeviceWakeOnMagicPacketOnly -computername $computer -Namespace $namespace | % {
if($_.InstanceName.ToUpper().startsWith($strNetworkAdapterID)){
$_.EnableWakeOnMagicPacketOnly = $false
$_.Put() | Out-Null
}
}
}
} Catch [Exception]
{
Write-Host $computer + " WMIC ERROR"
if ($_.Exception.GetType().Name -eq "COMException") {
Write-Host $computer + " WMIC ERROR"
}
$output = new-object psobject
$output | Add-Member noteproperty PCTag $computer
[PSObject[]]$exceptionarray += $output
}
} else {
Write-Host $computer + " OFFLINE"
$output = new-object psobject
$output | Add-Member noteproperty PCTag $computer
[PSObject[]]$array += $output
}
$array | Export-Csv -Path F:\Code\powershell\network_shutdown\ResultsOffline.csv
$exceptionarray | Export-Csv -Path F:\Code\powershell\network_shutdown\ResultsException.csv
}
Ошибка GetWMICOMException
является непрекращающейся ошибкой, что означает, что по умолчанию $ErrorActionPreference
из Continue
, код в блоке try
продолжит свое выполнение после записи исключения как ошибки
Enclose the Get-WmiObject
call с блоком try-catch
, но убедитесь, что -ErrorAction
установлено значение Stop
: Кроме того, вы можете установить $ErrorActionPreference
на Stop
перед выполнением вашего сценария:
# Before the rest of the script
$ErrorActionPreference = Stop
Для получения дополнительной помощи о конструкции финишной попытки улавливания:
Get-Help about_Try_Catch_Finally -Full
Для получения дополнительной справки о $*Preference
vars:
Get-Help about_Preference_variables -Full