title: Passing logged in user credentials to Powershell cmdlet
body: <p>I have a Windows Service running (service is running as "NT AUTHORITY\SYSTEM") on Windows 2012 Server. This service needs to get a list of VMs from a remote machine. It executes cmdlet equivalent of "get-vm -ComputerSystem RemoteServer1". Currently logged in user is 'Administrator' who has access to call get-vm on the remote machine. However, "NT AUTHORITY\SYSTEM" does not have permission to call get-vm on the remote machine. Below is the code.</p>
  
  <pre><code>PowerShell psStopVM = PowerShell.Create();
  psStopVM.AddCommand("Stop-VM");
  psStopVM.AddParameter("Name", vmName);
  psStopVM.AddParameter("ComputerName", VMHost);
  psStopVM.AddParameter("Force", true);
  
  psStopVM.Invoke(); //exception occurs here
  </code></pre>
  
  <p>When I run this code, I get the following error.
  Microsoft.HyperV.PowerShell.VirtualizationOperationFailedException: You do not have permission to perform the operation. Contact your administrator if you believe you should have permission to perform this operation. ---> System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))</p>
  
  <p>How can I pass the credentials of logged in user (Administrator) in the C# code such that I can successfully call get-vm cmdlet from C# Code?</p>