Advertisement
Guest User

Untitled

a guest
May 25th, 2010
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # sudo.ps1
  2. #
  3. # Authors: Brian Marsh, mrigns, This guy: http://tsukasa.jidder.de/blog/2008/03/15/scripting-sudo-with-powershell
  4. #
  5. # Sources:
  6. #       http://tsukasa.jidder.de/blog/2008/03/15/scripting-sudo-with-powershell
  7. #       http://www.ainotenshi.org/%E2%80%98sudo%E2%80%99-for-powershell-sorta
  8. #
  9. # Version:
  10. #       1.0     Initial version
  11. #       1.1     added -ps flag, cleaned up passed $file/$script full path
  12. #       1.2     Comments
  13. #       1.3     Fixed passing working directory to powershell/auto closing
  14.  
  15. param(
  16.         [switch]$ps,               # Switch for running args as powershell script
  17.         [string]$file,             # Script/Program to run
  18.         [string]$arguments = $args # Arguments to program/script
  19.      )
  20.  
  21. # Find our powershell full path
  22. $powershell = (get-command powershell).definition
  23.  
  24. # Get current directory
  25. $dir = get-location
  26.  
  27. #If we're running this as a elevated powershell script
  28. if ($ps){
  29.  
  30.         # Script verification
  31.         if([System.IO.File]::Exists("$(get-location)\$file")) {
  32.  
  33.                 # Set the $script to full path of the ps script
  34.                 $script = (get-childitem $file).fullname
  35.         }
  36.  
  37.         # Create a powershell process
  38.         $psi = new-object System.Diagnostics.ProcessStartInfo $powershell
  39.  
  40.         $psi.WorkingDirectory = Get-Location
  41.  
  42.         # Combine the script and its arguments
  43.         $sArgs = $script + " " + $arguments
  44.  
  45.         # Set the arguments to be the ps script and it's arguments
  46.         $psi.Arguments = "-noexit -command set-location $dir; $sArgs"
  47.  
  48.         # Magic to run as elevated
  49.         $psi.Verb = "runas";
  50. }
  51.  
  52. # We're running something other than a powershells script
  53. else {
  54.  
  55.         # File verification
  56.         if([System.IO.File]::Exists("$(get-location)\$file")) {
  57.  
  58.                 # Get full path
  59.                 $file = (get-childitem $file).fullname
  60.         }
  61.  
  62.         # Same as above, create proccess/working directory/arguments/runas
  63.         $psi = new-object System.Diagnostics.ProcessStartInfo $file
  64.         $psi.Arguments = $arguments
  65.         $psi.Verb = "runas"
  66. }
  67.  
  68. # Start the process
  69. [System.Diagnostics.Process]::Start($psi)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement