Powershell 传递包含路径的变量以获取ChildItem不会给出结果

Powershell 传递包含路径的变量以获取ChildItem不会给出结果,powershell,Powershell,我想传递一个path变量来获取ChildItem。但是,path变量不会拾取路径 我尝试了以下方法 [STRING]$global:svcName="RSCDsvc" $bsaPath=(Get-WmiObject -query "select Pathname from win32_service where name='$svcName'").PathName write-output $bsaPath 上面给出了产品的安装路径 “C:\Program Files\BMC Software

我想传递一个path变量来获取ChildItem。但是,path变量不会拾取路径

我尝试了以下方法

[STRING]$global:svcName="RSCDsvc"
$bsaPath=(Get-WmiObject -query "select Pathname from win32_service where name='$svcName'").PathName
write-output $bsaPath
上面给出了产品的安装路径

“C:\Program Files\BMC Software\BladeLogic\RSCD\RSCDsvc.exe”

我使用相同的变量$bsaPath来获取产品的版本,但它没有给我输出

PS> [STRING]$global:svcName="RSCDsvc"
PS> $bsaPath=(Get-WmiObject -query "select Pathname from win32_service where name='$svcName'").PathName
PS> write-output $bsaPath
"C:\Program Files\BMC Software\BladeLogic\RSCD\RSCDsvc.exe"
PS> $installedVersion=((Get-ChildItem -path $bsaPath -ErrorAction SilentlyContinue).VersionInfo).ProductVersion
PS> write-output $installedVersion
PS>
但是,我尝试下面的方法

PS> $installedVersion=((Get-ChildItem -path "C:\Program Files\BMC Software\BladeLogic\RSCD\RSCDsvc.exe" -ErrorAction SilentlyContinue).VersionInfo).ProductVersion
PS> write-output $installedVersion
8.9.01.68
PS>

如何通过将路径作为变量传递来获取版本?

您的查询似乎是
$bsaPath=(get WmiObject-query“从win32_服务中选择路径名,其中name='$svcName')。路径名
返回用双引号括起来的路径

您需要将其修剪掉:

$global:svcName="RSCDsvc"
$bsaPath=(Get-WmiObject -query "select Pathname from win32_service where name='$svcName'").PathName.Trim('"')
$bsaPath

$installedVersion=((Get-ChildItem -path $bsaPath -ErrorAction SilentlyContinue).VersionInfo).ProductVersion
$installedVersion
结果:

C:\Program Files\BMC Software\BladeLogic\RSCD\RSCDsvc.exe
8.9.01.68