Advertisement
guyrleech

Check if Get-Process has -Includeusername & is elevated

Jun 11th, 2021
1,599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. [hashtable]$processes = @{}
  3. [hashtable]$processParameters = @{}
  4.  
  5. if( (Get-Command -Name get-process).Parameters.ContainsKey( 'IncludeUsername' ) )
  6. {
  7.     $myWindowsPrincipal = New-Object -TypeName System.Security.Principal.WindowsPrincipal([System.Security.Principal.WindowsIdentity]::GetCurrent())
  8.  
  9.     if ( $myWindowsPrincipal -and $myWindowsPrincipal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator ))
  10.     {
  11.         $processParameters.Add( 'IncludeUsername' , $true )
  12.     }
  13. }
  14.  
  15. ## Create hashtable of processes keyed on id so we can do a fast lookup via pid
  16. Get-Process @processParameters | ForEach-Object `
  17. {
  18.     $processes.Add( [uint32]$_.Id , $_ )
  19. }
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement