title: Powershell run privileged commands from non privileged user by passing credentials
body: <p>i've some trouble with executing command that need more privileges than the calling user has.</p>
  
  <p>I wrote a "admin repository" of powershell scripts &amp; snippets for management reasons. I create a auto importer script and also an auto update if i release a new tag. Works like a charm!</p>
  
  <p>But than we decieded to split some privileges to other users to fulfill some <a href="https://docs.microsoft.com/de-de/powershell/jea/overview" rel="nofollow noreferrer">JEA</a> requirements.</p>
  
  <p>Now our unprivileged "working" users are importing the repository and are not allowed to run every command. (eg. querying DHCP sever)</p>
  
  <p>I thought it would be no issue - thought about the Get-Credential simply running the priv features with the priv user.... but i was wrong... it's not simple as i thought.</p>
  
  <p>First issue was that the command does not accept a -credential param.
  i ended up with something like this:</p>
  
  <pre><code>$pinfo = New-Object System.Diagnostics.ProcessStartInfo
  $pinfo.FileName = "powershell"
  $pinfo.RedirectStandardError = $true
  $pinfo.RedirectStandardOutput = $true
  $pinfo.UseShellExecute = $false
  $pinfo.Domain = $cred.UserName.Split('\')[0] 
  $pinfo.Password = $cred.Password
  $pinfo.UserName = $cred.UserName.Split('\')[1]
  $pinfo.CreateNoWindow = $true
  $pinfo.Arguments = "Get-DhcpServerv4Scope -computername $server"
  $p = New-Object System.Diagnostics.Process
  $p.StartInfo = $pinfo
  $p.Start() | Out-Null
  $stdout = $p.StandardOutput.ReadToEnd()
  $stderr = $p.StandardError.ReadToEnd()
  $p.WaitForExit()
  </code></pre>
  
  <p>it took me some time to get the output of this session to a variable...
  fist i wrote output to a tmp file and red it with the other session, but it feels like I do it terrible wrong.</p>
  
  <p>Now I've to parse the output, create a template and build the object again - i've to change half of the script and it become slow and I dont like the way....</p>
  
  <p>I can't escape the feeling that there is a better way - so im asking you :)</p>
  
  <p>Best Regards</p>
  
  <p>David Bla</p>