Restart stopped Exchange Services

  • Follow


Hi,

I am trying to run a script which retrieves a list of Exchange 2003 Servers. 
I then want to check the Exchange related services on each server and restart 
them automatically. However I have run into 2 problems. Firstly, when I run 
the script below, it will only restart the first failed service. Secondly, I 
had to put a check in for $Null values due to the following error...

You cannot call a method on a null-valued expression.
At X:\CheckServices.ps1:57 char:37
+         $Result = $StoppedSrv.StartService <<<< ()
    + CategoryInfo          : InvalidOperation: (StartService:String) [], 
RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull


What am I doing wrong? Must be a logic issue...

Please help!!!

#----------------------------------------------------------------------------------------------
#Test Service Health on Exchange 2003 Servers


$E2K3Server = Get-ExchangeServer | where-object {$_.IsExchange2007OrLater 
-eq $false}

ForEach ($s in $E2K3Server) 
		{
		$StoppedSrv = get-wmiobject win32_service -ComputerName $s | where-object 
{ $_.name -match "^msexchange." -and $_.State -ne "Running" -and $_.startmode 
-eq "Auto"}
		foreach ($service in $StoppedSrv)
		{
		write-host "...Service $StoppedSrv stopped on Host $s"
		write-host "...Starting $StoppedSrv"
		$Result = $StoppedSrv.StartService()
		$Result.ReturnValue
	
		if ($Result.ReturnValue -eq 0)
		{
		write-host "...Successfully started $StoppedSrv"
		}
		else
		{
		write-host "...Failed to start $StoppedSrv"
		}
		}}}
0
Reply Utf 11/11/2009 12:44:01 PM

Hi,

it is a logic issue ;)

$StoppedSrv = stopped services
$service - every service

So it should be
E2K3Server = Get-ExchangeServer | where-object
{$_.IsExchange2007OrLater -eq $false}


ForEach ($s in $E2K3Server) {
	$StoppedSrv = get-wmiobject win32_service -ComputerName $s | where-
object { $_.name -match "^msexchange." -and $_.State -ne "Running" -
and $_.startmode -eq "Auto"}
	foreach ($service in $StoppedSrv) {
        write-host "...Service $service stopped on Host $s"
        write-host "...Starting $service"
        $Result = $service.StartService()
        $Result.ReturnValue


        if ($Result.ReturnValue -eq 0) {
        	write-host "...Successfully started $service"
        } else {
        	write-host "...Failed to start $service"
        }
    }
}

0
Reply Martin 11/11/2009 12:57:27 PM


Hi Martin, thanks for your reply.

Thats not working. If I stop both MTA and Pop Service on SERVER01, it only 
restarts the MTA. After that I still get the error.... Here is part of the 
output... The Null error is reported for each subsequent server.

[PS] X:\>./checkservices.ps1
....Service \\SERVER01\root\cimv2:Win32_Service.Name="MSExchangeMTA" stopped 
on Host SERVER01
....Starting \\SERVER01\root\cimv2:Win32_Service.Name="MSExchangeMTA"
0
....Successfully started 
\\SERVER01\root\cimv2:Win32_Service.Name="MSExchangeMTA"
....Service  stopped on Host SERVER02
....Starting
You cannot call a method on a null-valued expression.
At X:\CheckServices.ps1:54 char:40
+         $Result = $service.StartService <<<< ()
    + CategoryInfo          : InvalidOperation: (StartService:String) [], 
RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull


"Martin Zugec" wrote:

> Hi,
> 
> it is a logic issue ;)
> 
> $StoppedSrv = stopped services
> $service - every service
> 
> So it should be
> E2K3Server = Get-ExchangeServer | where-object
> {$_.IsExchange2007OrLater -eq $false}
> 
> 
> ForEach ($s in $E2K3Server) {
> 	$StoppedSrv = get-wmiobject win32_service -ComputerName $s | where-
> object { $_.name -match "^msexchange." -and $_.State -ne "Running" -
> and $_.startmode -eq "Auto"}
> 	foreach ($service in $StoppedSrv) {
>         write-host "...Service $service stopped on Host $s"
>         write-host "...Starting $service"
>         $Result = $service.StartService()
>         $Result.ReturnValue
> 
> 
>         if ($Result.ReturnValue -eq 0) {
>         	write-host "...Successfully started $service"
>         } else {
>         	write-host "...Failed to start $service"
>         }
>     }
> }
> 
> .
> 
0
Reply Utf 11/11/2009 1:46:07 PM

