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: Restart stopped Exchange Services - microsoft.public.windows ...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 serve... batch script for stop and restart the services - microsoft.public ...Restart stopped Exchange Services - microsoft.public.windows ... Restart stopped Exchange Services - microsoft.public.windows ... Have to restart World Wide Web Publishing ... Help - Service Stopping - microsoft.public.windows.server.sbs ...Restart stopped Exchange Services - microsoft.public.windows ... Start/stop Services for Power ... Start/stop Services for Power users : Does anyone know of a way to give ... Have to restart World Wide Web Publishing Service - microsoft ...Restart stopped Exchange Services - microsoft.public.windows ... Have to restart World Wide Web Publishing Service - microsoft ... Restart stopped Exchange Services ... How to stop a service and wait until service stopped? - microsoft ...I want to stop a service and wait until it has been stopped, then do ... Restart stopped Exchange Services - microsoft.public.windows ... Hi, it is a logic issue ... variable not working within exchange 2010 powershell - microsoft ...Restart stopped Exchange Services - microsoft.public.windows ... > > Thats not working. ... Exchange 2010 services Creating a powershell script to stop the exchange services ... Access keeps stalling and restarting - microsoft.public.access ...Restart stopped Exchange Services - microsoft.public.windows ... Access keeps stalling and restarting - microsoft.public.access ... problem caused the program to stop ... IIS Admin Service - microsoft.public.windows.server.sbs ...Restart stopped Exchange Services - microsoft.public.windows ... Restart stopped Exchange Services - microsoft.public.windows ... When you stop Internet services, the ... Multiple string -notmatch - microsoft.public.windows.powershell ...Restart stopped Exchange Services - microsoft.public.windows ... Make sure that it has multiple services. try and access ... InvalidOperation: (StartService:String ... Email stopping every other day - microsoft.public.exchange.admin ...Email start flowing after I restart the Exchange Information Store service. If i restart the service let ... Status Notification for a particular email ... stop my ... Restart stopped Exchange Services .NET FrameworkHi, 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 re How to stop and restart Exchange Services: exchange, restart, servicesI have a batch file which is scheduled to run during the night. It stops my SqlServer and Exchange Server services in order to do a backup. I must be doing ... TechSpot: Script to Stop and Start Exchange Services before and ...Typically, exchange server will take much long time to shutdown and restart, if the exchange has a much more load, it will take around 25-30 minutes. The Real Shrimp: Restarting Exchange 2010 servicesCreating a powershell script to stop the exchange services is not so easy as i thought it would be. You could offcourse use the the Stop, restart and start ... Restart stopped Exchange Services - microsoft.public.windows ...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 serve... 7/24/2012 7:59:02 PM
|