0

This VBScript runs a few PowerShell scripts. I put the invocation of notepad.exe there to make sure it ran as an admin.

My PowerShell scripts open consoles but then they close immediately.
I would like to have them stay open.

What is wrong?

Here is my VBScript:

RunAsAdmin()

Set shell = WScript.CreateObject("WScript.shell")
shell.Run("powershell.exe -noexit -executionpolicy bypass -file .\Source\RemoveWindows10Apps-2.0.ps1"), 1 , True

Set shell = WScript.CreateObject("WScript.shell")
shell.Run("powershell.exe -noexit -executionpolicy bypass -file .\Source\ChangeWin10StartLayout.ps1"), 1 , True

Set shell = WScript.CreateObject("WScript.shell")
shell.Run("powershell.exe -noexit -executionpolicy bypass -file .\Source\NewFolder.ps1"), 1 , True

Set shell = WScript.CreateObject("WScript.shell")
shell.Run("notepad.exe"), 1 , True


Function RunAsAdmin()
  Dim objAPP
  If WScript.Arguments.length = 0 Then
    Set objAPP = CreateObject("Shell.Application")
    objAPP.ShellExecute "wscript.exe", """" & _
    WScript.ScriptFullName & """" & " RunAsAdministrator",,"runas", 1
    WScript.Quit
  End If
End Function
DAXaholic
  • 33,312
  • 6
  • 76
  • 74
  • Possible duplicate of [How to keep the shell window open after running a PowerShell script?](http://stackoverflow.com/questions/16739322/how-to-keep-the-shell-window-open-after-running-a-powershell-script) – user4317867 Jul 16 '16 at 16:38
  • It has something to do with the RunAsAdmin function because when I take it out my windows stay open but they don't run as an admin. – itsbaxagain Jul 16 '16 at 17:43
  • Does the answer have to be in VBScript? Here is some powershell that [elevates within the script](https://blogs.msdn.microsoft.com/virtual_pc_guy/2010/09/23/a-self-elevating-powershell-script/) – user4317867 Jul 16 '16 at 17:55

1 Answers1

2

I guess it is because you use relative paths so when your script will call itself as admin your current directory will change to ...\WINDOWS\system32.

Try it with building absolute paths and pass them to PowerShell like so

RunAsAdmin()

strScriptPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strScriptPath )
strScriptFolder = objFSO.GetParentFolderName(objFile) 

Set shell = WScript.CreateObject("WScript.shell")
shell.Run("powershell.exe -noexit -executionpolicy bypass -file " & strScriptFolder & "\Source\RemoveWindows10Apps-2.0.ps1"), 1 , True

...
DAXaholic
  • 33,312
  • 6
  • 76
  • 74