I would check the contents of your $stoppedSrv variable.  Make sure that it 
has multiple services. try and access each one manually to make sure the 
variable isn't being reset somewhere
-- 
Richard Siddaway
All scripts are supplied "as is" and with no warranty 
PowerShell MVP
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Don Pedro" wrote:

> Hi Martin, thanks for your reply.
> 
> Thats not working. If I stop both MTA and Pop Service on SERVER01, it only 
> restarts the MTA. After that I still get the error.... Here is part of the 
> output... The Null error is reported for each subsequent server.
> 
> [PS] X:\>./checkservices.ps1
> ...Service \\SERVER01\root\cimv2:Win32_Service.Name="MSExchangeMTA" stopped 
> on Host SERVER01
> ...Starting \\SERVER01\root\cimv2:Win32_Service.Name="MSExchangeMTA"
> 0
> ...Successfully started 
> \\SERVER01\root\cimv2:Win32_Service.Name="MSExchangeMTA"
> ...Service  stopped on Host SERVER02
> ...Starting
> You cannot call a method on a null-valued expression.
> At X:\CheckServices.ps1:54 char:40
> +         $Result = $service.StartService <<<< ()
>     + CategoryInfo          : InvalidOperation: (StartService:String) [], 
> RuntimeException
>     + FullyQualifiedErrorId : InvokeMethodOnNull
> 
> 
> "Martin Zugec" wrote:
> 
> > Hi,
> > 
> > it is a logic issue ;)
> > 
> > $StoppedSrv = stopped services
> > $service - every service
> > 
> > So it should be
> > E2K3Server = Get-ExchangeServer | where-object
> > {$_.IsExchange2007OrLater -eq $false}
> > 
> > 
> > ForEach ($s in $E2K3Server) {
> > 	$StoppedSrv = get-wmiobject win32_service -ComputerName $s | where-
> > object { $_.name -match "^msexchange." -and $_.State -ne "Running" -
> > and $_.startmode -eq "Auto"}
> > 	foreach ($service in $StoppedSrv) {
> >         write-host "...Service $service stopped on Host $s"
> >         write-host "...Starting $service"
> >         $Result = $service.StartService()
> >         $Result.ReturnValue
> > 
> > 
> >         if ($Result.ReturnValue -eq 0) {
> >         	write-host "...Successfully started $service"
> >         } else {
> >         	write-host "...Failed to start $service"
> >         }
> >     }
> > }
> > 
> > .
> > 
0
Reply Utf 11/11/2009 2:23:02 PM

Richard,

Thanks for your reply. The code is there as shown. I have been toying with 
this all day and can't see the woods from the trees at this stage.

I was hoping someone could direct me as to where my code was wrong by 
looking at the code I have posted.

"RichS [MVP]" wrote:

