The computer shutdown is rather easy - as others have noted you might use the shutdown
command for this. If you have configured your machines' local firewalls to allow SMB and RPC from management hosts (e.g. by enabling remote administration exceptions in group policies), you could trigger the shutdowns centrally for all machines in your domain using the /m \\computername
parameter to your shutdown call:
shutdown /s /f /t 30 /m \\pcXXX /c "Automatic shutdown"
A more flexible version of the shutdown command is offered by SysInternals as psshutdown
. Among other things, it allows for user interaction (i.e. a user would be given the opportunity to abort a shutdown request if desired)
Now for determining if a user is logged on to a machine, you would have to test each of them, e.g. using the quser
command. The typical output would look like:
C:\Users\denis>quser
USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME
>denis console 1 Active none 22.04.2014 15:11
You could filter for a string like "Active" to determine if you can shut down safely. A batched version might look like this:
shutdownunused.bat
ping %1
if errorlevel 1 ( echo %1 seems offline && exit 1)
quser /SERVER:%1 | find "Active"
if %errorlevel% == 1 shutdown /s /f /t 30 /M \\%1 /c "Automatic shutdown"
calling shutdownunused.bat pc1234
would test and shut down pc1234 if it is a) pingable and b) has no user logged on actively (it would shut down a host if the user is logged on, but disconnected or has his machine locked). This can be scripted to your liking - either by using a local-only version as a scheduled task or centrally triggered from a management host through enumeration of all stations in your domain. The advantage of the latter approach is that it would be easier to keep logs and create statistics.