Solved

Calling Powershell from VBScript with parameters

Posted on 2011-09-21
16
3,585 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Set OWA language and time zone in Exchange for individuals, all users or per database.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now