I hope someone can help me here.
I am trying to write a simple script which ask the question if I want to restart a service or a server. I tried using try catch so that when a wrong server OR Service name is given, an error will be displayed.
The try catch part seem not to be working. What I am doing wrong..
#Write-host "Do you want to restart a Servive or a Server?"
$Boolean=Read-host"Type 1 to restart a SERVICE and 2 to restart a SERVER"
if ($Boolean-eq2)
{
Get-ADComputer-Filter {(Name-like"*SAP*")-or(Name-like"*rdsft*") }|Fwname-column2
$ServerName=Read-Host-prompt"View the list Displayed above and Type in the Server that has to be Restarted"
try {
Restart-Computer-ComputerName$ServerName-Confirm-Force }
catch{
write-host"Server name not correct"-ForegroundColorRed
write-host"This script will Exit"
Exit}
Write-Host"Server is being Restarted"-BackgroundColorGreen
Start-Sleep-s10 Write-Host"The Connection to this Server will be tested if its back online"-BackgroundColorGreen
ping$ServerName-t
}
else
{
Get-ADComputer-Filter {(Name-like"*SAP*")-or(Name-like"*rdsft*")}|Fwname-column2
$ServerName=Read-Host-prompt"View the list of Servers displayed above and Type in the one from which you want"Write-host`n`n
Get-service-ComputerName$ServerName
$ServiceName=Read-Host"Which Service Do You want to Restart, Please give in the Service Name?"
try {
Restart-Service-Name$ServiceName
}
catch
{
write-host"Something went wrong"-ForegroundColorRed
exit
}
Write-host"Service will be restarting..."
Write-host`n`n
Start-Sleep-s20
Write-host" We will check the Event logs to see if the Service actually restarted"-BackgroundColorMagenta
Get-EventLog-Newest5-LogName"System"
}