知道为什么$ result.PathName不起作用?
$results = Get-WMIObject -query "select * from Win32_Service where Name LIKE '%sql%'" | Select Name, PathName | Format-List
ForEach ($result in $results) {
Write-Output "Test = " + $result.PathName
}
Write-Output "done"
预期产出:
Test = C:\blah\blah.sqlserver.exe
Test = C:\blah\blah.sqlserver.exe
实际输出
Test =
+
Test =
+
答案 0 :(得分:3)
Format-*
cmdlet生成格式说明作为输出。除了向用户或Out-*
cmdlet显示结果外,此说明对任何内容都没有用。从代码中删除Format-List
。并将Write-Output
后的表达式放在括号中:Write-Output ("Test = " + $result.PathName)
,将其解释为单个表达式,但不是Write-Output
的三个不同参数。或者您可以完全删除Write-Output
,因为它默认隐含。