When I run this script, I get this error
Get-WmiObject : Invalid class “Msvm_ImageManagementService”
and
Get-WmiObject : Invalid class "MSPower_DeviceEnable"
and
Get-WmiObject : Invalid class "MSPower_DeviceWakeEnable"
The above errors only occur on some computers and not on others.
$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
}
How can I fix the namespace so that I can target all computers?
My error logging isn't working either. What can I do to get the OFFLINE and FAILED and SUCCESS into a CSV file? (I am aware that in my code above, I am only capturing the offline ones but that doesn't work either)
I get the following
Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'.
The code is running on Windows 7
PowerShell Version
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
This class is exposed by the Hyper-V WMI provider. If you don't have Hyper-V or Hyper-V Management tools installed on the target machine, this class won't be there
The MSPower superclass is not officially supported, and the derivated _Device* classes is not exposed unless an installed network adapter supports Wake-on-LAN
is probably caused by the following line:
If
$array
does not already exist, PowerShell cannot automatically infer that you would like it to be an array ofPSObject
s - PowerShell then tries to use the type of the object on the right-hand side of the "Addition operator" (+
), and correctly fails becausePSObject
supports no such operation.Add the following at the start of your script:
To initialize an empty array, or indicate the desired type with an explicit cast: