Created
July 2, 2024 03:50
-
-
Save davidlu1001/5914a6b403ddae076065231947f33c1d 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
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory=$true)] | |
[string]$regPath, | |
[Parameter(Mandatory=$true)] | |
[string]$regName, | |
[Parameter(Mandatory=$true)] | |
[string]$regNewValue, | |
[Parameter(Mandatory=$false)] | |
[string[]]$computerNames = @("localhost"), | |
[Parameter(Mandatory=$false)] | |
[switch]$DryRun, | |
[Parameter(Mandatory=$false)] | |
[string]$outputFile = "RegUpdateResults.csv" | |
) | |
# Import required module | |
Import-Module Microsoft.PowerShell.Management | |
# Function to write log messages | |
function Write-Log { | |
param ( | |
[string]$Message, | |
[ValidateSet("INFO", "WARNING", "ERROR")] | |
[string]$Level = "INFO" | |
) | |
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" | |
$logMessage = "[$timestamp] [$Level] $Message" | |
Write-Verbose $logMessage | |
if ($Level -eq "ERROR") { | |
Write-Error $logMessage | |
} | |
} | |
# Function to update registry on local machine | |
function Update-LocalRegistryValue { | |
[CmdletBinding()] | |
param ( | |
[string]$Path, | |
[string]$Name, | |
[string]$NewValue, | |
[switch]$DryRun | |
) | |
try { | |
$regKey = Get-Item -Path $Path -ErrorAction Stop | |
$oldValue = $regKey.GetValue($Name) | |
if ($DryRun) { | |
Write-Log "DRY RUN: Would update '$Name' in '$Path' on localhost from '$oldValue' to '$NewValue'" | |
} else { | |
Set-ItemProperty -Path $Path -Name $Name -Value $NewValue -Force | |
Write-Log "Updated '$Name' in '$Path' on localhost from '$oldValue' to '$NewValue'" | |
} | |
return [PSCustomObject]@{ | |
ComputerName = $env:COMPUTERNAME | |
Path = $Path | |
Name = $Name | |
OldValue = $oldValue | |
NewValue = $NewValue | |
Status = if ($DryRun) { "Dry Run" } else { "Updated" } | |
} | |
} | |
catch { | |
Write-Log "Failed to update registry on localhost. Error: $_" -Level ERROR | |
return [PSCustomObject]@{ | |
ComputerName = $env:COMPUTERNAME | |
Path = $Path | |
Name = $Name | |
OldValue = "N/A" | |
NewValue = $NewValue | |
Status = "Failed" | |
} | |
} | |
} | |
# Function to update registry on remote machine | |
function Update-RemoteRegistryValue { | |
[CmdletBinding()] | |
param ( | |
[string]$ComputerName, | |
[string]$Path, | |
[string]$Name, | |
[string]$NewValue, | |
[switch]$DryRun | |
) | |
try { | |
$result = Invoke-Command -ComputerName $ComputerName -ScriptBlock { | |
param($Path, $Name, $NewValue, $DryRun) | |
try { | |
$regKey = Get-Item -Path $Path -ErrorAction Stop | |
$oldValue = $regKey.GetValue($Name) | |
if (-not $DryRun) { | |
Set-ItemProperty -Path $Path -Name $Name -Value $NewValue -Force | |
} | |
[PSCustomObject]@{ | |
ComputerName = $env:COMPUTERNAME | |
Path = $Path | |
Name = $Name | |
OldValue = $oldValue | |
NewValue = $NewValue | |
Status = if ($DryRun) { "Dry Run" } else { "Updated" } | |
} | |
} | |
catch { | |
[PSCustomObject]@{ | |
ComputerName = $env:COMPUTERNAME | |
Path = $Path | |
Name = $Name | |
OldValue = "N/A" | |
NewValue = $NewValue | |
Status = "Failed" | |
} | |
} | |
} -ArgumentList $Path, $Name, $NewValue, $DryRun | |
if ($result.Status -eq "Updated") { | |
Write-Log "Updated '$Name' in '$Path' on '$ComputerName' from '$($result.OldValue)' to '$NewValue'" | |
} elseif ($result.Status -eq "Dry Run") { | |
Write-Log "DRY RUN: Would update '$Name' in '$Path' on '$ComputerName' from '$($result.OldValue)' to '$NewValue'" | |
} else { | |
Write-Log "Failed to update registry on $ComputerName" -Level ERROR | |
} | |
return $result | |
} | |
catch { | |
Write-Log "Failed to connect to $ComputerName. Error: $_" -Level ERROR | |
return [PSCustomObject]@{ | |
ComputerName = $ComputerName | |
Path = $Path | |
Name = $Name | |
OldValue = "N/A" | |
NewValue = $NewValue | |
Status = "Connection Failed" | |
} | |
} | |
} | |
# Main script execution | |
try { | |
Write-Log "Script started. Registry path: $regPath, Name: $regName, New Value: $regNewValue" | |
$results = @() | |
foreach ($computer in $computerNames) { | |
Write-Log "Processing computer: $computer" | |
if ($computer -eq "localhost" -or $computer -eq "127.0.0.1" -or $computer -eq $env:COMPUTERNAME) { | |
$result = Update-LocalRegistryValue -Path $regPath -Name $regName -NewValue $regNewValue -DryRun:$DryRun | |
} else { | |
$result = Update-RemoteRegistryValue -ComputerName $computer -Path $regPath -Name $regName -NewValue $regNewValue -DryRun:$DryRun | |
} | |
$results += $result | |
} | |
# Export results to CSV | |
$results | Select-Object ComputerName, Path, Name, OldValue, NewValue, Status | Export-Csv -Path $outputFile -NoTypeInformation | |
Write-Log "Results exported to $outputFile" | |
# Display results in console | |
$results | Format-Table -AutoSize | |
} | |
catch { | |
Write-Log "An error occurred during script execution: $_" -Level ERROR | |
} | |
finally { | |
Write-Log "Script execution completed." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment