Starting Exchange services via Powershell

I've been looking at this for too long, and I'm pretty far down the rabbit hole at this point so I'm reaching out to the community for a sanity check.

$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
 
function Start-ExchServices
{
    do {
        $services = @((Test-ServiceHealth | Select-Object -ExpandProperty servicesnotrunning))
        $exchservice = @()
        foreach ($a in $services) {
            $exchservice += New-Object -TypeName PSObject -Property @{
                Name = Get-WmiObject win32_service -ComputerName $env:COMPUTERNAME | Where-Object {$_.startmode -notlike 'Disabled'} | Select-Object name
            }
            if ($a -match $exchservice.name) {
                Start-Service $a.name -ErrorAction SilentlyContinue
                if ($? -eq $false) {
                    Write-Host 'An error occurred attempting to start the' $a.name 'service.  Exiting script.' -ForegroundColor Red
                    break
                }
                else {
                    Write-Host 'The' $a.name 'service was successfully started.' -ForegroundColor Cyan
                }
            }
            else {
                Write-Host 'No workie!'
            }
        }
    }
    until ($services -eq $null)
}
 
if (!($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))) {
    Write-Host 'Exchange Management Shell is not being run as Administrator.  Restart EMS as Administrator and re-run this script.' -ForegroundColor red
    Break
}
else {
    Start-ExchServices
}

Open in new window


I'm trying to provide our helpdesk guys an easy way to check for required Exchange services after patching or rebooting, and start any services in a Stopped status. I need compatibility for Exchange 2007/2010/2013 so I'm trying to provide one script to rule them all.

Here are the challenges I'm facing. Apparently the only way to query the StartMode of a Windows service (Automatic, Delayed Start, Disabled, etc) is through WMI, and I'm running into issues filtering the list of services based on the name due to the WQS query limitation. For instance, I can cache the list of all required services to a variable using Test-ServiceHealth and selecting the ServicesNotRunning property, but filtering the list of WMI services against the name is tripping me up due to the different string types Powershell returns for 2007 vs. 2010/2013. My latest attempt involves creating a PSObject to use for name filtering, but it's failing.

I've taught myself Powershell in bits and pieces over the years, and I feel like I'm missing something fundamental. Thanks in advance.
LVL 17
Jason CrawfordTransport NinjaAsked:
Who is Participating?
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.

Jason CrawfordTransport NinjaAuthor Commented:
Thanks to the Powershell community at reddit.com I was given the nudge I needed.  This works now.

function Start-ExchServices
{
    $i = 0
    do {   
        $services = @()
        ForEach ($servicenotrunning in (Test-ServiceHealth | Select-Object -ExpandProperty servicesnotrunning)) {
            $services += $servicenotrunning
        }
    
        if ($services.count -eq 0) {
            Write-Host 'All Exchange servers are started' -ForegroundColor Green
            $i = 1
        }
        elseif ($services.count -gt 0) {
            $exchservices = Get-WmiObject win32_service -ComputerName $env:COMPUTERNAME | Where-Object {$_.startmode -notlike 'Disabled' -and $services -contains $_.name}
            foreach ($exchservice in $exchservices) {
                $start = $exchservice.startservice()
                if ($start.ReturnValue -eq 0) {
                    Write-Host 'The' $exchservice.name 'service started successfully' -ForegroundColor Green
                }
            }
        }
    }
    until ($i -gt 0)
}

Open in new window

0

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
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.