Solved

Calling Powershell from VBScript with parameters

Posted on 2011-09-21
16
3,784 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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