Advertisement
opexxx

sudo.vbs

Oct 10th, 2018
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' //***************************************************************************
  2. ' // ***** Script Header *****
  3. ' //
  4. ' // File:      Sudo.vbs
  5. ' // Additional files required:  Sudo.cmd
  6. ' //
  7. ' // Purpose:   To provide a command line method of launching applications that
  8. ' //            prompt for elevation (Run as Administrator).
  9. ' //
  10. ' // Usage:     (Not used directly.  Launched from Sudo.cmd)
  11. ' //
  12. ' // Version:   1.1.0
  13. ' // Date :     20/04/2018
  14. ' //
  15. ' // History:
  16. ' // 1.0.0   01-Feb-2007  Created initial version used internally.
  17. ' // 1.0.1   01-Mar-2007  Added detailed usage output.
  18. ' // 1.0.0   23-Oct-2013  Released on chocolatey
  19. ' // 1.0.1   08-Sep-2014  chocolatey framework changes required new version
  20. ' // 1.0.2   12-Jun-2015  chocolatey framework changes required new version
  21. ' // 1.1.0   20-Apr-2018  Added the option of keeping current directory
  22. ' //                      context by using the inserted . or _ as first command argument
  23. ' //                      when used a context cmd shell window is opened for setting
  24. ' //                      current directory context using _ hides this window
  25. ' //
  26. ' // ***** End Header *****
  27. ' //***************************************************************************
  28.  
  29. Set objShell = CreateObject("Shell.Application")
  30. Set objWshShell = WScript.CreateObject("WScript.Shell")
  31. Set objWshProcessEnv = objWshShell.Environment("PROCESS")
  32.  
  33. ' Get execution context information from sudo.cmd passed in through environment variables.
  34. strCommandLine = objWshProcessEnv("SUDO_CMDLINE")
  35. strApplicationArgument1 = objWshProcessEnv("SUDO_ARG1")
  36. strApplicationArgument2 = objWshProcessEnv("SUDO_ARG2")
  37. strCurrentDirectory = objWshProcessEnv("SUDO_CD")
  38. strCurrentDrive = objWshProcessEnv("SUDO_DRIVE")
  39.  
  40. If (WScript.Arguments.Count >= 1) Then
  41.     strFlag = WScript.Arguments(0)
  42.     If (strFlag = "") OR (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _
  43.         OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "-?") OR (strFlag="h") _
  44.         OR (strFlag = "?") Then
  45.         DisplayUsage
  46.         WScript.Quit
  47.     Else
  48.  
  49.         boolKeepCurrentDirectoryContext = ((strApplicationArgument1 = ".") OR (strApplicationArgument1 = "_"))
  50.         boolHideCurrentDirectoryContextCommandWindow = (strApplicationArgument1 = "_")
  51.  
  52.         If (boolKeepCurrentDirectoryContext = True) Then
  53.            
  54.             If (Len(strApplicationArgument2) > 0) Then  
  55.                 strApplication = strApplicationArgument2
  56.                 strArguments = Right(strCommandLine, (Len(strCommandLine) - (Len(strApplicationArgument1) + Len(strApplicationArgument2) + 1)))
  57.             Else
  58.                 strApplication = "cmd"
  59.                 strArguments = ""
  60.             End If
  61.  
  62.             'Shell.ShellExecute method https://msdn.microsoft.com/en-us/library/windows/desktop/gg537745(v=vs.85).aspx
  63.  
  64.             If (boolHideCurrentDirectoryContextCommandWindow) Then            
  65.                 vShow = 0 'Open the application with a hidden window
  66.                'vShow = 2 'Open the application with a minimized window
  67.                'vShow = 7 'Open the application with a minimized window. The active window remains active.
  68.            Else
  69.                 'vShow = 1  'Open the application with a normal window. If the window is minimized or maximized, the system restores it to its original size and position.
  70.                vShow = 10 'Open the application with its window in the default state specified by the application.
  71.            End If
  72.  
  73.             'WScript.Echo "objShell.ShellExecute ""cmd"",""/C """ & strCurrentDrive & " && cd " & strCurrentDirectory & " && " & strApplication & " " & strArguments & """, """ & strCurrentDirectory & """, ""runas"", " & vShow
  74.            objShell.ShellExecute "cmd","/C """ & strCurrentDrive & " && cd " & strCurrentDirectory & " && " & strApplication & " " & strArguments & """", strCurrentDirectory, "runas", vShow
  75.  
  76.         Else
  77.             strApplication = strApplicationArgument1
  78.             strArguments = Right(strCommandLine, (Len(strCommandLine) - Len(strApplication)))
  79.        
  80.             'WScript.Echo "objShell.ShellExecute """ & strApplication & """, """ & strArguments & """, """ & strCurrentDirectory & """, ""runas"" "
  81.            objShell.ShellExecute strApplication, strArguments, strCurrentDirectory, "runas"
  82.  
  83.             'Note that strCurrentDirectory is not respected when runas is used, why the piped commands on a command shell is in use above
  84.  
  85.         End if
  86.          
  87.     End If
  88. Else
  89.     DisplayUsage
  90.     WScript.Quit
  91. End If
  92.  
  93.  
  94. Sub DisplayUsage
  95.  
  96.     WScript.Echo "Start application that prompt for elevation (Run as Administrator)." & vbCrLf & _
  97.                  "" & vbCrLf & _
  98.                 "SUDO command" & vbCrLf & _
  99.                 "" & vbCrLf & _
  100.                 "    elevate command" & vbCrLf & _
  101.                 "" & vbCrLf & _
  102.                 "SUDO . command" & vbCrLf & _
  103.                 "" & vbCrLf & _
  104.                 "    elevate and preserve current directory" & vbCrLf & _
  105.                 "" & vbCrLf & _
  106.                 "SUDO _ command" & vbCrLf & _
  107.                 "" & vbCrLf & _
  108.                 "    elevate and preserve current directory but hide context window used to set context" & vbCrLf & _
  109.                 "" & vbCrLf & _
  110.                 "SUDO notepad %cd%\hosts" & vbCrLf & _
  111.                 "" & vbCrLf & _
  112.                 "    Note also the option of getting the current directory using %CD%\ where you would have used .\" & vbCrLf & _
  113.                  "" & vbCrLf & _
  114.                  "" & vbCrLf & _
  115.                  "Examples:" & vbCrLf & _
  116.                  "" & vbCrLf & _
  117.                  "    sudo cmd /k cd ""C:\Program Files""" & vbCrLf & _
  118.                  "    or       sudo . cmd" & vbCrLf & _
  119.                  "    or       sudo . " & vbCrLf & _
  120.                  "" & vbCrLf & _
  121.                  "    sudo powershell -NoExit -Command Set-Location 'C:\Windows'" & vbCrLf & _
  122.                  "    or       sudo . powershell" & vbCrLf & _
  123.                  "" & vbCrLf & _
  124.                  "    sudo notepad ""c:\Windows\System32\drivers\etc\hosts""" & vbCrLf & _
  125.                  "    or       sudo _ notepad hosts" & vbCrLf & _
  126.                  "    or       sudo notepad %cd%\hosts" & vbCrLf & _
  127.                  "" & vbCrLf & _
  128.                  "" & vbCrLf & _
  129.                  "Note that keeping current directory requires an elevated cmd shell " & vbCrLf & _
  130.                  "that sets directory context before executing your command. " & vbCrLf & _
  131.                  "" & vbCrLf & _
  132.                  "You can also keep context by working with %CD%\ on file locations." & vbCrLf & _
  133.                  "" & vbCrLf & _
  134.                  "Usage with scripts: When using the sudo command with scripts such as" & vbCrLf & _
  135.                  "Windows Script Host or Windows PowerShell scripts, you should specify" & vbCrLf & _
  136.                  "the script host executable (i.e., wscript, cscript, powershell) as the " & vbCrLf & _
  137.                  "application." & vbCrLf & _
  138.                  "" & vbCrLf & _
  139.                  "Sample usage with scripts:" & vbCrLf & _
  140.                  "" & vbCrLf & _
  141.                  "    sudo cscript ""C:\windows\system32\slmgr.vbs"" " & vbCrLf & _
  142.                  "    sudo powershell -NoExit -Command & 'C:\Temp\Test.ps1'" & vbCrLf & _
  143.                  "" & vbCrLf & _
  144.                  "" & vbCrLf & _
  145.                  "The sudo command consists of sudo.cmd and sudo.vbs" & vbCrLf
  146.  
  147. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement