itsbaxagain
itsbaxagain

Reputation: 31

Call PowerShell script from VBScript but windows close

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

Upvotes: 0

Views: 2327

Answers (1)

DAXaholic
DAXaholic

Reputation: 35338

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

...

Upvotes: 2

Related Questions