Solved

Pass a parameter to a powershell file from batch file

Posted on 2011-02-24
8
1,322 Views
Last Modified: 2012-05-11
I need to run a Powershell ps1 file from batch file and pass it one paramter - file path: C:\Test\My Folder\test.ps1

What's the best way to do that?
0
Comment
Question by:YZlat
  • 4
  • 4
8 Comments
 
LVL 27

Expert Comment

by:KenMcF
ID: 34970411
Can you post the powershell script?

you should  be able to run it from a batch file just like running from a powershell console. in the batch try somethign like this followed by any paramenters you need.

powershell.exe  C:\Test\My Folder\test.ps1



0
 
LVL 35

Author Comment

by:YZlat
ID: 34970456
do i need full path to powershell file in my batch?

and how do I pass a paramteter to powershell?  A parameter is a file path to another file C:\Temp\temp_file.bmp


0
 
LVL 27

Accepted Solution

by:
KenMcF earned 500 total points
ID: 34970564
Can you post the script?
it depends on your env path, you may need the full path to powershell.exe
the default is
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
then depending on what parametes the script needs you can put them after the ps1 file.

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\Test\My Folder\test.ps1 -file c:\temp\temp_file.bmp



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 35

Author Comment

by:YZlat
ID: 34970646
My script has no problem, I do not need to do anything to it, what I need is to call my script from a batch file and pass it a parameter
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 34970730
ok, then depending on what parameter needs past you can do what I posted before. It will all depend on how the script is written.
0
 
LVL 35

Author Comment

by:YZlat
ID: 34970781
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\Test\My Folder\test.ps1 -file c:\temp\temp_file.bmp



didn't wotk, since it would capture the "-file" as a first argument.  

I am calling my batch file from another program and I send it a file path as a string, so my batch has the following code:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\Test\My Folder\test.ps1 %1

and it works if the file passed to batch does not contain spaces, otherwise I need single quotes around it

for instance

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\Test\My Folder\test.ps1 %1

only caputres part of the file: C:\Program

so I need to run

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe C:\Test\My Folder\test.ps1 'C:\program Files\Test\my_file.bmp'

and then it works.  But the difficulty is in batch file I get it from somewhere else, so I have to pass it as %1




0
 
LVL 35

Author Comment

by:YZlat
ID: 34970888
the part of powershell script that grabs parameter is

$args[0]
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 34970957
that is why I wanted to see the PS script.
You can also add this

Param(
$File)

than you can pass -file

I will have to do some testing, i have never tried to pass params through a batch loop to powershell.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
A procedure for exporting installed hotfix details of remote computers using powershell
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

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