YZlat
asked on
Calling Powershell from VBScript with parameters
I am calling Powershell script from VBScript and passing it two parameters. The problem is that my Powershell script path has spaces in it and so does my second parameter
Here is my code:
Set objShell=CreateObject("Wsc ript.Shell )
sCmd="C:\WINDOWS\system32\ WindowsPow erShell\v1 .0\powersh ell.exe C:\My Path\test.ps1 " & _
"parameter1 " & _
"this is second"
objShell.Run sCmd, 1, true
The script only runs if I change the path to C:\MyPath\test.ps1 but the thing is that it needs to be C:\My Path\test.ps1
Also when I retrieve arguments with powershell:
Write-Host $args[0]
Write-Host $args[1]
I get
parameter1
this
insetead of
parameter1
this is second
Can anyone help?
Here is my code:
Set objShell=CreateObject("Wsc
sCmd="C:\WINDOWS\system32\
"parameter1 " & _
"this is second"
objShell.Run sCmd, 1, true
The script only runs if I change the path to C:\MyPath\test.ps1 but the thing is that it needs to be C:\My Path\test.ps1
Also when I retrieve arguments with powershell:
Write-Host $args[0]
Write-Host $args[1]
I get
parameter1
this
insetead of
parameter1
this is second
Can anyone help?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Which code did you used?
ASKER
The one you posted
Set objShell=CreateObject("Wsc ript.Shell )
PSFile="""C:\My Path\test.ps1"""
Parameter1="""parameter1"" "
Parameter2="""this is second"""
sCmd="C:\WINDOWS\system32\ WindowsPow erShell\v1 .0\powersh ell.exe " & PSFile & " " & _
Parameter1 & _
Parameter2
objShell.Run sCmd, 1, true
Set objShell=CreateObject("Wsc
PSFile="""C:\My Path\test.ps1"""
Parameter1="""parameter1""
Parameter2="""this is second"""
sCmd="C:\WINDOWS\system32\
Parameter1 & _
Parameter2
objShell.Run sCmd, 1, true
ASKER
RobSampson, you cannot use single quotes this way on Powershell
I don't know exact command, but try this:
Set objShell=CreateObject("Wscript.Shell")
PSFile="""C:\My Path\test.ps1"""
Parameter1="""parameter1"""
Parameter2="""this is second"""
sCmd="C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -File " & PSFile & " " & _
Parameter1 & " " & _
Parameter2
objShell.Run sCmd, 1, true
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Figured it out myself! The format was "& 'C:\My path\test.ps1'"
ASKER
Thank you for trying to help, I split the points between the two of you
You *can* use quotes that way, and mine works fine except the end of the string should have been
& "' '" & strParam2 & "'"
instead of
& "' '" & strParam2 "'"
sCmd="C:\WINDOWS\system32\ WindowsPow erShell\v1 .0\powersh ell.exe -noexit & '" & strScript & "' '" & strParam1 & "' '" & strParam2 & "'"
I missed the last &
Glad you got it sorted anyway.
Rob.
& "' '" & strParam2 & "'"
instead of
& "' '" & strParam2 "'"
sCmd="C:\WINDOWS\system32\
I missed the last &
Glad you got it sorted anyway.
Rob.
ASKER
quotes didn't work for me at all. I had to use escape characters Chr(34)
That's odd. I tested mine on Windows 7 and it worked. Oh well, there's always more than one way to get the job done ;-)
Rob.
Rob.
ASKER
That's strange. My machine is also Windows 7
ASKER
I think it might have something to do with switching from VBScript to Powershell
It shouldn't really matter. objShell.Run just emulates the Start --> Run ability.
If you have this:
sCmd="C:\WINDOWS\system32\ WindowsPow erShell\v1 .0\powersh ell.exe -noexit & '" & strScript & "' '" & strParam1 & "' '" & strParam2 & "'"
and then put this under it:
InputBox "Running Command", "Command", sCmd
you can then copy and paste the command from that inputbox directly into a command shell, and it should run fine. That's the same way objShell.Run should treat it in VBScript.
Doesn't really matter though, since you got it fixed ;-)
Rob.
If you have this:
sCmd="C:\WINDOWS\system32\
and then put this under it:
InputBox "Running Command", "Command", sCmd
you can then copy and paste the command from that inputbox directly into a command shell, and it should run fine. That's the same way objShell.Run should treat it in VBScript.
Doesn't really matter though, since you got it fixed ;-)
Rob.
ASKER
C:\My is not recognized as a name of a cmdlet