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
3

1 Answer 1

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

...
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.