mirror of
https://github.com/actions/runner-images.git
synced 2025-12-15 06:08:07 +00:00
PathHelpers: Dealing with hardcoded registry paths and optimizing code (#4125)
This commit is contained in:
@@ -1,68 +1,48 @@
|
|||||||
function Test-MachinePath{
|
function Get-SystemVariable {
|
||||||
[CmdletBinding()]
|
param(
|
||||||
|
[string]$SystemVariable
|
||||||
|
)
|
||||||
|
|
||||||
|
[System.Environment]::GetEnvironmentVariable($SystemVariable, "Machine")
|
||||||
|
}
|
||||||
|
|
||||||
|
function Set-SystemVariable {
|
||||||
|
param(
|
||||||
|
[string]$SystemVariable,
|
||||||
|
[string]$Value
|
||||||
|
)
|
||||||
|
|
||||||
|
[System.Environment]::SetEnvironmentVariable($SystemVariable, $Value, "Machine")
|
||||||
|
Get-SystemVariable $SystemVariable
|
||||||
|
}
|
||||||
|
|
||||||
|
function Get-MachinePath {
|
||||||
|
Get-SystemVariable PATH
|
||||||
|
}
|
||||||
|
|
||||||
|
function Set-MachinePath {
|
||||||
|
param(
|
||||||
|
[string]$NewPath
|
||||||
|
)
|
||||||
|
|
||||||
|
Set-SystemVariable PATH $NewPath
|
||||||
|
}
|
||||||
|
|
||||||
|
function Test-MachinePath {
|
||||||
param(
|
param(
|
||||||
[string]$PathItem
|
[string]$PathItem
|
||||||
)
|
)
|
||||||
|
|
||||||
$currentPath = Get-MachinePath
|
$pathItems = (Get-MachinePath).Split(';')
|
||||||
|
$pathItems.Contains($PathItem)
|
||||||
$pathItems = $currentPath.Split(';')
|
|
||||||
|
|
||||||
if($pathItems.Contains($PathItem))
|
|
||||||
{
|
|
||||||
return $true
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return $false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function Set-MachinePath{
|
function Add-MachinePathItem {
|
||||||
[CmdletBinding()]
|
|
||||||
param(
|
|
||||||
[string]$NewPath
|
|
||||||
)
|
|
||||||
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name Path -Value $NewPath
|
|
||||||
return $NewPath
|
|
||||||
}
|
|
||||||
|
|
||||||
function Add-MachinePathItem
|
|
||||||
{
|
|
||||||
[CmdletBinding()]
|
|
||||||
param(
|
param(
|
||||||
[string]$PathItem
|
[string]$PathItem
|
||||||
)
|
)
|
||||||
|
|
||||||
$currentPath = Get-MachinePath
|
$currentPath = Get-MachinePath
|
||||||
$newPath = $PathItem + ';' + $currentPath
|
$newPath = $PathItem + ';' + $currentPath
|
||||||
return Set-MachinePath -NewPath $newPath
|
Set-MachinePath -NewPath $newPath
|
||||||
}
|
|
||||||
|
|
||||||
function Get-MachinePath{
|
|
||||||
[CmdletBinding()]
|
|
||||||
param(
|
|
||||||
|
|
||||||
)
|
|
||||||
$currentPath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path
|
|
||||||
return $currentPath
|
|
||||||
}
|
|
||||||
|
|
||||||
function Get-SystemVariable{
|
|
||||||
[CmdletBinding()]
|
|
||||||
param(
|
|
||||||
[string]$SystemVariable
|
|
||||||
)
|
|
||||||
$currentPath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name $SystemVariable).$SystemVariable
|
|
||||||
return $currentPath
|
|
||||||
}
|
|
||||||
|
|
||||||
function Set-SystemVariable{
|
|
||||||
[CmdletBinding()]
|
|
||||||
param(
|
|
||||||
[string]$SystemVariable,
|
|
||||||
[string]$Value
|
|
||||||
)
|
|
||||||
Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name $SystemVariable -Value $Value
|
|
||||||
return $Value
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user