Solved

Pass a parameter to a powershell file from batch file

Posted on 2011-02-24
8
1,304 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

10 Experts available now in Live!

Get 1:1 Help Now