> I would check the contents of your $stoppedSrv variable.  Make sure that it 
> has multiple services. try and access each one manually to make sure the 
> variable isn't being reset somewhere
> -- 
> Richard Siddaway
> All scripts are supplied "as is" and with no warranty 
> PowerShell MVP
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
> 
> 
> "Don Pedro" wrote:
> 
> > Hi Martin, thanks for your reply.
> > 
> > Thats not working. If I stop both MTA and Pop Service on SERVER01, it only 
> > restarts the MTA. After that I still get the error.... Here is part of the 
> > output... The Null error is reported for each subsequent server.
> > 
> > [PS] X:\>./checkservices.ps1
> > ...Service \\SERVER01\root\cimv2:Win32_Service.Name="MSExchangeMTA" stopped 
> > on Host SERVER01
> > ...Starting \\SERVER01\root\cimv2:Win32_Service.Name="MSExchangeMTA"
> > 0
> > ...Successfully started 
> > \\SERVER01\root\cimv2:Win32_Service.Name="MSExchangeMTA"
> > ...Service  stopped on Host SERVER02
> > ...Starting
> > You cannot call a method on a null-valued expression.
> > At X:\CheckServices.ps1:54 char:40
> > +         $Result = $service.StartService <<<< ()
> >     + CategoryInfo          : InvalidOperation: (StartService:String) [], 
> > RuntimeException
> >     + FullyQualifiedErrorId : InvokeMethodOnNull
> > 
> > 
> > "Martin Zugec" wrote:
> > 
> > > Hi,
> > > 
> > > it is a logic issue ;)
> > > 
> > > $StoppedSrv = stopped services
> > > $service - every service
> > > 
> > > So it should be
> > > E2K3Server = Get-ExchangeServer | where-object
> > > {$_.IsExchange2007OrLater -eq $false}
> > > 
> > > 
> > > ForEach ($s in $E2K3Server) {
> > > 	$StoppedSrv = get-wmiobject win32_service -ComputerName $s | where-
> > > object { $_.name -match "^msexchange." -and $_.State -ne "Running" -
> > > and $_.startmode -eq "Auto"}
> > > 	foreach ($service in $StoppedSrv) {
> > >         write-host "...Service $service stopped on Host $s"
> > >         write-host "...Starting $service"
> > >         $Result = $service.StartService()
> > >         $Result.ReturnValue
> > > 
> > > 
> > >         if ($Result.ReturnValue -eq 0) {
> > >         	write-host "...Successfully started $service"
> > >         } else {
> > >         	write-host "...Failed to start $service"
> > >         }
> > >     }
> > > }
> > > 
> > > .
> > > 
0
Reply Utf 11/11/2009 3:08:01 PM

Hi Don,

in that case use following:

E2K3Server = Get-ExchangeServer | where-object
{$_.IsExchange2007OrLater -eq $false}


ForEach ($s in $E2K3Server) {
    [array]$StoppedSrv = get-wmiobject win32_service -ComputerName $s
| where-object { $_.name -match "^msexchange." -and $_.State -ne
"Running" -and $_.startmode -eq "Auto"}
    foreach ($service in $StoppedSrv) {
		If ($service -is [Object]) {
			write-host "...Service $service stopped on Host $s"
			write-host "...Starting $service"
			$Result = $service.StartService()
			$Result.ReturnValue


			if ($Result.ReturnValue -eq 0) {
					write-host "...Successfully started $service"
			} else {
					write-host "...Failed to start $service"
			}
		}
	}
}


Martin
0
Reply Martin 11/11/2009 3:15:31 PM

Hi Martin,

Thats close but no cigar. I stopped two Exchange Services on one server - 
MTA and POP3 - which match the criteria. However, it only finds and restarts 
the MTA.

We are still missing something although the NULL errors are no longer coming 
up.

Thanks for all your help thus far.

Pete

"Martin Zugec" wrote:

> Hi Don,
> 
> in that case use following:
> 
> E2K3Server = Get-ExchangeServer | where-object
> {$_.IsExchange2007OrLater -eq $false}
> 
> 
> ForEach ($s in $E2K3Server) {
>     [array]$StoppedSrv = get-wmiobject win32_service -ComputerName $s
> | where-object { $_.name -match "^msexchange." -and $_.State -ne
> "Running" -and $_.startmode -eq "Auto"}
>     foreach ($service in $StoppedSrv) {
> 		If ($service -is [Object]) {
> 			write-host "...Service $service stopped on Host $s"
> 			write-host "...Starting $service"
> 			$Result = $service.StartService()
> 			$Result.ReturnValue
> 
> 
> 			if ($Result.ReturnValue -eq 0) {
> 					write-host "...Successfully started $service"
> 			} else {
> 					write-host "...Failed to start $service"
> 			}
> 		}
> 	}
> }
> 
> 
> Martin
> .
> 
0
Reply Utf 11/11/2009 4:06:01 PM

