Powershell Get-WmiObject Access is denied

I have a Powershell script containing the following line:

$package = Get-WmiObject -Class Win32_Product -ComputerName $TargetServer -Filter ("Name='{0}'" -f $ApplicationName) 

I followed the steps on this answer in order to enable Powershell Remoting between the servers: remoting security steps

When I run the script from the Powershell ISE (in an elevated admin window) then I get the following error:

Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) At line:1 char:14 + Get-WmiObject <<<< win32_bios -computername d-vasbiz01 + CategoryInfo : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand 

I need to be able to run the script in the ISE so that I can troubleshoot other problems.

Can anyone please suggest what I need to do to fix this security error?

4

5 Answers

I needed to pass credentials to the Get-WmiObject cmdlet.

I found the answer here:Powershell Masters

1

After guessing for a WHILE, cached credentials were my problem.

Open windows credentials manager:

rundll32.exe keymgr.dll,KRShowKeyMgr

Delete all cached entries.

To expand on Rob Bowman's answer, you can either gather the credentials up front, or when running the command.

  • Up front (preferable, as you can re-use it for future Get-WmiObject commands:

    $creds = get-credential $package = Get-WmiObject -Class Win32_Product -ComputerName $TargetServer -Filter ("Name='{0}'" -f $ApplicationName) -credential $creds 
  • Inline:

    $package = Get-WmiObject -Class Win32_Product -ComputerName $TargetServer -Filter ("Name='{0}'" -f $ApplicationName) -credential (get-credential) 

You have to grant that user WMI permissions.

https://technet.microsoft.com/en-us/library/cc771551(v=ws.11).aspx

Local / domain admin should have that by default.

1

Additional Consideration: MS Patching

Our new system builds were failing to isntall an application which maks a WMI call when it starts to check something with the server application. I created a test WMI script and found that WMI calls worked everywhere in the environment but the new builds. Hence the fix was to install the MS patches to match everything else and the issue resolved itself (it may be a patch that came out July 2021).

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoaGpxZm1%2FcnyOqaawnaKotaa4y2aenqxdrLqqu8GjnJysXZawpLHSrGSiq12Zsq%2B1xJ0%3D