Solved

Calling Powershell from VBScript with parameters

Posted on 2011-09-21
16
3,706 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article will help you understand what HashTables are and how to use them in PowerShell.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

21 Experts available now in Live!

Get 1:1 Help Now