Announcement

Collapse
No announcement yet.

"Green" Script for Admins.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • "Green" Script for Admins.

    Here's one I cooked up for my Home Domain; It shuts down Windows computers if there is no one logged into the console. This is run as a scheduled task on the DC right after the kid's bedtime (actually three times per night, but that is for another day) . The Domain Logon hours for the kids force a logoff, so this script is intended shutdown computers which do not have a console user logged-on. The Script will not shutdown Windows Server Operating systems.

    Code:
    @ECHO OFF
    
    :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    ::                 Script to Shut Down Idle PCs After Hours.                 ::
    ::    Script will not shutdown PCs on the 2nd Tue. or Wed. of the Month.     ::
    :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
                                                                                              
    SETLOCAL
    
    SET BIN_PATH=C:\Tools\
    SET DAY_DATE=UNKNOWN
    SET DEVICE_NAME=UNKNOWN
    SET LOCAL_DOMAIN=UNKNOWN
    SET LOG_FILE=Domain_Shutdown.log
    SET LOG_PATH=C:\TEMP
    SET SHUTDOWN_LOG=%LOG_PATH%\%LOG_FILE%
    SET STATUS=UNKNOWN
    
    
    :GET_DOMAIN
    
    FOR /F "tokens=1,2 delims== usebackq" %%a IN (`WMIC COMPUTERSYSTEM GET Domain /FORMAT:CSV`) DO FOR /F "tokens=2 delims=, usebackq" %%b IN (`ECHO %%a`) DO FOR /F "tokens=1 delims=. usebackq" %%c IN (`ECHO %%b`) DO SET LOCAL_DOMAIN=%%c
    ECHO.>>%SHUTDOWN_LOG%& ECHO Domain is %LOCAL_DOMAIN%.>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG%
    
    
    :CHECK_DAY
    
    FOR /F "tokens=1,2 usebackq" %%a IN (`ECHO %DATE% /FIND /I "tue"`) DO FOR /F "tokens=1,2,3 delims=/- usebackq" %%c IN (`ECHO %%b`) DO SET DAY_DATE=%%d
    FOR /F "tokens=1,2 usebackq" %%a IN (`ECHO %DATE% /FIND /I "wed"`) DO FOR /F "tokens=1,2,3 delims=/- usebackq" %%c IN (`ECHO %%b`) DO SET DAY_DATE=%%d
    IF %DAY_DATE% LEQ 7 ECHO.>>%SHUTDOWN_LOG%& ECHO %DATE% is Restricted from Shutdown.>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG%& GOTO LOG_RESULTS
    ECHO.>>%SHUTDOWN_LOG%& ECHO %DATE% is Not Restricted from Shutdown.>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG%
    
    
    :CHECK_CLIENTS
    
    FOR /F "tokens=1 usebackq" %%a IN (`NET VIEW ^| FIND /I "\\"`) DO CALL :PARSE_DEVICE_NAME %%a
    
    
    :LOG_RESULTS
    
    ECHO.>>%SHUTDOWN_LOG%& ECHO END OF SCRIPT>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG%
    
    ENDLOCAL
    
    GOTO END
    
    
    :PARSE_DEVICE_NAME
    
    SET DEVICE_NAME=%1
    SET DEVICE_NAME=%DEVICE_NAME:~2%
    ECHO.>>%SHUTDOWN_LOG%& ECHO DEVICE NAME=%DEVICE_NAME%.>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG%
    
    ::DEVICE-CONNECTIVITY-CHECK
    FOR /F "tokens=* usebackq" %%a IN (`PING %DEVICE_NAME% -n 2 ^| FIND /I "time"`) DO ECHO.>>%SHUTDOWN_LOG%& ECHO %DEVICE_NAME% STATUS=ONLINE>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG%& GOTO SERVER-EXCEPTION-CHECK
    FOR /F "tokens=* usebackq" %%a IN (`PING %DEVICE_NAME% -n 2 ^| FIND /I "host"`) DO ECHO.>>%SHUTDOWN_LOG%& ECHO %DEVICE_NAME% STATUS=OFFLINE>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG%& GOTO END
    FOR /F "tokens=* usebackq" %%a IN (`PING %DEVICE_NAME% -n 2 ^| FIND /I "find"`) DO ECHO.>>%SHUTDOWN_LOG%& ECHO %DEVICE_NAME% STATUS=UNKNOWN>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG%& GOTO END
    
    
    :SERVER-EXCEPTION-CHECK
    FOR /F "tokens=* usebackq" %%a IN (`WMIC /NODE:%DEVICE_NAME% OS GET Name ^| FIND /I "server"`) DO ECHO.>>%SHUTDOWN_LOG%& ECHO Server OS Detected, no Shutdown attempted.>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG%& GOTO END
    
    ::CONSOLE-USER-CHECK
    FOR /F "tokens=1,2 delims=\ usebackq" %%a IN (`WMIC /NODE:%DEVICE_NAME% COMPUTERSYSTEM GET UserName ^| FIND /I "%LOCAL_DOMAIN%"`) DO ECHO.>>%SHUTDOWN_LOG%& ECHO Console User %%b Detected, no Shutdown attempted.>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG%& GOTO END
    
    
    :SHUTDOWN_PC
    
    ::PAYLOAD
    ECHO.>>%SHUTDOWN_LOG%& ECHO Executing the following Command Line: PSSHUTDOWN.exe -s -f -t 10 \\%DEVICE_NAME%>>%SHUTDOWN_LOG%& ECHO.>>%SHUTDOWN_LOG%
    "%BIN_PATH%\PSSHUTDOWN.exe" -s -f -t 10 \\%DEVICE_NAME%>>%SHUTDOWN_LOG%
    
    
    :END
    Hey, Donny! We got us a German who wants to die for his country... Oblige him. - Lt. Aldo Raine

  • #2
    Very nice, but it reminds me why I switched to PowerShell. I can't stand the DOS syntax anymore.

    Code:
    #requires -version 2.0
    ### shut down all computers on a domain or workgroup ###
    # assumes the running user has admin access to all systems on the domain, for workgroups update the credentials in the foreach loop.
    
    # stores script log for writing
    $log = @()
    # timestamp for log writing
    function TimeStamp {return Get-Date -format "yyyy-MM-dd HH:mm:ss"}
    
    ## make sure it is not Patch Tuesday/Wednesday ##
    $today = get-date
    # find Patch Tuesday
    1..7 | foreach-object {if ((get-date -month $today.month -day $_ -format dddd).toString() -eq "Tuesday") { $PatchTues = (get-date -month $today.month -day $_).addDays(7) }}
    
    if ($today.ToShortDateString() -ne $PatchTues.ToShortDateString() -and $today.ToShortDateString() -ne $PatchTues.addDays(1).ToShortDateString()) {
    	# get the domain
    	$domain = (get-wmiobject win32_computersystem).domain.toString()
    	
    	if ($domain -ne "WORKGROUP") {
    		# search AD for all computers on the domain
    		$strFilter = "computer" 
    		$objDomain = New-Object System.DirectoryServices.DirectoryEntry
    		$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
    		$objSearcher.SearchRoot = $objDomain
    		$objSearcher.SearchScope = "Subtree"
    		$objSearcher.PageSize = 1000
    		$objSearcher.Filter = "(objectCategory=$strFilter)"
    		$colResults = $objSearcher.FindAll()
    		$ComputerList = $colResults | %{$_.properties.dnshostname}
    	} else {
    		$ComputerList = [ADSI]"WinNT://$domain" | %{$_.psbase.children }
    		$ComputerList = $ComputerList | %{$_.Path.split('/')[-1]}
    	}
    	
    	foreach ($computer in $ComputerList) {
    		$responding = test-connection $computer -count 2 -quiet
    		if ($domain -ne "WORKGROUP") {
    			## workgroup credentials ##
    			# administrator account name used for all workgroup computers
    			$Script:ServerAdmin = "$computer\Administrator	"
    			# administrator account password
    			$Script:ServerAdminPass = "Password"
    			$tempPass = $ServerAdminPass | ConvertTo-SecureString -asPlainText -Force
    			$script:Creds = new-object System.Management.Automation.PSCredential($ServerAdmin, $tempPass)
    			$isServer = (get-wmiobject win32_OperatingSystem -computer $computer -credentials $creds -authentication 6).Caption -notmatch "Server"
    		} else {
    			$isServer = (get-wmiobject win32_OperatingSystem -computer $computer).Caption -notmatch "Server"
    		}
    		if ($responding -and (get-wmiobject win32_OperatingSystem -computer $computer).Caption -notmatch "Server") {
    			$log += "$(TimeStamp): $computer is being shut down."
    			#$log += stop-computer -computerName $computer -force
    		} else {
    			$log += "$(TimeStamp): $computer is not responding."
    		}
    		$log += ""
    	}	
    } else {
    	$log += "$(TimeStamp): Patch day, no shut downs."
    }
    # write the log
    # remove -append if you want to overwrite the log each day
    $log | out-file C:\TEMP\Domain_Shutdown.log -append -force
    $log
    “Inside every sane person there’s a madman struggling to get out”
    –The Light Fantastic, Terry Pratchett

    Comment


    • #3
      I could have done it in PS, but not many people seem to use it here...
      Hey, Donny! We got us a German who wants to die for his country... Oblige him. - Lt. Aldo Raine

      Comment


      • #4
        Originally posted by MultimediaMan View Post
        I could have done it in PS, but not many people seem to use it here...
        You should change that. PowerShell is the way to go for Windows automation. I converted my company to PowerShell and we haven't looked back.
        “Inside every sane person there’s a madman struggling to get out”
        –The Light Fantastic, Terry Pratchett

        Comment


        • #5
          No argument there... all of the Windows-based VMware PowerCLI script use PowerShell.

          Once the .Net issues get untangled (not my problem, fortunately) then we can roll PS.
          Hey, Donny! We got us a German who wants to die for his country... Oblige him. - Lt. Aldo Raine

          Comment


          • #6
            What .NET issues are you talking about?
            “Inside every sane person there’s a madman struggling to get out”
            –The Light Fantastic, Terry Pratchett

            Comment

            Working...
            X