传递包含Get-ChildItem路径的变量不会产生结果

时间:2019-04-13 09:05:12

标签: powershell

我想将路径变量传递给Get-ChildItem。但是,路径变量不能选择路径

我尝试了以下

[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>

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

1 个答案:

答案 0 :(得分:3)

您的查询$bsaPath=(Get-WmiObject -query "select Pathname from win32_service where name='$svcName'").PathName似乎返回用双引号引起来的路径。

您需要修剪掉它们:

$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