Link to home
Start Free TrialLog in
Avatar of Michael Leonard
Michael LeonardFlag for United States of America

asked on

need assistance with a powershell script

need some assistance with this script, we need the script to complete each action before moving to the next. It is currently not working as expected.

$serviceName = "CryptSvc";
$serviceStatus = (get-service "$serviceName").Status;

if ($serviceStatus -eq "Running") {
    echo "service is running. Stopping CryptSvc now!";
    Stop-Service -Name $servicename;
}


if ($serviceStatus -eq "Stopped") {
    echo "CryptSvc is stopped. Starting CryptSvc now!";
    Start-Service -Name $servicename;   
}


if ($serviceStatus -eq "Running") {
    echo "service is running!";
    Send-MailMessage -To team@mydomain.com -Subject "Monthly Maintenance Reboot Successful!" -

Body "Monthly Maintenance Reboot completed successfully!

Service has been stopped and started successfully, and service is now running."  -SmtpServer 

10.1.2.1 -From test@mydomain.com;
}

elseif ($serviceStatus -eq "Stopped"){
    echo "service is Running again now!";
    Send-MailMessage -To team@mydomain.com -Subject "Monthly Maintenance 

Reboot Failed!" -Body "Monthly Maintenance Reboot FAILED!

Service is STOPPED!"  -SmtpServer 10.1.2.4 -From 

test@mydomain.com;
}

Open in new window



thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Michael Leonard

ASKER

thank you. works great!