Linux Teche
Linux Teche

Reputation: 77

Passing a variable which contains path to Get-ChildItem does not give result

I want to pass a path variable to Get-ChildItem. But, the path variable does not pick the path

I tried the following

[STRING]$global:svcName="RSCDsvc"
$bsaPath=(Get-WmiObject -query "select Pathname from win32_service where name='$svcName'").PathName
write-output $bsaPath

The above throws me the path where the product is installed

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

I use the same variable $bsaPath to get the version of the product it does not give me the output

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>

But, I try the below

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>

How to get the version by passing the path as a variable?

Upvotes: 1

Views: 181

Answers (1)

Theo
Theo

Reputation: 61028

It seems your query $bsaPath=(Get-WmiObject -query "select Pathname from win32_service where name='$svcName'").PathName returns the path enclosed in double-quotes.

You need to trim those off:

$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

result:

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

Upvotes: 3

Related Questions