简体   繁体   中英

powershell.exe cmd line variables

I was wondering if is possible to call a variable for use in a PowerShell script using the command line.

For instance, I have a script that I would like to run tonight. I want to run it at two times using a different input file (csv) each time.

Can I call...

powershell.exe -command {$input = <csv path>} -file <script path>

and discretely call a variable for use in the script from outside of the script? Do I just need to make two copies of the script with the variable change included in the script itself? Is there a way to do this that I am missing?

PowerShell scripts can have parameters, just as functions can. That way you can have one script and just call it with different input file names. Also see here for a similar question, here what more you can do with parameters and here for more commandline options.

Script

Param([Parameter(Mandatory=$True)] [string] $FileName)

Write-Output "Script called with $FileName"

Call

powershell.exe -file Untitled2.ps1 Test3.csv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM