当我运行这个脚本,我得到这个错误
Get-WmiObject:无效的类“Msvm_ImageManagementService”
和
Get-WmiObject:无效的类“MSPower_DeviceEnable”
和
Get-WmiObject:无效的类“MSPower_DeviceWakeEnable”
上述错误只出现在一些电脑上,而不是其他电脑上。
$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)) { 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 } } } } else { Write-Host $computer + " OFFLINE" $output = new-object psobject $output | Add-Member noteproperty ComputerName $computer $array += $output } $array | Export-Csv -Path F:\Code\powershell\network_shutdown\Results.csv }
如何修复名称空间以便我可以定位所有计算机?
我的错误日志logging也无法正常工作。 我能做些什么来将OFFLINE,FAILED和SUCCESS变成CSV文件? (我知道在我上面的代码中,我只捕获了离线的代码,但是这也不起作用)
我得到以下
方法调用失败,因为[System.Management.Automation.PSObject]不包含名为“op_Addition”的方法。
代码在Windows 7上运行
Major Minor Build Revision ----- ----- ----- -------- 2 0 -1 -1
Get-WmiObject:无效的类“Msvm_ImageManagementService”
该类由Hyper-V WMI提供程序公开。 如果目标机器上没有安装Hyper-V或Hyper-Vpipe理工具,则该课程将不在此处
Get-WmiObject:无效的类“MSPower_ *”
MSPower超类没有官方的支持,派生的_Device *类不公开,除非安装的networking适配器支持LAN唤醒
方法调用失败,因为[System.Management.Automation.PSObject]不包含名为“op_Addition”的方法。
可能是由以下行引起的:
$array += $output
如果$array尚不存在,PowerShell就不能自动推断出你希望它是一个PSObject数组 – 然后PowerShell尝试使用“Addition operator”( + )的右边的对象types,并且正确地失败,因为PSObject支持这样的操作。
在脚本开始处添加以下内容:
$array = @()
要初始化一个空数组,或者使用显式的强制types转换来指示所需的types:
[PSObject[]]$array += $output