Vbscript valid path - powershell

I'm trying to have a scheduler to run a Vbscript, which it will run a Powershell script.
Basically here is my Vbscript
command = "powershell.exe -nologo -command C:\Users\someuser\Desktop\appAPIMonitor.ps1"
set shell = CreateObject("WScript.Shell")
shell.Run command,0
When I run above script, it worked. But when I place the script at my preferred path, the script wont run as before. So it must be the path not valid. Here is my new script
command = "powershell.exe -nologo -command C:\Users\someuser\My Work\App\Project\My.API.App\Scripts\appAPIMonitor.ps1"
set shell = CreateObject("WScript.Shell")
shell.Run command,0
Can somebody point what is wrong with my path? Is it the whitespace in My Work? Or the dot in My.Api.App?

This is the solution I managed to get.
command = "powershell.exe -nologo -file ""C:\Users\someuser\My Work\App\Project\My.API.App\Scripts\appAPIMonitor.ps1"" "
set shell = CreateObject("WScript.Shell")
shell.Run command,0
Give the last quote some white space and change -command to -file should do the trick. I solve this in my case. Hopefully help anybody in future. By the way, thanks to those who spent their precious time to answer my question.

Related

Powershell via VBScript - Environmental Variable path terminated when username contains spaces [duplicate]

This question already has answers here:
How can I run a PowerShell script with white spaces in the path from the command line?
(5 answers)
Closed 1 year ago.
I have been working on a Powershell Winforms app that requires the console window to be hidden. To do this, I am calling a .ps1 script from a .vbs file (as starting via another .ps1 script and using "-WindowStyle Hidden" still briefly shows the console window upon opening the script). I am using the following code:
Dim shell,command
command = "powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -command ""%localappdata%\test\test.ps1"""
Set shell = CreateObject("WScript.Shell")
shell.Run command,0
This works with no issues when using an account name with no spaces (e.g. "TESTUSER" will resolve to "C:\USERS\TESTUSER\AppData\Local"). However, when any part of the path generated by the "%localappdata%" environmental variable contains spaces (in this case, using something like "TEST USER 1"), Powershell will terminate the command at "C:\Users\TEST" with an error stating: "The term "C:\Users\TEST" is not recognised as the name of a cmdlet, function, script file or operable program."
I am aware that any strings with spaces in VBScript need to use two sets of surrounding double quotes (""path with spaces""), but this doesn't work in this case - I have tried every combination that has been suggested and the %localappdata% path still has the same issue.
Things I have tried:
Two quotes surrounding path (Working with no spaces):
command = "powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -command ""%localappdata%\test\test.ps1"""
Two quotes surrounding entire argument:
command = ""powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -command ""%localappdata%\test\test.ps1""""
Single quotes surrounding both:
command = "powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -command "%localappdata%\test\test.ps1""
Expanding %localappdata% variable:
Dim shell,path,command
Set shell.CreateObject("WScript.Shell")
path = shell.ExpandEnvironmentStrings("%localappdata%")
command = "powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -command " & path &"\test\test.ps1"
shell.Run command,0
Adding "Chr(34)" to replace spaces:
command = "powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -command ""%localappdata%\test\test.ps1""" & Chr(34)
Using:
"$env:localappdata" or "$env:username"
(both inside and out of "command" quotes) to replace
"%localappdata%" or "C:\Users\%username%\AppData\Local"
I have also tried various solutions provided here and here, but nothing works in this case.
I also tried replacing %localappdata% with the absolute file path ("C:\Users\TEST USER 1\AppData\Local\test\test.ps1"), but this also gives the same error.
Any help would be greatly appreciated! Thanks in advance.
Since you're invoking a script file by path rather than passing PowerShell statements to PowerShell's CLI, use the -File parameter, not -Command, which implicitly solves your quoting problems:
' Note the use of -File instead of -Command
command = "powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -File ""%localappdata%\test\test.ps1"""
The reason that -Command didn't work in your case is that its argument(s) are subject to another round of interpretation, namely as PowerShell code, after stripping syntactic " chars. during command-line parsing. This means that a path with spaces is then seen unquoted by PowerShell, causing the invocation to fail; you'd need additional quoting -either escaped " quotes (\"...\", i.e. \""...\"" from inside a VBScript string) or single quotes ('...'), which in turn would necessitate use of &, the call operator:
' With -Command: note the embedded '...' quoting and the need to call with `&`
' However, there's usually no need for -Command to invoke scripts with arguments.
command = "powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass -Command ""& '%localappdata%\test\test.ps1'"""
See this answer for more information.

How to run a PowerShell Script in the background (no window) from Windows Explorer? [duplicate]

The shortcut below executes powershell.exe and passes it the script vscode.ps1. It works except that for all my attempts, it still displays a terminal window briefly when running. You can see I've passed parameters which is supposed to prevent this, but I still see the window. What do I have to do to execute a PS script from a shortcut without the terminal window being displayed?
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -NoProfile -WindowStyle Hidden -file C:\Users\bernie\OneDrive\PowerShell\vscode.ps1 C:\Users\bernie\OneDrive\Documents\windows.txt
It's a known issue, see https://github.com/PowerShell/PowerShell/issues/3028#issuecomment-583834582
As #Ciantic mentioned, the best way to work around this issue is by using a VB script:
In, say ps-run.vbs put
Set objShell = CreateObject("Wscript.Shell")
Set args = Wscript.Arguments
For Each arg In args
objShell.Run("powershell -windowstyle hidden -executionpolicy bypass -noninteractive ""&"" ""'" & arg & "'"""),0
Next
Then use it to run the command you want, e.g.
wscript "C:\Path\To\ps-run.vbs" "C:\Other\Path\To\your-script.ps1"
I use something like this to run a task frequently without seeing any flashing windows.
Yes, Powershell can't completely do this. But VBScript can do this. Also I use Hidden Start which can start any program hidden or bypass UAC.

