/build/static/layout/Breadcrumb_cap_w.png

Remote Management of Software on Network Computers

Hi all,
I work in a College environment with a large number of computers in classrooms. I always thought it would be handy to have a script to remotely gpupdate a specific room of computers after linking up new GPO software to a room. I've managed to write the first script below which will work in a .bat file. You'll also need the psexec.exe file from https://technet.microsoft.com/en-us/sysinternals/pxexec.aspx. It may not be smartly or cleanly written but it works for me and I'm happy with it. Feel free to make modification as you see fit. I have it saved on my network drive so that's why it points to M:\Programs\Program. I enter the first part of our room name which is typically SiteName-RoomNumber and then the number of computers in the room. I have an extra '-' hard coded in. It then run a gpupdate against each pc in the room with a second pause between each.

The second script at the bottom is a powershell so will need saved as .ps1 and it can remotely delete GPO's based on partial entry of the GPO name. Be aware it deletes any software GPO that matches the search phrase, so it may delete more than you expect. Hands up to the fact that it is probably not that well written but it works for me and I'm happy that it works for me.

The powershell code I've copied from the internet but have managed to work it to what I wanted it to do, so it's not solely my own doing. Any comments welcome and again, free free to ignore, edit, rewrite, etc.

*******************************
Batch File Below
*******************************
@echo off
"M:\Programs\Program\PsExec.exe" /accepteula
:Start
c:
cls
Echo GPUPDATE or Reboot Computers
echo.
Echo 1. GPUPDATE Computers
Echo 2. Reboot Computers
Echo 3. Log Off Computers
echo 4. Shutdown Computers
echo 5. Exit
echo.
CHOICE /C 123456 /M "Enter your choice: "

:: Note - list ERRORLEVELS in decreasing order
IF ERRORLEVEL 5 GOTO End
IF ERRORLEVEL 4 GOTO SRoom
IF ERRORLEVEL 3 GOTO LRoom
IF ERRORLEVEL 2 GOTO RRoom
IF ERRORLEVEL 1 GOTO GRoom

:SRoom
cls
Echo Shutdown Computers
Echo.
set /a i=1
set /p STE=Site and Room Number: 
set /p NUM=Number of Computers: 

:Sloop
cls
echo %STE%-0%i%
echo.
shutdown /f /s /t 0 /m \\%STE%-0%i%
TIMEOUT /T 1 /NOBREAK
IF %i%==%NUM% GOTO Sloop3
SET /a i=%i%+1
if %i%==10 GOTO SLOOP2
GOTO Sloop

:Sloop2
echo %STE%-%i%
echo.
shutdown /f /s /t 0 /m \\%STE%-%i%
TIMEOUT /T 1 /NOBREAK
IF %i%==%NUM% GOTO Sloop3
SET /a i=%i%+1
GOTO SLOOP2

:Sloop3
echo %STE%-staff
echo.
TIMEOUT /T 5 /NOBREAK
shutdown /f /s /t 0 /m \\%STE%-staff
GOTO Menu

:RRoom
cls
Echo Reboot Computers
Echo.
set /a i=1
set /p STE=Site and Room Number: 
set /p NUM=Number of Computers: 

:Rloop
cls
echo %STE%-0%i%
echo.
shutdown /f /r /t 0 /m \\%STE%-0%i%
TIMEOUT /T 1 /NOBREAK
IF %i%==%NUM% GOTO Rloop3
SET /a i=%i%+1
if %i%==10 GOTO RLOOP2
GOTO Rloop

:Rloop2
echo %STE%-%i%
echo.
shutdown /f /r /t 0 /m \\%STE%-%i%
TIMEOUT /T 1 /NOBREAK
IF %i%==%NUM% GOTO Rloop3
SET /a i=%i%+1
GOTO RLOOP2

:Rloop3
echo %STE%-staff
echo.
TIMEOUT /T 5 /NOBREAK
shutdown /f /r /t 0 /m \\%STE%-staff
GOTO Menu

:groom
cls
Echo GPUPDATE Classroom
Echo.
set /a i=1
set /p STE=Site and Room Number: 
set /p NUM=Number of Computers: 

