Powershell – Deploying environment variables to multiple remote computers

This script uses setx to deploy the variable, then it’ll ask you to reboot the machines.

It uses a variable from the local machine, and will simply mirror it to all other workstations.

# Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
   {
   # We are running "as Administrator" - so change the title and background color to indicate this
   $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
   $Host.UI.RawUI.BackgroundColor = "DarkBlue"
   clear-host
   }
else
   {
   # We are not running "as Administrator" - so relaunch as administrator
   # Create a new process object that starts PowerShell
    $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
   # Specify the current script path and name as a parameter
   $newProcess.Arguments = $myInvocation.MyCommand.Definition;
   # Indicate that the process should be elevated
   $newProcess.Verb = "runas";
   # Start the new process
   [System.Diagnostics.Process]::Start($newProcess);
   # Exit from the current, unelevated, process
   exit
   }

write-host ""
write-host ""
write-host "##########################################################################" -ForegroundColor Green
write-host "#                                                                        #" -ForegroundColor Green
write-host "#          `"WORKINGPATH`" VARIABLE MUST BE ALREADY SET LOCALLY.           #" -ForegroundColor Green
write-host "#       REMOTE MACHINES WILL BE SET TO THE LOCAL MACHINES SETTING.       #" -ForegroundColor Green
write-host "#                                                                        #" -ForegroundColor Green
#--- BEGIN SINGLE LINE
write-host "#" -ForegroundColor Green -NoNewline
write-host "      PLEASE REBOOT THE REMOTE MACHINES AFTER SETTING THE VARIABLE      " -ForegroundColor Magenta -NoNewline
write-host "#" -ForegroundColor Green
#--- END SINGLE LINE
write-host "#                                                                        #" -ForegroundColor Green
write-host "##########################################################################" -ForegroundColor Green
write-host ""


#--- VARIABLES
    #--- TXT FILE CONTAINING A LIST OF WORKSTATION IPs (hostnames not recommmended due to DNS issues)
    $workstations = "$env:WORKINGPATH\setup\config_files\workstations.txt"

#--- TEST ALL WORKSTATIONS
(Get-Content $workstations) -notmatch '^#' | foreach {
    if (test-Connection -Count 1 -Cn $_ -quiet) {
        Write-Host "$_ is online" -ForegroundColor Green
    } else {
        Write-Host "$_ is not online" -ForegroundColor Red
		   }
		   }


#--- Pause Script 3 2 1 GO!
$x = 3
$length = $x / 100
$MinText = "minutes"
Write-Host ""
while($x -gt 0) {
$min = [int](([string]($x/60)).split('.')[0])
if ($min -eq "1"){$MinText = "minute"}
if ($min -eq "0") {
$min = $null
$MinText = "less than a minute"
$Beep = [system.console]::Beep(2000,150)
}

$text = $min + ($x % 60)
#Write-Progress "Pausing Script" -status $text -perc ($x/$length)
Write-Host "$text..." -NoNewline
$beep
start-sleep -s 1
$x--
}
Write-Host "GO!!" -ForegroundColor Green
$Beep = [system.console]::Beep(3300,500)




#--- GET LIST AND TEST CONNECTION AND SET VAR
(Get-Content $workstations) -notmatch '^#' | foreach {
    if (test-Connection -Count 1 -Cn $_ -quiet) {

        #--- SET VARIABLE
        Write-Host ""
        Write-Host "Setting Environmental Variable for $_" -ForegroundColor Green
        SetX /S "$_" "WORKINGPATH" "$env:WORKINGPATH" /M

    } else {
        "$_ is not online"
		   }
		   }

write-host ""

#--- RESTART MACHINES
$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("Do you want to RESTART ALL MACHINES??",0,"RESTART ALL MACHINES???",4)
If ($intAnswer -eq 6) {
    $a = new-object -comobject wscript.shell
    $intAnswer = $a.popup("Are you sure you want to RESTART ALL MACHINES??",0,"NO GOING BACK!!!",4)
    If ($intAnswer -eq 6) {
            write-host ""
            write-host "######################################" -ForegroundColor Green
            write-host "#         Starting reboots...        #" -ForegroundColor Green
            write-host "######################################" -ForegroundColor Green
            write-host ""

            (Get-Content $workstations) -notmatch '^#' | foreach {
            if (test-Connection -Count 1 -Cn $_ -quiet) {

                #--- RESTART 'EM
                Write-Host "Attempting to restart $_" -ForegroundColor Green
                Restart-Computer -ComputerName $_ -Force -AsJob

            } else {
                "$_ is not online"
		            }
		            }
                    
    } else {
    
    }
} else {
    
}
		           

#--- ALL DONE POPUP
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Finished setting environmental variables on clients!!",0,"All Done!",0x0)

Leave a Reply

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