|
| 1 | +' Usage: |
| 2 | +' wscript.exe //E:vbscript HiddentPowershell.vbs -ExecutionPolicy ByPass -File "C:\Program Files\Get-HelloWorld.ps1" |
| 3 | +' |
| 4 | +' Will run Powershell in a hidden console, like this: |
| 5 | +' powershell.exe -ExecutionPolicy ByPass -File "C:\Program Files\Get-HelloWorld.ps1" |
| 6 | +' |
| 7 | +' I recommend that you also pass the `-WindowStyle Hidden` parameter, |
| 8 | +' so that the executing powershell script knows that it's hidden. |
| 9 | +' Using `-WindowStyle Hidden` without this script, doesn't completely hide |
| 10 | +' the powershell console. |
| 11 | +' |
| 12 | +' More Info: https://github.com/UNT-CAS/HiddenPowershell |
| 13 | + |
| 14 | +Set oShell = CreateObject("Wscript.Shell") |
| 15 | + |
| 16 | +Const LOG_EVENT_SUCCESS = 0 |
| 17 | +Const LOG_EVENT_ERROR = 1 |
| 18 | +Const LOG_EVENT_INFORMATION = 4 |
| 19 | + |
| 20 | +Dim iExitStatus : iExitStatus = LOG_EVENT_SUCCESS |
| 21 | +Dim sArgs : sArgs = "powershell.exe" |
| 22 | +Dim sMessage : sMessage = "" |
| 23 | + |
| 24 | +For Each sArg in Wscript.Arguments |
| 25 | + If InStr(sArg, " ") > 0 Then |
| 26 | + ' If there's a space in the argument, wrap it in quotes. |
| 27 | + sArgs = sArgs &" """& sArg &"""" |
| 28 | + Else |
| 29 | + sArgs = sArgs &" "& sArg |
| 30 | + End If |
| 31 | +Next |
| 32 | + |
| 33 | +sMessage = "HiddenPowershell Running: " _ |
| 34 | + & vbCrLf & vbTab & Wscript.ScriptFullName _ |
| 35 | + & vbCrLf & vbTab & sArgs |
| 36 | +oShell.LogEvent LOG_EVENT_INFORMATION, sMessage |
| 37 | + |
| 38 | +iReturn = oShell.Run(sArgs, 0, False) |
| 39 | + |
| 40 | +If iReturn <> 0 Then |
| 41 | + iExitStatus = LOG_EVENT_ERROR |
| 42 | +End If |
| 43 | + |
| 44 | +sMessage = "HiddenPowershell Exited: " _ |
| 45 | + & vbCrLf & vbTab & Wscript.ScriptFullName _ |
| 46 | + & vbCrLf & vbTab & sArgs _ |
| 47 | + & vbCrLf & vbTab & "Exit Code: " &iReturn |
| 48 | +oShell.LogEvent iExitStatus, sMessage |
| 49 | + |
| 50 | +Wscript.Quit iReturn |
0 commit comments