Server Management Shell (PowerShell)
09-26-2018
Language or Platform: Powershell
Code:
# Link your VCenter here:
$vcenter = 'SERVERNAMEHERE'
<#
-------------- SMASH - Server Management Shell --------------
.DISCLAIMER
I AM NOT RESPONSIBLE FOR ANYTHING YOU DO WITH THIS! TEST ON LAB ENVIRONMENT FIRST!
.IMPORTANT
RUN THIS SCRIPT AS ADMINISTRATOR
MAKE SURE YOU HAVE INSTALLED VMware PowerCLI 6.5+
.SYNOPSIS
Maintain and monitor lists of servers and services.
.SETUP
Place list of servers and services in servers.txt and services.txt.
.TROUBLESHOOTING
If you get an error about missing 'log4net' file, switch to a different architecture
of PowerShell. For example, you may need to run the 32bit version of PowerShell.
.AUTHOR
Josh M - 09/26/18
#>
#####################################
## Set Variables For Files ##########
$services = "$PSScriptRoot\services.txt" # $PSScriptRoot references current directory
$servers = "$PSScriptRoot\servers.txt"
$svs = Get-Content $services
$svr = Get-Content $servers
function menu {
@"
_________ .__
/ _____/ _____ _____ _____| |__
\_____ \ / \\__ \ / ___/ | \
/ \ Y Y \/ __ \_\___ \| Y \
/_______ /__|_| (____ /____ >___| /
\/ \/ \/ \/ \/
Server Management Shell
0. Quit
1. Check service status.
2. Check server status.
3. Set services to manual.
4. Set services to automatic.
5. Shut down services in order of server and service list.
6. Turn on services in reverse order of server and service list.
7. Shut down servers.
8. Turn on servers.
9. Restart servers.
10. Get Uptime on servers.
Type number and press Enter.
"@
$UI = Read-Host -Prompt ' '
Switch ($UI)
{
1 {one}
2 {two}
3 {three}
4 {four}
5 {five}
6 {six}
7 {seven}
8 {eight}
9 {nine}
10 {ten}
0 {zero}
Default {menu}
}
} # menu function end here
#####################################
## Pause Function ###################
Function pause ($message)
{
# Check if running Powershell ISE
if ($psISE)
{
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show("$message")
}
else
{
Write-Host "$message" -ForegroundColor Yellow
$x = $host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
}
#####################################
## OPTIONS #########################
Function one {
#####################################
## Check Services ###################
$svr | % {
foreach($name in $svs) {
if ($result = get-service -computer $_ -name $name -ErrorAction SilentlyContinue)
{
$result | select MachineName, ServiceName, StartType, Status
}
else {
Write-Host "NOTE Service $name does not exist on $_ : SKIPPING" -ForegroundColor Yellow
}
}
}
menu
}
Function two {
#####################################
## Server Status ####################
foreach($name in $svr) {
if (Test-Connection -ComputerName $name -BufferSize 16 -Count 1 -EA 0 -Quiet) {
Write-Host "$name Online" -ForegroundColor Green
} else {
Write-Host "$name Offline" -ForegroundColor Yellow
}
}
menu
}
Function three {
#####################################
## Services Manual ##################
$svr | % {
foreach($name in $svs) {
if ($result = get-service -computer $_ -name $name -ErrorAction SilentlyContinue)
{
Start-Sleep -Seconds 3 # pause 3 seconds between each service
$result | select MachineName, ServiceName, StartType, Status | Set-Service -computer $_ -StartupType Manual
"$name set to Manual on $_"
}
else {
Write-Host "NOTE Service $name does not exist on $_ : SKIPPING" -ForegroundColor Yellow
}
}
}
menu
}
Function four {
#####################################
## Services Auto ####################
$svr | % {
foreach($name in $svs) {
if ($result = get-service -computer $_ -name $name -ErrorAction SilentlyContinue)
{
Start-Sleep -Seconds 3 # pause 3 seconds between each service
$result | select MachineName, ServiceName, StartType, Status | Set-Service -computer $_ -StartupType Automatic
"$name set to Automatic on $_"
}
else {
Write-Host "NOTE Service $name does not exist on $_ : SKIPPING" -ForegroundColor Yellow
}
}
}
menu
}
Function five {
#####################################
## Shutdown Service In Order ########
Write-Host "`n"
Write-Host "We will now stop services for each server in the list. We will go through the list from top to bottom" -ForegroundColor Green
Write-Host "to make sure services are brought down in the proper order." -ForegroundColor Green
Write-Host "`n"
$svr | % {
foreach ($name in $svs) {
if ($result = get-service -computer $_ -name $name -ErrorAction SilentlyContinue)
{
$result | Get-Service -computer $_ | Stop-Service -Force
Write-Host "`n"
Write-Host "Wait 30 seconds to give the service $name on $_ enough time to stop, then press OK to continue..." -ForegroundColor Green
Write-Host "NOTE Sometimes the OK button will appear behind the application." -ForegroundColor Magenta
pause
$result | select MachineName, ServiceName, Status
Write-Host "Wait 30 seconds to give the service enough time to stop, then press OK to continue..." -ForegroundColor Green
Write-Host "If the status is 'Running', you may want to manually turn off the service $name on $_. "
pause
}
else {
Write-Host "`n"
# Write-Host "Service is not available on $_" -ForegroundColor Green
Write-Host "NOTE Service $name does not exist on $_ : SKIPPING" -ForegroundColor Yellow
# Write-Host "Press OK to continue..." -ForegroundColor Green
# pause
}
}
}
menu
}
Function six {
#####################################
## Start Service In Reverse Order ###
Write-Host "`n"
Write-Host "We will now stop services for each server in the list. We will go through the list from top to bottom" -ForegroundColor Green
Write-Host "to make sure services are brought down in the proper order." -ForegroundColor Green
Write-Host "`n"
# sometimes services and servers need to be brought up in reverse order of how they were shut down
# use the reverse
[array]::Reverse($svr) # reverse list of servers
[array]::Reverse($svs) # reverse list of services
$svr | % {
foreach ($name in $svs) {
if ($result = get-service -computer $_ -name $name -ErrorAction SilentlyContinue)
{
$result | Get-Service -computer $_ | Start-Service
Write-Host "`n"
Write-Host "Wait 30 seconds to give the service $name on $_ enough time to start, then press OK to continue..." -ForegroundColor Green
Write-Host "NOTE Sometimes the OK button will appear behind the application." -ForegroundColor Magenta
pause
$result | select MachineName, ServiceName, Status
Write-Host "Wait 30 seconds to give the service enough time to start, then press OK to continue..." -ForegroundColor Green
Write-Host "If the status is 'Stopped', you may want to manually turn on the service $name on $_. "
pause
}
else {
Write-Host "`n"
Write-Host "NOTE Service $name does not exist on $_ : SKIPPING" -ForegroundColor Yellow
}
}
}
menu
}
function seven {
#####################################
## Shutdown Servers #################
foreach ($name in $svr) {
stop-computer -computername $name -force -ErrorAction SilentlyContinue
# -throttlelimit 10 option shuts down 10 computers at a time
Write-Host "Shutting down $name." -ForegroundColor Green
}
menu
}
function eight {
#####################################
## Turn On Servers ##################
$user = Read-Host -Prompt 'Enter network username: '
$hidden = Read-Host -Prompt 'Enter network password: ' -AsSecureString
$pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($hidden))
# connect to vCenter
Import-module vmware.vimautomation.core
Connect-VIServer -Server $vcenter -Protocol https -User $user -Password $pass
foreach ($name in $svr) {
Start-VM -VM $name -RunAsync
# -Confirm option asks for confirmation before restart
Write-Host "Restarting $name." -ForegroundColor Green
}
menu
}
function nine {
#####################################
## Restart Servers ##################
$user = Read-Host -Prompt 'Enter network username: '
$hidden = Read-Host -Prompt 'Enter network password: ' -AsSecureString
$pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($hidden))
# connect to vCenter
Import-module vmware.vimautomation.core
Connect-VIServer -Server $vcenter -Protocol https -User $user -Password $pass
foreach ($name in $svr) {
Restart-VM -VM $name -RunAsync
# -Confirm option asks for confirmation before restart
Write-Host "Restarting $name." -ForegroundColor Green
}
menu
}
function ten {
#####################################
## Ping and Uptime ##################
$svr | % {
if ( Test-Connection -ComputerName $_ -Count 1 -ErrorAction SilentlyContinue )
{
$wmi = gwmi Win32_OperatingSystem -computer $_
$LBTime = $wmi.ConvertToDateTime($wmi.Lastbootuptime)
[TimeSpan]$uptime = New-TimeSpan $LBTime $(get-date)
Write-output "$_ Uptime is $($uptime.days) Days $($uptime.hours) Hours $($uptime.minutes) Minutes $($uptime.seconds) Seconds"
} else {
Write-output "$_ : No Connection."
}
}
menu
}
# another way to ping:
# $computers = "computer1","computer2","computer3"
# test-connection $computers -Count 1 | Select Address , IPv4Address , ResponseTime , BufferSize
function zero {
" "
" "
"Good Bye"
}
cls #clear screen
menu
Back