Link to home
Start Free TrialLog in
Avatar of YZlat
YZlatFlag for United States of America

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("Wscript.Shell)
sCmd="C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.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?


SOLUTION
Avatar of X Layer
X Layer
Flag of Slovenia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of YZlat

ASKER

I got an error:

C:\My  is not recognized as a name of a cmdlet
Which code did you used?
Avatar of YZlat

ASKER

The one you posted
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 " & PSFile & " " & _
      Parameter1 & _
      Parameter2
objShell.Run sCmd, 1, true
Avatar of YZlat

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

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of YZlat

ASKER

Figured it out myself! The format was "& 'C:\My path\test.ps1'"
Avatar of YZlat

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\WindowsPowerShell\v1.0\powershell.exe -noexit & '" & strScript & "' '" & strParam1 & "' '" & strParam2 & "'"


I missed the last &

Glad you got it sorted anyway.

Rob.
Avatar of YZlat

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.
Avatar of YZlat

ASKER

That's strange. My machine is also Windows 7
Avatar of YZlat

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\WindowsPowerShell\v1.0\powershell.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.