How to gpudate and RDP with PowerShell

In my first PowerShell post, I described running into the issue of maintaining a network where you do not have all admin permissions. A second issue that I ran into was how to gpupdate and RDP with PowerShell.  Updates were pushed out from group policy but machines were not pulling them and staying up to date. How I forced gpupdate was a lot like how I forced remote reboots with PowerShell.

for($i=0; $i -lt $WorkstationArray.Count; $i++){
     $temp = $WorkstationArray[$i]
     Write-Output "Initiate gpupdate for: " $temp
     Invoke-GPUpdate -Computer $temp
}

Loop through an array and RDP into machines

While looping through an array of the workstations it was possible that some of them would be disconnected from the network. I added code to allow me to log any workstation that I could not RDP into. I also added code to save my username and password, but you could make that more secure and not save that in your script.

$LogFile = "C:\workstation_unreachables.txt"
$User="XXXXXXXXXXXXXXX"
$Password="XXXXXXXXXXXXXXX"

Function Logfile{
    Param ([string]$logstring)
    Add-content $LogFile -value $logstring
}

for($i=0; $i -lt $ServerArray.Count; $i++){ 
    cmdkey /generic:"$Templogin" /U:$User /pass:$Password
    $temp = $ServerArray[$i] mstsc /v:"$temp" /admin 
    $log = Read-Host -Prompt "Press y to log this computer name
                   Press any other key to continue"
    if ($log -match "y"){
        LogFile "$temp"
    }
}

Run PowerShell as admin

The last item I had to do was run the PowerShell script as an administrator.  I decided to make a batch script for that. This prompted me for admin credentials whenever it was ran.

@ECHO OFF 

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\gpupdate.ps1""' -Verb RunAs}"

:END
pause

I hope this helps with running gpupdate and RDP with PowerShell on your network. If you missed my first post you can read more here. Thank you – Powersjo

Most of the help I found online was located here at StackOverflow.

Enter your address to subscribe to this blog and receive notifications of new posts!

Published by Powersjo

If you want to contact me I can be found here: https://gab.com/Powersjo Christ is King