Error calling get-wmiobject for Win32_powerplan - powershell

I'm trying to return the Win32_PowerPlan in PowerShell 7 on Windows 11 with the following code:
get-wmiobject -namespace "root\cimv2\power" -class Win32_powerplan
However, I get the output:
get-wmiobject :
At line:1 char:1
+ get-wmiobject -namespace "root\cimv2\power" -class Win32_powerplan
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Not sure what could be going on or how to get better error logs. The official docs use this exact code snippet, so not sure what it should change to if it's wrong.
Potentially related to PowerShell call to Win32_PowerPlan showing invalid class error

It would appear that, to retrieve the information you are seeking, PowerShell needs to be run in "elevated" mode (i.e., "Run as Administrator").
When doing so, I see the information as intended; however, when running 'normally' (even though I'm logged on with admin rights), I see the same error message that you have reported.

Related

Getting Strange errors from Server - PowerShell

When I try to run this simple line of code on one of our Print Servers it fails, but it works fine on the other Server.
get-wmiobject -Class Win32_Printer -ComputerName "PRINT-SERVER" | Select-Object Name, Direct >>C:\Temp\Printer-Direct.txt
I get the following:
At line:1 char:1
get-wmiobject -Class Win32_Printer -ComputerName "PRINT-SERVER" | S ...
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Any ideas?
BTW - the name is correct, the server responds to Test-NetConnection and my brain is in a fog today thanks to Daylight Saving.
The issue wasn't with PowerShell or anything to do with the code. The error I was getting was a direct issue with the Print Server.
I didn't do any diagnostics since rebooting the server solved the problem. If the issue appears again, I may have to run the WMI Diagnostic tools to find out what is the problem, but I suspect it might have been a pending update on the Server due to Security updates.
Thank you to AussieJoe for the tip.

MSBTS_HostSetting is Not Found

Upon building a script to automate the deletion of Host and Host Instances, I run the script below in PowerShell.
PS
C:\WINDOWS\system32>
[System.Management.ManagementObject]$objHostSetting =
([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_HostSetting").Delete()
However, after running the script, it seems that the MSBTS_HostSetting is gone because I was receiving the error below every time I am running it.
PS
C:\WINDOWS\system32>
[System.Management.ManagementObject]$objHostSetting
=[WmiClass]"root/MicrosoftBizTalkServer:MSBTS_HostSetting" Cannot convert value "root/MicrosoftBizTalkServer:MSBTS_HostSetting" to type
"System.Management.ManagementClass". Error: "Not found " At line:1
char:2
+ [System.Management.ManagementObject]$objHostSetting =[WmiClass]"root/MicrosoftB ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastToWMIClass
I am trying to load the BizTalkOMExplorer but the MSBTS_HostSetting has not returned. Any suggestions or knowledge how can I bring it back.
Just fixed my issue! Below are what I've done.
Open CMD and register the BTSWMIProvider.dll
Example:
Regsvr32.exe “C:\Program Files (x86)\Microsoft BizTalk Server 2013 R2\Bins32\BTSWMIProvider.dll“
Run the following in CMD as well.
mofcomp.exe BTSWMISchema.mof
mofcomp.exe BTSWMISchema.mfl
Restart WMI in services.
This would do the trick! :)
With PowerShell, you can get your HostInstances as:
$hostInstances = Get-WmiObject MSBTS_HostInstance -namespace root\MicrosoftBizTalkServer -ErrorAction Stop
And then you can find which one you want to delete and invoke Delete method:
$hostInstances[0].Delete()
The same with Hosts:
$hosts = Get-WmiObject MSBTS_Host -Namespace root\MicrosoftBizTalkServer -ErrorAction Stop
$hostToDelete = $hosts | where {$_.Name -eq 'HostNameToDelete'}
$hostToDelete.Delete()

Error 'Get-WmiObject : Invalid class "Msvm_ImageManagementService"'

Recently I've been getting this error, and I have no idea what triggered it. The documentation I found regarding this class is minimal and I'm not sure what I need to check or where to look in order to understand it and fix it.
System:
Windows Server 2012 R2 Datacenter Preview Build 9431
PowerShell Version 4.0 32bit and 64bit
Output:
PS > Get-WmiObject -Class "Msvm_ImageManagementService" -Namespace "root\virtualization"
Get-WmiObject : Invalid class "Msvm_ImageManagementService"
At line:1 char:1
+ Get-WmiObject -Class "Msvm_ImageManagementService" -Namespace "root\virtualizati ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidType: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
The class is still there, it now resides in a different namespace. It's in the namespace "root\virtualization\v2". It's also worth noting that the class has changed, previously used methods may not exist anymore.

Powershell stop-service error: cannot find any service with service name

I'm working on a script to deploy vendor software to a large environment. The first step is to stop the services in question. The script executes fine in our test environment, but I'm not an admin in the production environment so I'm convinced it's a permissions issue. I can't get admin rights to the prod environment so I need to try to find out anything that I may need to set to grant permissions to stop services remotely. I'm issuing the following command to stop services:
Stop-Service -InputObject $(Get-Service -Computer $destination.Server -Name ("moca."+$destEnv))
When I run the script I get:
Cannot find any service with service name 'moca.WMSPRD'.
+ CategoryInfo : ObjectNotFound: (moca.WMSPRD:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
Cannot validate argument on parameter 'InputObject'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
+ CategoryInfo : InvalidData: (:) [Stop-Service], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StopServiceCommand
The service definitely exists and if I rdp into the target box and issue the stop-service command locally it will execute. So there is something preventing me from stopping the service remotely. Any ideas?
Edit:
A coworker suggested using WMI so tried replacing the Stop-Service line with:
(Get-WmiObject -computer $destination.Server Win32_Service -Filter ("Name='moca."+$destEnv+"'")).InvokeMethod("StopService",$null)
and I get:
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
+ CategoryInfo : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
If you know the exact service name you can try this
(Get-WmiObject -computerName $_.name Win32_Service -Filter "Name='moca'").StopService()
Here im assuming that the service name is moca
Is DCOM working on the remote computer? I know how to do it with remote powershell, which uses wsman:
invoke-command comp001 { stop-service adobearmservice }

Why do Powershell queries on remote machine throwing access denied errors?

I'm getting such errors from the PowerShell ISE when I execute simple PowerShell queries on my machine connecting to a remote machine:
Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At line:8 char:43
+ $WmiOS = Get-WmiObject <<<< -Class Win32_OperatingSystem -ComputerName $targetComputer -credential $credential
+ CategoryInfo : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
You cannot call a method on a null-valued expression.
At line:9 char:45
+ $WmiOS.ConvertToDateTime <<<< ($WmiOS.LastBootUpTime)
+ CategoryInfo : InvalidOperation: (ConvertToDateTime:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
However, the same queries would work on my other team member's machine (or on any other machine) without any issues.
An example of the script that we're trying to execute:
$WmiOS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $targetComputer -credential $credential
$WmiOS.ConvertToDateTime($WmiOS.LastBootUpTime)
Any ideas?
Try adding the below flags to the Get-WmiObject command:
-Authentication PacketPrivacy -Impersonation Impersonate
Check firewall on remote machine and/or credentials to be in administrators local group of remote computer
Make sure the script you are running is not located on the server itself. It is common practice for people to host their powershell scripts on the server in order to have them in various locations. If your scripts are local, then you should be able to shut down the machine.