:Gloop
start "%STE%-0%i%" M:\Programs\Program\PsExec.exe -nobanner \\%STE%-0%i% gpupdate /force /boot
TIMEOUT /T 1 /NOBREAK
IF %i%==%NUM% GOTO Gloop3
SET /a i=%i%+1
if %i%==10 GOTO Gloop2
GOTO Gloop

:Gloop2
start "%STE%-%i%" M:\Programs\Program\PsExec.exe -nobanner \\%STE%-%i% gpupdate /force /boot
TIMEOUT /T 1 /NOBREAK
IF %i%==%NUM% GOTO Gloop3
SET /a i=%i%+1
GOTO Gloop2

:Gloop3
TIMEOUT /T 5 /NOBREAK
start "%STE%-staff" M:\Programs\Program\PsExec.exe -nobanner \\%STE%-staff gpupdate /force /boot
GOTO Menu

:LRoom
cls
Echo Log Off Computers
Echo.
set /a i=1
set /p STE=Site and Room Number: 
set /p NUM=Number of Computers: 

:Lloop
cls
echo %STE%-0%i%
echo.
logoff /server:%STE%-0%i% 1
TIMEOUT /T 1 /NOBREAK
IF %i%==%NUM% GOTO Lloop3
SET /a i=%i%+1
if %i%==10 GOTO LLOOP2
GOTO Lloop

:Lloop2
echo %STE%-%i%
echo.
logoff /server:%STE%-%i% 1
TIMEOUT /T 1 /NOBREAK
IF %i%==%NUM% GOTO Lloop3
SET /a i=%i%+1
GOTO LLOOP2

:Lloop3
echo %STE%-staff
echo.
TIMEOUT /T 5 /NOBREAK
logoff /server:%STE%-staff 1
GOTO Menu

:Menu
If %computername% == %STE%-staff (
goto End
) Else (
GOTO Start

:End
EXIT

**************************
Batch File Above
**************************

*************************
Powershell Below
*************************
Clear-Host
$Search = read-host "Software to delete"
$SiteName = read-host "Site and Room"
$ComNum = read-host "Number of Computers"

For ($i=1; $i -le $ComNum; $i++) {
    if ($i -lt 10) {
        Invoke-Command -ComputerName $SiteName"-0"$i -ScriptBlock {
            param(
                $Search
                )
            $Keys = Get-ChildItem 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Group Policy\AppMgmt'
            $Items = $Keys | Foreach-Object {Get-ItemProperty $_.PsPath }
            foreach ($Item in $Items) {
                if ($Item.'GPO Name' -match $Search) {
                    Remove-Item $Item.PSPath
                    
                }
            }
        }-ArgumentList $Search, $SiteName, $ComNum
        $Computer = $SiteName + "-0" + $i
        Start-Process "c:\windows\system32\cmd.exe" "/c M:\Programs\Program\PsExec.exe \\$Computer gpupdate /force /boot"
                    
    }

    Else {Invoke-Command -ComputerName $SiteName-$i -ScriptBlock {
            param(
                $Search
                )
            $Keys = Get-ChildItem 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Group Policy\AppMgmt'
            $Items = $Keys | Foreach-Object {Get-ItemProperty $_.PsPath }
            foreach ($Item in $Items) {
                if ($Item.'GPO Name' -match $Search) {
                    Remove-Item $Item.PSPath
                }
            }
        }-ArgumentList $Search, $SiteName, $ComNum
        $Computer = $SiteName + "-" + $i
        Start-Process "c:\windows\system32\cmd.exe" "/c M:\Programs\Program\PsExec.exe \\$Computer gpupdate /force /boot"
    }
}

Invoke-Command -ComputerName $SiteName"-staff" -ScriptBlock {
    param(
        $Search
        )
    $Keys = Get-ChildItem 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Group Policy\AppMgmt'
    $Items = $Keys | Foreach-Object {Get-ItemProperty $_.PsPath }
    foreach ($Item in $Items) {
        if ($Item.'GPO Name' -match $Search) {
            Remove-Item $Item.PSPath
        }
    }
}-ArgumentList $Search, $SiteName, $ComNum
$Computer = $SiteName + "-staff"
Start-Process "c:\windows\system32\cmd.exe" "/c M:\Programs\Program\PsExec.exe \\$Computer gpupdate /force /boot"

************************
Powershell Above
************************

Comments

This post is locked

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

Share

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