mirror of
https://github.com/actions/runner-images.git
synced 2025-12-15 22:26:56 +00:00
[Windows] Refactor PATH helpers (#8885)
This commit is contained in:
committed by
GitHub
parent
c73276d3f6
commit
bfe32a2b12
@@ -7,19 +7,10 @@ param()
|
||||
. $PSScriptRoot\VisualStudioHelpers.ps1
|
||||
|
||||
Export-ModuleMember -Function @(
|
||||
'Connect-Hive'
|
||||
'Disconnect-Hive'
|
||||
'Test-MachinePath'
|
||||
'Get-MachinePath'
|
||||
'Get-DefaultPath'
|
||||
'Set-MachinePath'
|
||||
'Set-DefaultPath'
|
||||
'Mount-RegistryHive'
|
||||
'Dismount-RegistryHive'
|
||||
'Add-MachinePathItem'
|
||||
'Add-DefaultPathItem'
|
||||
'Add-DefaultItem'
|
||||
'Get-SystemVariable'
|
||||
'Get-DefaultVariable'
|
||||
'Set-DefaultVariable'
|
||||
'Install-Binary'
|
||||
'Install-VisualStudio'
|
||||
'Get-ToolsetContent'
|
||||
@@ -47,8 +38,8 @@ Export-ModuleMember -Function @(
|
||||
'Get-VisualStudioInstance'
|
||||
'Get-VisualStudioComponents'
|
||||
'Get-WindowsUpdateStates'
|
||||
'New-ItemPath'
|
||||
'Use-ChecksumComparison'
|
||||
'Get-HashFromGitHubReleaseBody'
|
||||
'Test-FileSignature'
|
||||
'Update-Environment'
|
||||
)
|
||||
|
||||
@@ -869,3 +869,39 @@ function Test-FileSignature {
|
||||
throw "Signature thumbprint do not match expected."
|
||||
}
|
||||
}
|
||||
|
||||
function Update-Environment {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Updates the environment variables by reading values from the registry.
|
||||
|
||||
.DESCRIPTION
|
||||
This function updates current environment by reading values from the registry.
|
||||
It is useful when you need to update the environment variables without restarting the current session.
|
||||
|
||||
.NOTES
|
||||
The function requires administrative privileges to modify the system registry.
|
||||
#>
|
||||
|
||||
$locations = @(
|
||||
'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
|
||||
'HKCU:\Environment'
|
||||
)
|
||||
|
||||
# Update PATH variable
|
||||
$pathItems = $locations | ForEach-Object {
|
||||
(Get-Item $_).GetValue('PATH').Split(';')
|
||||
} | Select-Object -Unique
|
||||
$Env:PATH = $pathItems -join ';'
|
||||
|
||||
# Update other variables
|
||||
$locations | ForEach-Object {
|
||||
$key = Get-Item $_
|
||||
foreach ($name in $key.GetValueNames()) {
|
||||
$value = $key.GetValue($name)
|
||||
if (-not ($name -ieq 'PATH')) {
|
||||
Set-Item -Path Env:$name -Value $value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,62 @@
|
||||
function Connect-Hive {
|
||||
|
||||
function Mount-RegistryHive {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Mounts a registry hive from a file.
|
||||
|
||||
.DESCRIPTION
|
||||
The Mount-RegistryHive function loads a registry hive from a specified file into a specified subkey.
|
||||
|
||||
.PARAMETER FileName
|
||||
The path to the file from which to load the registry hive.
|
||||
|
||||
.PARAMETER SubKey
|
||||
The registry subkey into which to load the hive.
|
||||
|
||||
.EXAMPLE
|
||||
Mount-RegistryHive -FileName "C:\Path\To\HiveFile.hiv" -SubKey "HKLM\SubKey"
|
||||
#>
|
||||
param(
|
||||
[string]$FileName = "C:\Users\Default\NTUSER.DAT",
|
||||
[string]$SubKey = "HKLM\DEFAULT"
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$FileName,
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$SubKey
|
||||
)
|
||||
|
||||
Write-Host "Loading the file $FileName to the Key $SubKey"
|
||||
if (Test-Path $SubKey.Replace("\",":")) {
|
||||
if (Test-Path $SubKey.Replace("\", ":")) {
|
||||
Write-Warning "The key $SubKey is already loaded"
|
||||
return
|
||||
}
|
||||
|
||||
$result = reg load $SubKey $FileName *>&1
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host "Failed to load hive: $result"
|
||||
Write-Error "Failed to load file $FileName to the key ${SubKey}: $result"
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
function Disconnect-Hive {
|
||||
function Dismount-RegistryHive {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Dismounts a registry hive.
|
||||
|
||||
.DESCRIPTION
|
||||
The Dismount-RegistryHive function unloads a registry hive from a specified subkey.
|
||||
|
||||
.PARAMETER SubKey
|
||||
The registry subkey from which to unload the hive.
|
||||
|
||||
.EXAMPLE
|
||||
Dismount-RegistryHive -SubKey "HKLM\SubKey"
|
||||
#>
|
||||
param(
|
||||
[string]$SubKey = "HKLM\DEFAULT"
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$SubKey
|
||||
)
|
||||
|
||||
Write-Host "Unloading the hive $SubKey"
|
||||
if (-not (Test-Path $SubKey.Replace("\",":"))) {
|
||||
if (-not (Test-Path $SubKey.Replace("\", ":"))) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -33,123 +67,72 @@ function Disconnect-Hive {
|
||||
}
|
||||
}
|
||||
|
||||
function Get-SystemVariable {
|
||||
param(
|
||||
[string]$SystemVariable
|
||||
)
|
||||
|
||||
[System.Environment]::GetEnvironmentVariable($SystemVariable, "Machine")
|
||||
}
|
||||
|
||||
function Get-DefaultVariable {
|
||||
param(
|
||||
[string]$DefaultVariable,
|
||||
[string]$Name = "DEFAULT\Environment",
|
||||
[bool]$Writable = $false
|
||||
)
|
||||
|
||||
$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($Name, $Writable)
|
||||
$key.GetValue($DefaultVariable, "", "DoNotExpandEnvironmentNames")
|
||||
$key.Handle.Close()
|
||||
[System.GC]::Collect()
|
||||
}
|
||||
|
||||
function Set-DefaultVariable {
|
||||
param(
|
||||
[string]$DefaultVariable,
|
||||
[string]$Value,
|
||||
[string]$Name = "DEFAULT\Environment",
|
||||
[string]$Kind = "ExpandString",
|
||||
[bool]$Writable = $true
|
||||
)
|
||||
|
||||
$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($Name, $Writable)
|
||||
$key.SetValue($DefaultVariable, $Value, $Kind)
|
||||
Get-DefaultVariable -DefaultVariable $DefaultVariable -Name $Name
|
||||
$key.Handle.Close()
|
||||
[System.GC]::Collect()
|
||||
}
|
||||
|
||||
function Get-MachinePath {
|
||||
Get-SystemVariable PATH
|
||||
}
|
||||
|
||||
function Get-DefaultPath {
|
||||
Get-DefaultVariable Path
|
||||
}
|
||||
|
||||
function Set-MachinePath {
|
||||
param(
|
||||
[string]$NewPath
|
||||
)
|
||||
|
||||
[System.Environment]::SetEnvironmentVariable("PATH", $NewPath, "Machine")
|
||||
}
|
||||
|
||||
function Set-DefaultPath {
|
||||
param(
|
||||
[string]$NewPath
|
||||
)
|
||||
|
||||
Set-DefaultVariable PATH $NewPath
|
||||
}
|
||||
|
||||
function Test-MachinePath {
|
||||
param(
|
||||
[string]$PathItem
|
||||
)
|
||||
|
||||
$pathItems = (Get-MachinePath).Split(';')
|
||||
$pathItems.Contains($PathItem)
|
||||
}
|
||||
|
||||
function Add-MachinePathItem {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Adds a new item to the machine-level PATH environment variable.
|
||||
|
||||
.DESCRIPTION
|
||||
The Add-MachinePathItem function adds a new item to the machine-level PATH environment variable.
|
||||
It takes a string parameter, $PathItem, which represents the new item to be added to the PATH.
|
||||
|
||||
.PARAMETER PathItem
|
||||
Specifies the new item to be added to the machine-level PATH environment variable.
|
||||
|
||||
.EXAMPLE
|
||||
Add-MachinePathItem -PathItem "C:\Program Files\MyApp"
|
||||
|
||||
This example adds "C:\Program Files\MyApp" to the machine-level PATH environment variable.
|
||||
#>
|
||||
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$PathItem
|
||||
)
|
||||
|
||||
$currentPath = Get-MachinePath
|
||||
$currentPath = [System.Environment]::GetEnvironmentVariable("PATH", "Machine")
|
||||
$newPath = $PathItem + ';' + $currentPath
|
||||
Set-MachinePath -NewPath $newPath
|
||||
[System.Environment]::SetEnvironmentVariable("PATH", $newPath, "Machine")
|
||||
}
|
||||
|
||||
function Add-DefaultPathItem {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Adds a path item to the default user profile path.
|
||||
|
||||
.DESCRIPTION
|
||||
This function adds a specified path item to the default user profile path.
|
||||
It mounts the NTUSER.DAT file of the default user to the registry,
|
||||
retrieves the current value of the "Path" environment variable,
|
||||
appends the new path item to it, and updates the registry with the modified value.
|
||||
|
||||
.PARAMETER PathItem
|
||||
The path item to be added to the default user profile path.
|
||||
|
||||
.EXAMPLE
|
||||
Add-DefaultPathItem -PathItem "C:\Program Files\MyApp"
|
||||
|
||||
This example adds "C:\Program Files\MyApp" to the default user profile path.
|
||||
|
||||
.NOTES
|
||||
This function requires administrative privileges to modify the Windows registry.
|
||||
#>
|
||||
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$PathItem
|
||||
)
|
||||
|
||||
Connect-Hive
|
||||
$currentPath = Get-DefaultPath
|
||||
$newPath = $PathItem + ';' + $currentPath
|
||||
Set-DefaultPath -NewPath $newPath
|
||||
Disconnect-Hive
|
||||
}
|
||||
|
||||
function Add-DefaultItem {
|
||||
param(
|
||||
[string]$DefaultVariable,
|
||||
[string]$Value,
|
||||
[string]$Name = "DEFAULT\Environment",
|
||||
[string]$Kind = "ExpandString",
|
||||
[bool]$Writable = $true
|
||||
)
|
||||
|
||||
Connect-Hive
|
||||
$regPath = Join-Path "HKLM:\" $Name
|
||||
if (-not (Test-Path $Name)) {
|
||||
Write-Host "Creating $regPath key"
|
||||
New-Item -Path $regPath -Force | Out-Null
|
||||
}
|
||||
Set-DefaultVariable -DefaultVariable $DefaultVariable -Value $Value -Name $Name -Kind $Kind -Writable $Writable
|
||||
Disconnect-Hive
|
||||
}
|
||||
|
||||
function New-ItemPath {
|
||||
param (
|
||||
[string]$Path
|
||||
)
|
||||
|
||||
if (-not (Test-Path $Path)) {
|
||||
New-Item -Path $Path -Force -ErrorAction Ignore | Out-Null
|
||||
}
|
||||
Mount-RegistryHive `
|
||||
-FileName "C:\Users\Default\NTUSER.DAT" `
|
||||
-SubKey "HKLM\DEFAULT"
|
||||
|
||||
$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("DEFAULT\Environment", $true)
|
||||
$currentValue = $key.GetValue("Path", "", "DoNotExpandEnvironmentNames")
|
||||
$updatedValue = $PathItem + ';' + $currentValue
|
||||
$key.SetValue("Path", $updatedValue, "ExpandString")
|
||||
$key.Handle.Close()
|
||||
[System.GC]::Collect()
|
||||
|
||||
Dismount-RegistryHive "HKLM\DEFAULT"
|
||||
}
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
. $PSScriptRoot\..\PathHelpers.ps1
|
||||
|
||||
Describe 'Test-MachinePath Tests' {
|
||||
Mock Get-MachinePath {return "C:\foo;C:\bar"}
|
||||
It 'Path contains item' {
|
||||
Test-MachinePath -PathItem "C:\foo" | Should Be $true
|
||||
}
|
||||
It 'Path does not containe item' {
|
||||
Test-MachinePath -PathItem "C:\baz" | Should Be $false
|
||||
}
|
||||
}
|
||||
|
||||
Describe 'Set-MachinePath Tests' {
|
||||
Mock Get-MachinePath {return "C:\foo;C:\bar"}
|
||||
Mock Set-ItemProperty {return}
|
||||
It 'Set-MachinePath should return new path' {
|
||||
Set-MachinePath -NewPath "C:\baz" | Should Be "C:\baz"
|
||||
}
|
||||
}
|
||||
|
||||
Describe "Add-MachinePathItem Tests"{
|
||||
Mock Get-MachinePath {return "C:\foo;C:\bar"}
|
||||
Mock Set-ItemProperty {return}
|
||||
It 'Add-MachinePathItem should return complete path' {
|
||||
Add-MachinePathItem -PathItem 'C:\baz' | Should Be 'C:\baz;C:\foo;C:\bar'
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user