[英]Value of a Variable as Part of the Name of a Variable
I'm still kind of new to Powershell and I want to know if it's possible to use the value of a variable as a part of the variable name. 我仍然是Powershell的新手,我想知道是否可以将变量的值用作变量名的一部分。
$a = "Stuff";
$abc???_def; # ??? = It's where I want to use $a
You can do this with New-Variable: 您可以使用New-Variable执行此操作:
$a = "Stuff"
New-Variable -Name "abc$($a)_def" -Value 'This is abcStuff_def'
Get-Variable abc*
Name Value
---- -----
abcStuff_def This is abcStuff_def
But I suspect a hash table might be a better choice: 但我怀疑哈希表可能是更好的选择:
$abc = @{}
$a='Stuff'
$abc.$a = 'This is abcStuff_def'
$abc
Name Value
---- -----
Stuff This is abcStuff_def
Looks like the way to do it is to use New-Variable
syntax. 看起来这样做的方法是使用
New-Variable
语法。
For your example, you would do 举个例子,你会这样做
New-Variable "abc&a_def" "value"
For more info, see http://technet.microsoft.com/en-us/library/hh849913.aspx 有关详细信息,请参阅http://technet.microsoft.com/en-us/library/hh849913.aspx
Much credit goes to fsimonazzi for his answer to this question: Powershell, how do I increment variable names? fsimonazzi对这个问题的回答很有用: Powershell,如何增加变量名称?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.