2

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?

Community
  • 1
  • 1
Rob Bowman
  • 6,364
  • 19
  • 76
  • 162
  • 1
    I just want to verify - you've tried it from a standard window as well as one with elevated privileges, right? – Chris N Aug 15 '12 at 11:51
  • 1
    I notice that you're talking about remoting. Are you trying to double-hop at any point? – Chris N Aug 15 '12 at 13:15
  • 1
    No, the VM hosting the client and server both in the same domain - actually on the same physical host. – Rob Bowman Aug 16 '12 at 13:28
  • 1
    OK, my mistake - this doesn't work when run from TFS either. Regardless of how the code is run I get the security exception. Do I need to enable a trust between machines or something like that? – Rob Bowman Aug 23 '12 at 13:27

4 Answers4

3

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

I found the answer here:Powershell Masters

Rob Bowman
  • 6,364
  • 19
  • 76
  • 162
0

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

Open windows credentials manager:

rundll32.exe keymgr.dll,KRShowKeyMgr

Delete all cached entries.

user584572
  • 449
  • 4
  • 9
0

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)
    
KERR
  • 1,071
  • 14
  • 11
0

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.

Arnab
  • 1
  • 1
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30159266) – Toni Oct 24 '21 at 11:37