Shutdown Scripts for Linux and Windows

Before We Get Started.

Yes I know Powershell is an awesome tool so please don’t take this post as the only way to accomplish this task. Since I work in large enterprise networks that run pre-08 Windows boxes it’s not always possible to install Powershell just to execute my scripts. I focus on the task to be done not how to make my life easier.

Problem Statement:

In my home office I have a very powerful little box setup with VMWare ESXi that hosts several Windows and Linux guests. When these guests are all running (around 15 or so) the CPU fan kicks into high gear. During the day this is not a problem but at night the whining from the fan is just annoying. So each night I have to log into each guest and shut it down manually. I was in need of a way to shutdown these virtual boxes remotely without logging into each one.

Automated Solution:

What I decided to create was a VBScript that I could execute from my laptop that would remotely connect to the Window machines and execute a shutdown then login via ssh to the linux hosts and execute a shutdown.

The Finished Script:


‘*********************************************************
‘**  Set the global parameters
‘*********************************************************
pathPlink = “plink.exe”
strUserName = “remshutdown”
strKey = “privKey_remshutdown.ppk”
Set objWSH=CreateObject(“WScript.Shell”)

‘*********************************************************
‘**  Shutdown Linux Boxes
‘*********************************************************
cmdShutdownLinux “192.168.1.10″,strUserName,strKey
cmdShutdownLinux “192.168.1.11″,strUserName,strKey

‘*********************************************************
‘** Shutdown Windows Boxes
‘*********************************************************
cmdShutdownWindows “192.168.1.12″
cmdShutdownWindows “192.168.1.13″

‘*********************************************************
‘**   Done
‘*********************************************************
msgbox “Done”

‘*********************************************************
‘** Shutdown remote linux boxes via SSH
‘*********************************************************
Function cmdShutdownLinux(strComputerName,strUserName,strKey)
     strLinuxShutdownCommand = pathPlink & ” -T ” &_
     strUserName & “@” & strComputerName &” -i ” & strKey &_
     ” sudo /sbin/shutdown -h -P now”

     objWSH.Run (strLinuxShutdownCommand), 1, True

End Function

‘*********************************************************
‘Shutdown remote windows computer with WMI
‘*********************************************************
Function cmdShutdownWindows(strComputerName)
     strUser = “Administrator”
     strPassword = “”
     Set objLocator = CreateObject(“WbemScripting.SWbemLocator”)

‘had to update local security policy to allow user to login on
‘network as provided creds
‘Network Access: Sharing And Security Model For Local Accounts.

     Set objSWbemServices = objLocator.ConnectServer &_
          (strComputerName, “root\cimv2″, strUser, strPassword)

‘If using this script on an Active Directory Domain then use
‘this section. Otherwise you will need to provide creds to login
‘remotely as show above.
‘Dim objWMI
‘Set objWMI = GetObject(“winmgmts:{impersonationLevel=impersonate,(Shutdown)}!\\” &_
‘strComputerName &”\root\cimv2″)

     Set osColl = objSWbemServices.ExecQuery(“Select * from Win32_OperatingSystem”)
     For Each o in osColl
          o.Shutdown(1)
     Next
End Function

‘*********************************************************
‘Execute command on windows
‘*********************************************************
Function cmdProc(strComputerName, strCommand)
     Dim objWMI
     Set objWMI = GetObject(“winmgmts:{impersonationLevel=impersonate}!\\” &
          strComputerName &”\root\cimv2″)
     Set objProc = objWMI.Get(“Win32_Process”)
     errReturn = objProc.Create(strCommand, null, null, intProcessID)
     strCreated = “false”

     Do until strCreated = “true”
          Set colProc = objWMI.ExecQuery(“Select * from Win32_Process where ProcessID=’”
               & intProcessID & “‘”)
          If colProc.Count = 0 Then
               strCreated  = “true”
          End IF
     Loop

     If errReturn = 0 Then
          cmdProc = “true”
     Else
          cmdProc = “false”
     End If

End Function

Leave a Reply