Most likely test.ps1
doesn't reside in the working directory of the VBScript run, so it isn't found and thus not executed. If you have VBScript and PowerShell script in the same directory it's best to build the PowerShell script path from the VBScript path:
Set fso = CreateObject("Scripting.FileSystemObject")
dir = fso.GetParentFolderName(WScript.ScriptFullName)
psscript = fso.BuildPath(dir, "test.ps1")
Then you can build the command string like this:
cmd = "powershell.exe -NoProfile -File """ & psscript & """"
The additional double quotes are to take care of spaces in the path. I also recommend using the parameter -File
instead of the convoluted -Command "...; exit $LASTEXITCODE"
.
As an alternative (if you want to stick with running the PowerShell script from the current working directory) you could also change the directory in your script:
Set fso = CreateObject("Scripting.FileSystemObject")
Set sh = CreateObject("WScript.Shell")
sh.CurrentDirectory = fso.GetParentFolderName(WScript.ScriptFullName)
...
cmd = "powershell.exe -NoProfile -File .\test.ps1"
Edit: HTAs have a different runtime environment than regular VBScripts. The HTA engine doesn't provide a WScript
object, so WScript.ScriptFullName
doesn't work in HTAs. You can, however, determine the script directory from the document location:
Set fso = CreateObject("Scripting.FileSystemObject")
dir = fso.GetParentFolderName(Replace(document.location.href, "file:///", ""))
psscript = fso.BuildPath(dir, "test.ps1")
If you want to canonicalize path separators to backslashes you can do it either like this:
psscript = fso.BuildPath(Replace(dir, "/", "\"), "test.ps1")
or like this:
psscript = fso.BuildPath(fso.GetFolder(dir).Path, "test.ps1")