Powershell scripts for Tomcat node management

m_donovan
Contributor III

With a large windows based environment it became necessary for me to figure out how to use powershell to automate some of my tasks. Here are three scripts that I use in my environment that someone else with a windows environment may find helpful. I run all of these scripts from the master node.

The functions are all feed from an array of the server names and there is also a test version of the array you can use for testing. I am certain that these can all be improved upon and I welcome any suggestions or comments. I still think I like BASH better. As with everything please test, test, and test again before using in a production environment.

Child Node Management is used to Start or Stop Tomcat as well as check the status on all nodes.

# Child Node Tomcat Management script
# Created by Mike Donovan
# March 2018

#Porduction Array
$svrList = @('JSS107.yourcompany.org', 'JSS106.yourcompany.org','JSS105.yourcompany.org','JSS104.yourcompany.org','JSS103.yourcompany.org','JSS102.yourcompany.org','JSS101.yourcompany.org')

#Test Array
#$svrList = @(JSS101TST.yourcompany.org')

$request = Read-Host -prompt "1 for Get Status
2 for Stop
3 for Start
4 for Restart
5 to Exit
Enter "

write-host $request

function getStatus {
    write-host "Get Status"

    ForEach ($Index in (($svrList.Count - 1)..0))
    {    
        invoke-command {get-Service Tomcat8} -comp $svrList[$Index] 
    }
}

function stopService {

    ForEach ($Index in (($svrList.Count - 1)..0))
    {    
        $arrService = invoke-command {get-Service Tomcat8} -comp $svrList[$Index]
        $myHost = $svrList[$Index]
        if ($arrService.Status -ne "Stopped"){
            write-host "Stopping Tomcat on $myHost"
            invoke-command {stop-Service Tomcat8} -comp $svrList[$Index]
        } Else {

            write-host "Tomcat on $myHost is already stopped"
        }
    }
}

function restartService {

    ForEach ($Index in (($svrList.Count - 1)..0))
    {    
        $myHost = $svrList[$Index]
        $arrService = invoke-command {get-Service Tomcat8} -comp $svrList[$Index]
        if ($arrService.Status -ne "Stopped"){
            write-host "Restarting Tomcat on $myHost"
            invoke-command {restart-Service Tomcat8} -comp $svrList[$Index]
            sleep 10
        } Else {
            write-host "Tomcat on $myHost is already stopped. Starting Tomcat"
            invoke-command {start-Service Tomcat8} -comp $svrList[$Index]
            sleep 10
        }
    }
}

function startService {
    ForEach ($Index in (($svrList.Count - 1)..0))
    {    
        $arrService = invoke-command {get-Service Tomcat8} -comp $svrList[$Index]
        $myHost = $svrList[$Index]
        if ($arrService.Status -ne "Running"){
            write-host "Starting Tomcat on $myHost"
            invoke-command {start-Service Tomcat8} -comp $svrList[$Index]
        } Else {

            write-host "Tomcat on $myHost is already running"
        }

    }
}

function pauseScript {
    read-host "Press Enter to continue"
}

switch ( $request )
    {
        1 { getStatus; pauseScript }
        2 { stopService; sleep 3; getStatus; pauseScript  }
        3 { startService; sleep 3; getStatus; pauseScript  }
        4 { restartService; sleep 3; getStatus; pauseScript  }
        5 {}

    }

Last Reboot Time is used to check the last reboot times for all nodes. I will often times get a false alarm from our server software.

# Get last boot times script
# Created by Mike Donovan
# March 2018

#Porduction Array
$svrList = @('JSS107.yourcompany.org', 'JSS106.yourcompany.org','JSS105.yourcompany.org','JSS104.yourcompany.org','JSS103.yourcompany.org','JSS102.yourcompany.org','JSS101.yourcompany.org')

#Test Array
#$svrList = @(JSS101TST.yourcompany.org')

$request = Read-Host -prompt "1 for Get Last Boot Time Remote
2 for Get Last Boot Time Local
3 for Get Last Boot Time All
4 to Exit
Enter"

write-host $request

function getBootTimeRemote {
    write-host "Remote Computers"

    ForEach ($Index in (($svrList.Count - 1)..0))
    {    
        invoke-command {Get-CimInstance -ClassName win32_operatingsystem} -ComputerName $svrList[$Index] | select csname, lastbootuptime
    }
}

function pauseScript {
    read-host "Press Enter to continue"
}

function getBootTimeLocal {
    write-host "Local Computer"
    $os = Get-WmiObject win32_operatingsystem -ErrorAction SilentlyContinue
    $uptime = (Get-Date) - $os.ConvertToDateTime($os.LastBootUpTime)
    Write-Output ("Last boot: " + $os.ConvertToDateTime($os.LastBootUpTime) )
    Write-Output ("Uptime   : " + $uptime.Days + " Days " + $uptime.Hours + " Hours " + $uptime.Minutes + " Minutes" )
}

switch ( $request )
    {
        1 { getBootTimeRemote; pauseScript }
        2 { getBootTimeLocal; pauseScript  }
        3 { getBootTimeRemote; getBootTimeLocal; pauseScript  }
        4 {  }

    }

Copy File allows me to copy a file to all nodes at once. Comes in handy when copying the upgrade installers to all the nodes.

# Copy Files to all nodes script
# Created by Mike Donovan
# March 2018

#Porduction Array
$svrList = @('JSS107.yourcompany.org', 'JSS106.yourcompany.org','JSS105.yourcompany.org','JSS104.yourcompany.org','JSS103.yourcompany.org','JSS102.yourcompany.org','JSS101.yourcompany.org')

#Test Array
#$svrList = @(JSS101TST.yourcompany.org')

$request = Read-Host -prompt "1 To copy to all
2 to Exit
Enter"

write-host "You selected " + $request

function copyToRemote {
    write-host "Write to Remote Computers"

    # This is the file/folder(s) you want to copy to the servers in the $computer variable
    #$source = "C:Program FilesJSSTomcatwebappsROOTquickadd.zip"
    $source = Read-Host -prompt "Full path to Source File (C:Program FilesJSSTomcatwebappsROOTquickadd.zip)"

    # The destination location you want the file/folder(s) to be copied to
    #$destination = "c$Program FilesJSSTomcatwebappsROOT"
    $dest = Read-Host -prompt "Full path to Destination (C:Program FilesJSSTomcatwebappsROOT)"
    $destT = $dest -creplace '^[^\]*', ''
    $destination = "c$"+"$destT"

    ForEach ($Index in (($svrList.Count - 1)..0))
    {    
        $computer = $svrList[$Index]
        if ((Test-Path -Path \$computer$destination)) {
        Copy-Item $source -Destination \$computer$destination -Verbose
        } 

    }

}

function pauseScript {
    read-host "Press Enter to continue"
}


switch ( $request )
    {
        1 { copyToRemote; pauseScript }
        2 {  }

    }
0 REPLIES 0