PowerShell Script to Remotely Update Registry keys and Restart Services on multiple Computers with Progress bar showing the Status of the Script

Requirement:

In an enterprise automation its very often a requirement to REMOTELY

  • Query list of servers/computers/desktops
  • Apply a registry change to the servers/computers/desktops
  • Restart a Service on the servers/computers/desktops

Solution:

Implementing a PowerShell script for such an automation requirement helps you easily build enterprise capabilities to the script like:

  • Showing a Progress bar indicating current activity of the script and how much percentage the processing is completed
  • Implement safety check to avoid attempts connect to an inaccessible or offline servers/computers/desktops
  • Display output to standard output channel

Examples:

PowerShell in PowerGUI:

image

Code:

$Servers            = Get-Content "C:\Temp\Servers.txt"
$TotalServersCount     = ($Servers).Count 
$PerCentageCounter     = 1

Foreach ($Server IN $Servers) {    
    $PerCentCompleted = [System.Math]::Round(($PerCentageCounter/$TotalServersCount*100),0)
    
    Write-Progress -Activity "Processing Server [$PerCentageCounter] [$Server]" -CurrentOperation "Checking Connectivity to the Server" 
    # Check Ping Connection to the Server 
    if (! (Test-Connection -ComputerName $Server -Count 2 -ErrorAction SilentlyContinue -Quiet)) {
        Write-Warning "Ping check failed for $Server, Skipping Server"
        Write-Progress -Activity "Ping check failed for $Server, Skipping Server" -Status "$PerCentCompleted% Complete:" -PercentComplete $PerCentCompleted
        Start-Sleep -Seconds 5
        $PerCentageCounter++
        Continue  
    }
    
    Write-Progress -Activity "Processing $Server Server" -CurrentOperation "Updating Registry Value" 
    # Enable Remote Desktop Connection to the Server 
    REG ADD "\\$Server\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f    
    Start-Sleep -Seconds 2
    
    Write-Progress -Activity "Processing $Server Server" -CurrentOperation "Restarting [Windows Update] Service" 
    # Restart Windows Updates Service on the Remote Service
    Restart-Service -InputObject $(Get-Service -Computer $Server -Name wuauserv)
    Start-Sleep -Seconds 2
    
    # Output the Status to Standard Output as well as Show Progressbar while processing the Servers.
    Write-Output "Restarted [Windows Update] Service on [$Server] Server"
    Write-Progress -Activity "Completed Processing $Server" -Status "$PerCentCompleted% Complete:" -PercentComplete $PerCentCompleted
    Start-Sleep -Seconds 5
    $PerCentageCounter++
}

 

References:

Script File:

Leave a Reply

Your email address will not be published. Required fields are marked *