Martin,

My bad. POP3 does not match the criteria. 

Sorry, your code works a treat.

Peter

"Don Pedro" wrote:

> Hi Martin,
> 
> Thats close but no cigar. I stopped two Exchange Services on one server - 
> MTA and POP3 - which match the criteria. However, it only finds and restarts 
> the MTA.
> 
> We are still missing something although the NULL errors are no longer coming 
> up.
> 
> Thanks for all your help thus far.
> 
> Pete
> 
> "Martin Zugec" wrote:
> 
> > Hi Don,
> > 
> > in that case use following:
> > 
> > E2K3Server = Get-ExchangeServer | where-object
> > {$_.IsExchange2007OrLater -eq $false}
> > 
> > 
> > ForEach ($s in $E2K3Server) {
> >     [array]$StoppedSrv = get-wmiobject win32_service -ComputerName $s
> > | where-object { $_.name -match "^msexchange." -and $_.State -ne
> > "Running" -and $_.startmode -eq "Auto"}
> >     foreach ($service in $StoppedSrv) {
> > 		If ($service -is [Object]) {
> > 			write-host "...Service $service stopped on Host $s"
> > 			write-host "...Starting $service"
> > 			$Result = $service.StartService()
> > 			$Result.ReturnValue
> > 
> > 
> > 			if ($Result.ReturnValue -eq 0) {
> > 					write-host "...Successfully started $service"
> > 			} else {
> > 					write-host "...Failed to start $service"
> > 			}
> > 		}
> > 	}
> > }
> > 
> > 
> > Martin
> > .
> > 
0
Reply Utf 11/11/2009 4:10:02 PM

Ok, so services are correctly restarted, however on one server only?

[array]E2K3Server = Get-ExchangeServer | where-object
{$_.IsExchange2007OrLater -eq $false}


ForEach ($s in $E2K3Server) {
    [array]$StoppedSrv = get-wmiobject win32_service -ComputerName $s
| where-object { $_.name -match "^msexchange." -and $_.State -ne
"Running" -and $_.startmode -eq "Auto"}
    foreach ($service in $StoppedSrv) {
                If ($service -is [Object]) {
                        write-host "...Service $service stopped on
Host $s"
                        write-host "...Starting $service"
                        $Result = $service.StartService()
                        $Result.ReturnValue


                        if ($Result.ReturnValue -eq 0) {
                                        write-host "...Successfully
started $service"
                        } else {
                                        write-host "...Failed to start
$service"
                        }
                }
        }



}
0
Reply Martin 11/11/2009 4:10:31 PM

Hi Martin,

No, they restart all services that match the criteria on all servers.

So, mission accomplished. Thanks for your help.

"Martin Zugec" wrote:

> Ok, so services are correctly restarted, however on one server only?
> 
> [array]E2K3Server = Get-ExchangeServer | where-object
> {$_.IsExchange2007OrLater -eq $false}
> 
> 
> ForEach ($s in $E2K3Server) {
>     [array]$StoppedSrv = get-wmiobject win32_service -ComputerName $s
> | where-object { $_.name -match "^msexchange." -and $_.State -ne
> "Running" -and $_.startmode -eq "Auto"}
>     foreach ($service in $StoppedSrv) {
>                 If ($service -is [Object]) {
>                         write-host "...Service $service stopped on
> Host $s"
>                         write-host "...Starting $service"
>                         $Result = $service.StartService()
>                         $Result.ReturnValue
> 
> 
>                         if ($Result.ReturnValue -eq 0) {
>                                         write-host "...Successfully
> started $service"
>                         } else {
>                                         write-host "...Failed to start
> $service"
>                         }
>                 }
>         }
> 
> 
> 
> }
> .
> 
0
Reply Utf 11/12/2009 9:24:01 AM

9 Replies
369 Views

(page loaded in 0.116 seconds)

Similiar Articles:
















7/24/2012 7:59:02 PM


Reply: