title: How to troubleshoot "Get-WmiObject : Access is denied."?
body: <p>I have two working powershell scripts. You would invoke script1.ps1 with:</p>
  
  <pre><code>.\script1.ps1 "sender-ip=10.10.10.10"
  </code></pre>
  
  <p>And the script is supposed to return <code>userId=DOMAIN/UserId</code>. The first script:</p>
  
  <pre><code>#script1.ps1
  
  $abc = $args
  $startInfo = $NULL
  $process = $NULL
  $standardOut = $NULL
  
  &lt;#Previously created password file in C:\Script\cred.txt, read-host -assecurestring | convertfrom-securestring | out-file C:\Script\cred.txt#&gt;
  $password = get-content C:\Script\cred.txt | convertto-securestring
  
  $startInfo = New-Object System.Diagnostics.ProcessStartInfo
  $startInfo.FileName = "powershell.exe"
  $startInfo.Arguments = "C:\script\script2.ps1 " + $abc
  
  $startInfo.RedirectStandardOutput = $true
  $startInfo.UseShellExecute = $false
  $startInfo.CreateNoWindow = $false
  $startInfo.Username = "Username"
  $startInfo.Domain = "DOMAIN"
  $startInfo.Password = $password 
  
  $process = New-Object System.Diagnostics.Process
  $process.StartInfo = $startInfo
  $process.Start() | Out-Null
  $standardOut = $process.StandardOutput.ReadToEnd()
  $process.WaitForExit()
  
  # $standardOut should contain the results of "C:\script\script1.ps1"
  $standardOut 
  </code></pre>
  
  <p>And this is the entire working script2.ps1</p>
  
  <pre><code>#script2.ps1
  
  $line_array = @()
  $multi_array = @()
  [hashtable]$my_hash = @{}
  $Sender_IP = $NULL
  $Win32OS = $NULL
  $Build = $NULL
  $LastUser = $NULL
  $UserSID = $NULL
  $userID=$NULL
  $output = $NULL
  
  
  foreach ($i in $args){
     $line_array+= $i.split(" ")
  }
  
  foreach ($j in $line_array){
      $multi_array += ,@($j.split("="))
  }
  
  foreach ($k in $multi_array){
      $my_hash.add($k[0],$k[1])
  }
  
  $Sender_IP = $my_hash.Get_Item("sender-ip")
  
  
  &lt;#Gather information on the computer corresponding to $Sender_IP#&gt;
  
  $Win32OS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Sender_IP 
  
  
  &lt;#Determine the build number#&gt;
  $Build = $Win32OS.BuildNumber
  
  
  &lt;#Running Windows Vista with SP1 and later, i.e. $Build is greater than or equal to 6001#&gt;
  if($Build -ge 6001){
      $Win32User = Get-WmiObject -Class Win32_UserProfile -ComputerName $Sender_IP
      $Win32User = $Win32User | Sort-Object -Property LastUseTime -Descending
      $LastUser = $Win32User | Select-Object -First 1
      $UserSID = New-Object System.Security.Principal.SecurityIdentifier($LastUser.SID)
      $userId = $UserSID.Translate([System.Security.Principal.NTAccount])
      $userId = $userId.Value
  }
  
  
  &lt;#Running Windows Vista without SP1 and earlier, i.e $Build is less than or equal to 6000#&gt;
  elseif ($Build -le 6000){
      $SysDrv = $Win32OS.SystemDrive
      $SysDrv = $SysDrv.Replace(":","$")
      $ProfDrv = "\\" + $Sender_IP + "\" + $SysDrv
      $ProfLoc = Join-Path -Path $ProfDrv -ChildPath "Documents and Settings"
      $Profiles = Get-ChildItem -Path $ProfLoc
      $LastProf = $Profiles | ForEach-Object -Process {$_.GetFiles("ntuser.dat.LOG")}
      $LastProf = $LastProf | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1
      $userId = $LastProf.DirectoryName.Replace("$ProfLoc","").Trim("\").ToUpper()
  }
  
  else{
      $userId = "Unknown/UserID"
  }
  
  if ($userId -ne $NULL){
      $output = "userId=" + $userId
  }
  elseif ($userID -eq $NULL)
  {
      $userId = "Unknown/UserID"
      $output = "userId=" + $userId
  }
  
  
  $output.replace("\","/") 
  </code></pre>
  
  <p>Here is my problem. On most IP addresses in our domain, it returns:</p>
  
  <blockquote>
    <p>Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005
    (E_ACCESS DENIED))</p>
  </blockquote>
  
  <p>I researched this, and 
  <a href="https://stackoverflow.com/questions/14952833/get-wmiobject-win32-process-computername-gets-error-access-denied-code-0x8">&quot;get-wmiobject win32_process -computername&quot; gets error &quot;Access denied , code 0x80070005&quot;</a> recommends giving the elevated account permission to run WMI throughout the Domain. </p>
  
  <p>And this article, <a href="http://technet.microsoft.com/en-us/library/cc787533(v=ws.10).aspx" rel="nofollow noreferrer">http://technet.microsoft.com/en-us/library/cc787533(v=ws.10).aspx</a> explains how to do it.</p>
  
  <p>But convincing the team that is in charge of the domain to grant WMI permissions to this elevated account will be a stretch. And, the script is able to return the UserId for some of the IP addresses in the domain.</p>
  
  <p>Is there another way to resolve this? What should I research on google?</p>