?
Solved

Calling Powershell from VBScript with parameters

Posted on 2011-09-21
16
Medium Priority
?
4,262 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 1400 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 600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

770 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