Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pass a parameter to a powershell file from batch file

Posted on 2011-02-24
8
Medium Priority
?
1,381 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 2000 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Screencast - Getting to Know the Pipeline

971 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