使用Windows服务的PathName的一部分形成文件夹/文件路径 - 任何更好的方法?

时间:2016-10-25 17:00:27

标签: string powershell release-management

我的要求是使用Windows服务的PathName的一部分来形成文件夹路径。

例如,“Microsoft Deployment Agent”窗口服务的PathName是“C:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ Release Management \ bin \ DeploymentAgent.exe”

“C:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ Release Management \ Client \ bin \ ReleaseManagementBuild.exe”中还有另一个exe。

所以我需要形成ReleaseManagementBuild.exe的这条路径。所以我可以执行它来触发Microsoft Release Management中的发布。这是必需的,因为服务的PathName在所有服务器中都不相同。一个可能在C盘中,另一个可能在另一个驱动器中。

到目前为止,我已经使用Join-Path或[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 

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

有没有更好的方法来形成文件路径?可能是注册表项?还是环境变量?

1 个答案:

答案 0 :(得分:1)

$ RMBuildExePath = Get-WmiObject win32_service | ?{$ _。类似名称" VSO "} |选择-ExpandProperty PathName

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

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

$工作