Solved

problem running exchange powershell script

Posted on 2013-06-13
15
1,837 Views
Last Modified: 2013-06-21
I have 8 exchange servers in environment out of which 4 are in site A and 4 are in site B. I'm using scripts to monitor various health variables for all of these. All was working fine for me for 2 months until this Tuesday. Suddenly all servers in site B are failing to run script remotely.
I did some troubleshooting and came to find out that if I remote into any server that is giving me trouble and start windows powershell than load Add-PSSnapin Microsoft.Exchange.Management.PowerShell.e2010 non of the exchange commandlets want to run. Error I get is
Get-MailboxDatabaseCopyStatus : Value cannot be null.
Parameter name: serverSettings
At line:1 char:30
+ Get-MailboxDatabaseCopyStatus <<<<
    + CategoryInfo          : NotSpecified: (:) [Get-MailboxDatabaseCopyStatus], ArgumentNullException
    + FullyQualifiedErrorId : System.ArgumentNullException,Microsoft.Exchange.Management.SystemConfigurationTasks.GetM
   ailboxDatabaseCopyStatus

If I run exchange commandlet from EMC it works fine.

This is script I'm running from remote machine that started failing with similar error as above:
if ( (Get-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction:SilentlyContinue) -eq $null)
{
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
}

$NagiosStatus = "0"
$NagiosDescription = ""

ForEach ($Type in Get-MailboxDatabaseCopyStatus -Server  em-dr-exmb01) 
{

 	
	if ($Type.ContentIndexState -notlike "*Healthy*") 
	{
	# Look for failed Index state
	
		
		if ($NagiosDescription -ne "") 
		{
			# Format the output for Nagios
			$NagiosDescription = $NagiosDescription + ", "
		}
		
		$NagiosDescription = $NagiosDescription + $Type.Name + " = " + $Type.ContentIndexState
		
		# Set the status to failed.
		$NagiosStatus = "2"
		
	}
}

# Output, which string should we write to Nagios?
if ($NagiosStatus -eq "2") 
{
	Write-Host "CRITICAL:"$NagiosDescription
} 
else 
{
	Write-Host "OK: All content index states are healthy!"
}

exit $NagiosStatus

Open in new window


error I get on remote machine is:
Get-MailboxDatabaseCopyStatus : Value cannot be null.
Parameter name: serverSettings
At C:\Program Files\NSClient++\scripts\contentindexstate.ps1:48 char:48
+ ForEach ($Type in Get-MailboxDatabaseCopyStatus <<<<  -Server  em-dr-exmb01)
    + CategoryInfo          : NotSpecified: (:) [Get-MailboxDatabaseCopyStatus
   ], ArgumentNullException
    + FullyQualifiedErrorId : System.ArgumentNullException,Microsoft.Exchange.
   Management.SystemConfigurationTasks.GetMailboxDatabaseCopyStatus

OK: All content index states are healthy!
0
Comment
Question by:rpribanic
15 Comments
 
LVL 41

Expert Comment

by:Amit
Comment Utility
any patching done recently?
0
 
LVL 1

Author Comment

by:rpribanic
Comment Utility
no. that is first thing I was looking at. also to my knowledge nothing was changed on those 4 servers that are throwing errors.
0
 
LVL 38

Expert Comment

by:Adam Brown
Comment Utility
Change this:
ForEach ($Type in Get-MailboxDatabaseCopyStatus -Server  em-dr-exmb01)
to this
$status = get-mailboxdatabasecopystatus -Server em-dr-exmb01
foreach ($type in $status)
also run get-mailboxdatabasecopystatus -server em-dr-exmb01 to make sure it returns data. The error your getting basically says that you are passing null data.
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
Change line 9 to


ForEach ($Type in (Get-MailboxDatabaseCopyStatus -Server  em-dr-exmb01))
{
0
 
LVL 1

Author Comment

by:rpribanic
Comment Utility
I just tried but no luck.
Please note that if I open local windows powershell window and add exchange powershell snapin I get the same error. After exchange snapin is loaded I can tab through all exchange commandlets but it always returns "value cannot be null".
Being that this is happening on all server in site B I am thinking it could be something related to DC authentication but cannot find anything wrong so far.
0
 
LVL 1

Author Comment

by:rpribanic
Comment Utility
@Sedgwick
Tried that as well and same result. I tend to think there is nothing wrong with script as it works on all servers in my A site and until 2 days ago it worked just fine on servers in B site where this issue came up now.
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
Guys,

the syntax is correct. Neither putting the cmdlet in brackets nor assigning to a var makes a difference. It is ok to use something like
   foreach ($var in get-bla -one two -three four)

Open in new window



rpibanic,

All notes I found point towards an incorrect domain authentication, and tell you need to use a remote powershell session (see also http://technet.microsoft.com/en-us/library/dd297932.aspx) prior to execute Exchange cmdlets:
Import-PSSession (New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://em-dr-exmb01/PowerShell/ -Authentication Kerberos)

Open in new window

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:rpribanic
Comment Utility
@qlemo
thanks for the input I tried using import-possession before with a little different syntax but with no luck. I get different error when using import possession.
I used your example and following is the error I get on my monitoring server

[em-dr-exmb01] Processing data from remote server failed with the following err
or message: The user "domain.domain.net/Computers/EM-DR-EXMB02" isn't assigned
to any management roles. For more information, see the about_Remote_Troubleshoo
ting Help topic.
    + CategoryInfo          : OpenError: (System.Manageme....RemoteRunspace:Re
   moteRunspace) [], PSRemotingTransportException
    + FullyQualifiedErrorId : PSSessionOpenFailed
Import-PSSession : Cannot validate argument on parameter 'Session'. The argumen
t is null. Supply a non-null argument and try the command again.
At C:\Program Files\NSClient++\scripts\contentindexstate.ps1:37 char:18
+  Import-PSSession <<<<  (New-PSSession -ConfigurationName Microsoft.Exchange
-ConnectionUri http://em-dr-exmb01/PowerShell/ -Authentication Kerberos)
    + CategoryInfo          : InvalidData: (:) [Import-PSSession], ParameterBi
   ndingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power
   Shell.Co


This is how changed script looks like after I used your example:
 Import-PSSession (New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://em-dr-exmb01/PowerShell/ -Authentication Kerberos) 


# if ( (Get-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction:SilentlyContinue) -eq $null)
# {
#    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
# }

$NagiosStatus = "0"
$NagiosDescription = ""


ForEach ($Type in (get-mailboxdatabasecopystatus -Server em-dr-exmb01)) 
{

 	
	if ($Type.ContentIndexState -notlike "*Healthy*") 
	{
	# Look for failed Index state
	
		
		if ($NagiosDescription -ne "") 
		{
			# Format the output for Nagios
			$NagiosDescription = $NagiosDescription + ", "
		}
		
		$NagiosDescription = $NagiosDescription + $Type.Name + " = " + $Type.ContentIndexState
		
		# Set the status to failed.
		$NagiosStatus = "2"
		
	}
}

# Output, which string should we write to Nagios?
if ($NagiosStatus -eq "2") 
{
	Write-Host "CRITICAL:"$NagiosDescription
} 
else 
{
	Write-Host "OK: All content index states are healthy!"
}

exit $NagiosStatus

Open in new window

0
 
LVL 1

Author Comment

by:rpribanic
Comment Utility
I should also mention that if I run your import-pssession  from vanilla windows PowerShell it works just fine. However, when I invoke it in script it fails with above mentioned error. Also not sure if it matters but I'm calling script from Linux box that has no domain authentication. It basically communicates to server via installed agent on that particular server.
0
 
LVL 41

Expert Comment

by:Amit
Comment Utility
Linux box??????????????????????????????????????????????Great!!!

I don't think MS supports or create PS for linux yet.
0
 
LVL 1

Author Comment

by:rpribanic
Comment Utility
Linux uses agent on windows that is running as a service. As I mentioned before this was working just fine using Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
not sure if import-pssession would be option to use in my case-scenario.
0
 
LVL 41

Expert Comment

by:Amit
Comment Utility
Did you have remote PS enable.
0
 
LVL 1

Author Comment

by:rpribanic
Comment Utility
ok just found fix how to runn script using import-pssession instead of Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
My agent service was running as local system. As soon as I changed it to run as domain username with exchange rights it executed fine on my Linux box.

Will do some more testing before deciding if this will work for my purposes.

I'm still curious to why is my initial script failing when using Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010. I hope I do not have issues within my AD in site B.
0
 
LVL 1

Author Closing Comment

by:rpribanic
Comment Utility
So as I mentioned in earlier post I got it working. So to summarize I was able to get it working with code snippet provided by qlemo. I still had to add/change some parameters in order to have it working properly for my purposes.
code provided by qlemo:
Import-PSSession (New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://em-dr-exmb01/PowerShell/ -Authentication Kerberos)

Open in new window


Code I used:
Import-PSSession (New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$env:ComputerName/PowerShell/ -Authentication Kerberos) -CommandName Get-MailboxDatabaseCopyStatus | out-null

Open in new window


$env:ComputerName - changed servername to variable so that it is more general and I can use same code without changing it on multiple servers
CommandName Get-MailboxDatabaseCopyStatus - added this in order to avoid warning that was returned due to unapproved verbs. This way I only load commandlet I need.
out-null - used this so that I do not get any other return on import-pssession so I can parse only return I'm interested in after running desired commandlet

Also I had to change the way I was running service so that it runs under domain username instead localsystem.

I'm still at loss why my original way of running this script started failing. If anyone has any idea of what cause to that could be I would much appreciate any suggestions.
0
 
LVL 1

Author Comment

by:rpribanic
Comment Utility
For anyone with similar issue I just wanted to update the cause to this issue. As I initially suspected there was some authentication issues with AD. One of our DC's in this particular site was acting up and we were not able to see this during initial troubleshooting. As soon as we rectified problem with DC my script started working as originally implemented.
0

Featured Post

Too many email signature changes to deal with?

Are you constantly being asked to update your organization's email signatures? Do they take up too much of your time? Wouldn't you love to be able to manage all signatures from one central location, easily design them and deploy them quickly to users. Well, you can!

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
Following basic email etiquette rules will help you write a professional email and achieve a good, lasting impression with your contacts.
In this video we show how to create a User Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Mailb…
In this video we show how to create an Accepted Domain in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Ac…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now