Created
June 25, 2024 22:38
-
-
Save davidlu1001/5a2e8df75d9d9a629b31a4f3321fd3b5 to your computer and use it in GitHub Desktop.
Set Reg Values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Examples | |
# | |
# Run locally with dry-run mode | |
#.\setRegValues.ps1 -csvFilePath ".\DNSPatternMatches.csv" -dryRun -Verbose | |
# | |
# Run locally | |
#.\setRegValues.ps1 -csvFilePath ".\DNSPatternMatches.csv" | |
# | |
# Run remotely | |
#.\setRegValues.ps1 -csvFilePath ".\DNSPatternMatches.csv" -computerNames "RemoteComputer1", "RemoteComputer2" | |
# Example for DNSPatternMatches (generated by getRegValues.ps1 and add new column "newValue") | |
# | |
#"ComputerName","Path","Name","Value","newValue" | |
#"local","Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Google\Chrome\Extensions\cifnddnffldieaamihfkhkdgnbhfmaci","update_url","https://clients2.google.com/service/update2/crx","https://clients2.google.com/TEST" | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory=$true)] | |
[string]$csvFilePath, | |
[Parameter(Mandatory=$false)] | |
[switch]$dryRun, | |
[Parameter(Mandatory=$false)] | |
[string[]]$computerNames = @("localhost") | |
) | |
function Set-RegistryValueLocal { | |
[CmdletBinding()] | |
param ( | |
[string]$path, | |
[string]$name, | |
[string]$newValue, | |
[switch]$dryRun | |
) | |
try { | |
$regPath = $path -replace '^HKEY_LOCAL_MACHINE\\', 'HKLM:\' | |
if ($dryRun) { | |
Write-Verbose "[DRY RUN] Would set registry value: $regPath\$name to $newValue" | |
} else { | |
if (!(Test-Path $regPath)) { | |
New-Item -Path $regPath -Force | Out-Null | |
Write-Verbose "Created new registry key: $regPath" | |
} | |
Set-ItemProperty -Path $regPath -Name $name -Value $newValue | |
Write-Verbose "Set registry value: $regPath\$name to $newValue" | |
} | |
} | |
catch { | |
Write-Error "Error setting registry value $path\$name`: $_" | |
} | |
} | |
function Set-RegistryValueRemote { | |
[CmdletBinding()] | |
param ( | |
[string]$computerName, | |
[string]$path, | |
[string]$name, | |
[string]$newValue, | |
[switch]$dryRun | |
) | |
try { | |
$scriptBlock = { | |
param($path, $name, $newValue, $dryRun) | |
Set-RegistryValueLocal -path $path -name $name -newValue $newValue -dryRun:$dryRun -Verbose:$VerbosePreference | |
} | |
Invoke-Command -ComputerName $computerName -ScriptBlock $scriptBlock -ArgumentList $path, $name, $newValue, $dryRun | |
} | |
catch { | |
Write-Error "Error setting registry value on $computerName`: $_" | |
} | |
} | |
# Validate CSV file | |
if (!(Test-Path $csvFilePath)) { | |
throw "CSV file not found: $csvFilePath" | |
} | |
$csvData = Import-Csv -Path $csvFilePath | |
if (@($csvData | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name) -notcontains 'newValue') { | |
throw "CSV file does not contain 'newValue' column" | |
} | |
$totalOperations = ($computerNames.Count * ($csvData | Measure-Object).Count) | |
$currentOperation = 0 | |
foreach ($computerName in $computerNames) { | |
Write-Host "Processing computer: $computerName" | |
foreach ($row in $csvData) { | |
$currentOperation++ | |
# Check if required fields are present and not empty | |
if ([string]::IsNullOrWhiteSpace($row.Path) -or [string]::IsNullOrWhiteSpace($row.Name) -or [string]::IsNullOrWhiteSpace($row.newValue)) { | |
Write-Warning "Skipping row due to missing data: Path=$($row.Path), Name=$($row.Name), newValue=$($row.newValue)" | |
continue | |
} | |
$progressParams = @{ | |
Activity = "Setting registry values" | |
Status = "Processing $computerName : $($row.Path)\$($row.Name)" | |
PercentComplete = ($currentOperation / $totalOperations) * 100 | |
} | |
Write-Progress @progressParams | |
if ($computerName -eq "localhost" -or $computerName -eq "127.0.0.1" -or $computerName -eq $env:COMPUTERNAME) { | |
Set-RegistryValueLocal -path $row.Path -name $row.Name -newValue $row.newValue -dryRun:$dryRun -Verbose:$VerbosePreference | |
} | |
else { | |
Set-RegistryValueRemote -computerName $computerName -path $row.Path -name $row.Name -newValue $row.newValue -dryRun:$dryRun -Verbose:$VerbosePreference | |
} | |
} | |
} | |
Write-Progress -Activity "Setting registry values" -Completed | |
Write-Host "Registry value setting process completed." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment