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
siber1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
A couple problems I see are:
- $serviceStatus is never updated.  Its value will never change from the time you set it on line 2.
- If I remember right, Stop- and Start-Service commands don't wait for the service, they function like the sc start <service> command and return immediately.

So, after each action you need to check the status (repeatedly) until the action is completed.  Try something like this.
$serviceName = "CryptSvc";
$serviceStatus = (get-service "$serviceName").Status;

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

do
{
    $serviceStatus = (get-service "$serviceName").Status;
    Start-Sleep -Milliseconds 500
} until ($serviceStatus -eq "Stopped")

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

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
siber1Author Commented:
thank you. works great!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.