Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String PowerShell:What';“有什么不对吗?”${uu.Name}…“;?_String_Powershell_Concatenation - Fatal编程技术网

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!