YZlat
asked on
Pass a parameter to a powershell file from batch file
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?
What's the best way to do that?
ASKER
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
and how do I pass a paramteter to powershell? A parameter is a file path to another file C:\Temp\temp_file.bmp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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.
ASKER
C:\Windows\System32\Window sPowerShel l\v1.0\pow ershell.ex e 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\Window sPowerShel l\v1.0\pow ershell.ex e 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\Window sPowerShel l\v1.0\pow ershell.ex e C:\Test\My Folder\test.ps1 %1
only caputres part of the file: C:\Program
so I need to run
C:\Windows\System32\Window sPowerShel l\v1.0\pow ershell.ex e 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
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\Window
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\Window
only caputres part of the file: C:\Program
so I need to run
C:\Windows\System32\Window
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
ASKER
the part of powershell script that grabs parameter is
$args[0]
$args[0]
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.
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.
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