How to run PowerShell script without terminal window?

The shortcut below executes powershell.exe and passes it the script vscode.ps1. It works except that for all my attempts, it still displays a terminal window briefly when running. You can see I've passed parameters which is supposed to prevent this, but I still see the window. What do I have to do to execute a PS script from a shortcut without the terminal window being displayed?
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -NoProfile -WindowStyle Hidden -file C:\Users\bernie\OneDrive\PowerShell\vscode.ps1 C:\Users\bernie\OneDrive\Documents\windows.txt
It's a known issue, see https://github.com/PowerShell/PowerShell/issues/3028#issuecomment-583834582
As #Ciantic mentioned, the best way to work around this issue is by using a VB script:
In, say ps-run.vbs put
Set objShell = CreateObject("Wscript.Shell")
Set args = Wscript.Arguments
For Each arg In args
objShell.Run("powershell -windowstyle hidden -executionpolicy bypass -noninteractive ""&"" ""'" & arg & "'"""),0
Next
Then use it to run the command you want, e.g.
wscript "C:\Path\To\ps-run.vbs" "C:\Other\Path\To\your-script.ps1"
I use something like this to run a task frequently without seeing any flashing windows.
Yes, Powershell can't completely do this. But VBScript can do this. Also I use Hidden Start which can start any program hidden or bypass UAC.

Run script using Task Scheduler with user context complete silent with no popup or cmd flash

I have tried to run the script using command
cmd.exe /c Start /min powershell.exe -windowstyle hidden -file <file>.ps1
But getting a CMD window for a fraction of a second. I need it to run completely hidden.
Configure the scheduled task to run whether the user is logged on or not:
and reduce the commandline to this:
powershell.exe -File "C:\path\to\your.ps1"
This makes the task run in the background with no visible window.
I have had this issue and the only way I could fix it was to call the PowerShell script with a simple VBS wrapper:
https://github.com/gbuktenica/PsRun
http://blog.buktenica.com/run-a-powershell-task-silently/
' SYNOPSIS
' Run a PowerShell script in the user context without a script window
' EXAMPLE
' wscript.exe PsRun.vbs MyPsScript.ps1
' AUTHOR
' Glen Buktenica
Set objShell = CreateObject("Wscript.Shell")
Set args = Wscript.Arguments
For Each arg In args
Dim PSRun
PSRun = "powershell.exe -WindowStyle hidden -ExecutionPolicy bypass -NonInteractive -File " & arg
objShell.Run(PSRun),0

Calling Powershell with WshShell.Exec() method hangs the script

I am trying to call Powershell with the Exec method of the WshShell object. I am writing the script in JScript, but I have reproduced the problem in VBScript as well. Both of the following short test scripts will cause WSH to hang indefinitely:
test.js
var shell = new ActiveXObject("WScript.Shell");
WScript.Echo(shell.exec("powershell -Command $Host.Version; Exit").StdOut.ReadAll());
test.vbs
dim shell
set shell = CreateObject("WScript.Shell")
WScript.Echo shell.exec("powershell -Command $Host.Version; Exit").StdOut.ReadAll
Am I doing something wrong, or am I running into or a limitation/incompatibility? The Run method works very well, but I need to capture output, which it's not capable of doing.
Edit: I forgot to mention that my platform is Windows 7 Pro, 64-bit with PowerShell 3. I've tested on Windows XP with PowerShell 1 as well.
Edit 2: I've updated the test scripts that I'm running to fit with x0n's answer. Unfortunately, I'm still having trouble. Here are my current tests:
test.js:
var shell = new ActiveXObject("WScript.Shell");
WScript.Echo(shell.exec('powershell -noninteractive -noprofile -Command "& { echo Hello_World ; Exit }"').StdOut.ReadAll());
test.vbs:
dim shell
set shell = CreateObject("WScript.Shell")
WScript.Echo shell.exec("powershell -noninteractive -noprofile -Command ""& { echo Hello_World ; Exit }""").StdOut.ReadAll
You have to close StdIn:
var shell = new ActiveXObject("WScript.Shell");
var exec = shell.Exec('powershell -noninteractive -noprofile -Command "& { echo Hello_World ; Exit }"');
exec.StdIn.Close();
WScript.Echo(exec.StdOut.ReadAll());
Microsoft said:
StdIn is still open so PowerShell is waiting for input. (This is an
"implementation consideration" that we're hoping to fix in V2. The
PowerShell executable gathers all input before processing.) So
objexec.StdIn.Close() needs to be added.
Use:
powershell.exe -noninteractive -noprofile -command $host.version
as your string. For more complicated groups of commands, use this syntax:
powershell.exe -noninteractive -noprofile -command "& { $host.version; $host.version }"