[英]Automated dictionary entry filling in PowerShell from input command line arguments
In PowerShell script I have a dictionary(variable input below) which I want to fill from command line arguments.在 PowerShell 脚本中,我有一个字典(下面的变量输入),我想从命令行参数中填充它。
script to execute: ./win.ps1 -var1 <value> -var2 <value> -var3<value>
param($var1, $var2, $var3)
$input = @{"var1" = ""; "var2" = ""; "var3" = ""}
if ($var1){
$input["var1"] = $var1}
if ($var2){
$input["var2"] = $var2}
if ($var3){
$input["var3"] = $var3}
If I have a lot of arguments can I automate the above task of filling the dictionary entry (may be in a loop), provided the key of dictionary and the param variable name, where we are receiving the input parameters are same.如果我有很多参数,我可以自动执行上述填充字典条目的任务(可能在循环中),前提是字典的键和 param 变量名称,我们接收输入参数的位置相同。
you are using $Input
as a variable.您正在使用
$Input
作为变量。 that is an automatic variable, so you likely otta NOT do that.这是一个自动变量,因此您可能不会这样做。 you are likely to get some very odd results.
你可能会得到一些非常奇怪的结果。
from Get-Help about_Automatic_Variables
...来自
Get-Help about_Automatic_Variables
...
$Input Contains an enumerator that enumerates all input that is passed to a function.
$Input 包含一个枚举器,用于枚举传递给函数的所有输入。 The $input variable is available only to functions and script blocks (which are unnamed functions).
$input 变量仅可用于函数和脚本块(它们是未命名的函数)。 In the Process block of a function, the $input variable enumerates the object that is currently in the pipeline.
在函数的 Process 块中, $input 变量枚举当前在管道中的对象。 When the Process block completes, there are no objects left in the pipeline, so the $input variable enumerates an empty collection.
当 Process 块完成时,管道中没有剩余的对象,因此 $input 变量枚举一个空集合。 If the function does not have a Process block, then in the End block, the $input variable enumerates the collection of all input to the function.
如果函数没有 Process 块,则在 End 块中, $input 变量枚举函数的所有输入的集合。
hope that helps,希望有帮助,
lee李
I would suggest you to have it in a csv file and pass the file to the script.我建议你把它放在一个 csv 文件中并将文件传递给脚本。
Name Value
---- -----
var2 2
var3 3
var1 1
And inside your script在你的脚本中
$HashTable = Import-Csv -Path $CSVPath | ForEach-Object -Process { @{$_.Name=$_.Value} }
Just use $args variable, and use it for set an hashtable.只需使用 $args 变量,并将其用于设置哈希表。 Remove param($var1, $var2, $var3) into your script, its not necessary in your case
将 param($var1, $var2, $var3) 删除到您的脚本中,在您的情况下不需要
You could use $args automatic variable and hashtable to hold parameter name and its value.您可以使用 $args 自动变量和哈希表来保存参数名称及其值。 Like so:
像这样:
function Do-Things{
$hashtable = @{}
for ($i = 0; $i -lt $args.Count; $i += 2)
{
$hashtable[$args[$i]] = $args[$i+1]
}
$hashtable
}
$Results = Do-Things -af df -fds 421 -fgsd hfd
$Results
Output:输出:
Name Value
---- -----
-fds 421
-fgsd hfd
-af df
You could also access specific keys:您还可以访问特定的键:
$Results['-af']
Output:输出:
df
Also, like @Lee_Dailey already mention do not use $Input
automatic variable to hold data.另外,就像@Lee_Dailey已经提到的那样,不要使用
$Input
自动变量来保存数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.