[英]using variable in PowerShell array element
How to I add variables data inside the string element of an array? 如何在数组的字符串元素内添加变量数据? If I do $s.Length
, the output is 1 instead of 2. 如果我执行$s.Length
,则输出为1而不是2。
$IPAddress = '192.168.1.1'
[string[]]$s = (
'https://google.com/' + $IPAddress + '/hostname',
'https://google.com/' + $IPAddress + '/DNS'
)
foreach ($element in $s) {
Write-Host $element
}
$s
contains a single string because of the way you define the array. 由于定义数组的方式, $s
包含一个字符串。 The concatenation operator ( +
) has a weaker precedence than the array construction operator ( ,
). 串联运算符( +
)的优先级比数组构造运算符( ,
)的优先级弱。 Because of that a statement 因此,一个声明
'foo' + $v + 'bar', 'foo' + $v + 'baz'
actually works like this: 实际上是这样的:
'foo' + $v + @('bar', 'foo') + $v + 'baz'
Due to the string concatenation operation the array is mangled into a space-separated string (the separator is defined in the automatic variable $OFS
), resulting in this: 由于进行了字符串串联操作,该数组被整形为以空格分隔的字符串(分隔符在自动变量 $OFS
定义),结果是:
'foo' + $v + 'bar foo' + $v + 'baz'
To avoid this behavior you need to either put the concatenation operations in grouping expressions: 为了避免这种行为,您需要将串联操作放在分组表达式中:
$s = ('https://google.com/' + $IPAddress + '/hostname'),
('https://google.com/' + $IPAddress + '/DNS')
or inline the variables (requires double-quoted strings): 或内联变量(需要双引号字符串):
$s = "https://google.com/${IPAddress}/hostname",
"https://google.com/${IPAddress}/DNS"
You could also use the format operator , but that requires grouping expressions as well: 您还可以使用format运算符 ,但这也需要对表达式进行分组:
$s = ('https://google.com/{0}/hostname' -f $IPAddress),
('https://google.com/{0}/DNS' -f $IPAddress)
Side note: Casting the variable to [string[]]
is optional. 旁注:将变量强制转换为[string[]]
是可选的。 Using the comma operator will give you an array even without an explicit cast. 即使没有显式强制转换,使用逗号运算符也会为您提供一个数组。
The simplest way to accomplish what you are trying (string expansion) is: 完成您尝试的工作(字符串扩展)的最简单方法是:
$s = "https://google.com/$IPAddress/hostname",
"https://google.com/$IPAddress/DNS"
By using double quotes it will automatically expand $IPAddress
within the strings. 通过使用双引号,它将自动在字符串中扩展$IPAddress
。 This works best when the variable is a string, as more complex objects may not perform as expected. 当变量是字符串时,此方法效果最好,因为更复杂的对象可能无法按预期执行。 If you need to reference a property of an object in this manner you will need to wrap it in $()
, for example "Hello $($User.Name)!"
如果需要以这种方式引用对象的属性,则需要将其包装在$()
,例如"Hello $($User.Name)!"
to expand the Name
property of the $User
object. 展开$User
对象的Name
属性。
TheMadTechnician beat me to it by a few seconds, but if you prefer to construct the string expressions explicitly, wrap them in parens: The MadTechnician击败了我几秒钟,但如果您希望显式构造字符串表达式,请将其包装在括号中:
$IPAddress = '192.168.1.1'
[string[]]$s = (
('https://google.com/'+$IPAddress+'/hostname'),
('https://google.com/'+$IPAddress+'/DNS'))
foreach ($element in $s)
{
Write-Host $element
}
The parens force the expressions inside to be evaluated first. 括号迫使内部的表达式首先被求值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.