Solved

Calling Powershell from VBScript with parameters

Posted on 2011-09-21
16
3,959 Views
Last Modified: 2012-05-12
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?


0
Comment
Question by:YZlat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
  • 3
16 Comments
 
LVL 11

Assisted Solution

by:X_layer
X_layer earned 350 total points
ID: 36576691
Try like 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 " & PSFile & " " & _
      Parameter1 & _
      Parameter2
objShell.Run sCmd, 1, true

Open in new window

0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 150 total points
ID: 36580572
Hi, try this.

Regards,

Rob.
Set objShell=CreateObject("Wscript.Shell)
strScript = "C:\My Path\test.ps1"
strParam1 = "parameter1"
strParam2 = "this is second"
sCmd="C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -noexit & '" & strScript & "' '" & strParam1 & "' '" & strParam2 "'"
objShell.Run sCmd, 1, True

Open in new window

0
 
LVL 35

Author Comment

by:YZlat
ID: 36580688
I got an error:

C:\My  is not recognized as a name of a cmdlet
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:X_layer
ID: 36580722
Which code did you used?
0
 
LVL 35

Author Comment

by:YZlat
ID: 36580867
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
0
 
LVL 35

Author Comment

by:YZlat
ID: 36580900
RobSampson, you cannot use single quotes this way on Powershell
0
 
LVL 11

Expert Comment

by:X_layer
ID: 36581701
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

0
 
LVL 35

Accepted Solution

by:
YZlat earned 0 total points
ID: 36582945
Figured it out!

It needed to be in the following format:

"& 'C:\My Path\test.ps1'"

So I did

 
Set objShell=CreateObject("Wscript.Shell)
sCmd="C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe  " & Chr(34) & "& '" & myPath & "'" & Chr(34) &  " " Chr(34) & param1 & Chr(34) & " " & Chr(34) & param2 & Chr(34)
objShell.Run sCmd, 1, true

Open in new window

0
 
LVL 35

Author Closing Comment

by:YZlat
ID: 36708125
Figured it out myself! The format was "& 'C:\My path\test.ps1'"
0
 
LVL 35

Author Comment

by:YZlat
ID: 36582980
Thank you for trying to help, I split the points between the two of you
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36583852
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.
0
 
LVL 35

Author Comment

by:YZlat
ID: 36586445
quotes didn't work for me at all. I had to use escape characters Chr(34)
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36586560
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.
0
 
LVL 35

Author Comment

by:YZlat
ID: 36588463
That's strange. My machine is also Windows 7
0
 
LVL 35

Author Comment

by:YZlat
ID: 36588471
I think it might have something to do with switching from VBScript to Powershell
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36591002
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question