String PowerShell:What';“有什么不对吗?”${uu.Name}…“;?
为什么我不能像使用其他变量一样在文本中使用$String PowerShell:What';“有什么不对吗?”${uu.Name}…“;?,string,powershell,concatenation,String,Powershell,Concatenation,为什么我不能像使用其他变量一样在文本中使用$ Get-ChildItem -Path $path -filter *.mp3 | foreach { $count++; write-host "File${count}=${_.Name}"; } 我知道我可以这样写: Get-ChildItem -Path $path -filter *.mp3 | foreach { $count++; write
Get-ChildItem -Path $path -filter *.mp3 | foreach {
$count++;
write-host "File${count}=${_.Name}";
}
我知道我可以这样写:
Get-ChildItem -Path $path -filter *.mp3 | foreach {
$count++;
write-host "File${count}=$($_.Name)";
}
当您编写
${{.Name}
时,实际上是在请求名为{.Name
的变量,而不是${/code>变量的Name
属性
PS > ${_.Name} = "test"
PS > Get-Variable _*
Name Value
---- -----
_.Name test
之所以$($.Name)
起作用,是因为$()
的意思是“先处理这个”,所以您可以在里面指定您想要的任何内容。在本例中,您只需指定一个变量名和所需的属性,但也可以使其更复杂,如:
PS > $a = 1
PS > "A's value is 1(true or false?): $(if($a -eq 1) { "This is TRUE!" } else { "This is FALSE!" })"
A's value is 1(true or false?): This is TRUE!
PS > $a = 2
PS > "A's value is 1(true or false?): $(if($a -eq 1) { "This is TRUE!" } else { "This is FALSE!" })"
A's value is 1(true or false?): This is FALSE!
当您编写${{.Name}
时,实际上是在请求名为{.Name
的变量,而不是${/code>变量的Name
属性
PS > ${_.Name} = "test"
PS > Get-Variable _*
Name Value
---- -----
_.Name test
之所以$($.Name)
起作用,是因为$()
的意思是“先处理这个”,所以您可以在里面指定您想要的任何内容。在本例中,您只需指定一个变量名和所需的属性,但也可以使其更复杂,如:
PS > $a = 1
PS > "A's value is 1(true or false?): $(if($a -eq 1) { "This is TRUE!" } else { "This is FALSE!" })"
A's value is 1(true or false?): This is TRUE!
PS > $a = 2
PS > "A's value is 1(true or false?): $(if($a -eq 1) { "This is TRUE!" } else { "This is FALSE!" })"
A's value is 1(true or false?): This is FALSE!