简体   繁体   English

powershell.exe cmd行变量

[英]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. 我想知道是否可以使用命令行调用在PowerShell脚本中使用的变量。

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. 我想每次使用不同的输入文件(csv)两次运行它。

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. 就像功能一样,PowerShell脚本可以具有参数。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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