Search code examples
stringpowershellrelease-management

Forming folder/file path using part of the PathName of a windows service - Any better approach?


My requirement is to form a folder path by using part of the PathName of a windows service.

For example, "Microsoft Deployment Agent" windows service's PathName is "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Release Management\bin\DeploymentAgent.exe"

There is another exe in "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Release Management\Client\bin\ReleaseManagementBuild.exe".

So I need to form this path to ReleaseManagementBuild.exe. So I can execute it to trigger a release in Microsoft Release Management. This is needed because the PathName of the service is not the same in all servers. One might be in C drive, another might be in another drive.

I have explored two methods so far, using Join-Path or [IO.Path]::Combine.

"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

$RMBuildExePath = Get-WmiObject win32_service | ?{$_.Name -like "*VSO*"} | select PathName

[string]$RMBuildExePathString = $RMBuildExePath.PathName

[string[]]$split = $RMBuildExePathString.Split("\")



$WorkFolder = [IO.Path]::Combine($split[0].Replace("`"","") + "\", $split[1], $split[2], '_work')

Test-Path $WorkFolder



$work = Join-Path $split[0].Replace("`"","") $split[1] | Join-Path -ChildPath $split[2] | Join-Path -ChildPath '_work'

Test-Path $work 

"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

Can there be a better approach to form the file path? May be registry key? Or environment variable?


Solution

  • $RMBuildExePath = Get-WmiObject win32_service | ?{$_.Name -like "VSO"} | select -ExpandProperty PathName

    $RMBuildExePath -match '^"[A-Z]:\[^\]\[^\]'

    $work = $matches[0] + "_work" -replace '"', ""

    $work