problem running exchange powershell script

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!
LVL 1
rpribanicAsked:
Who is Participating?
 
QlemoConnect With a Mentor Batchelor, Developer and EE Topic AdvisorCommented:
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
 
AmitIT ArchitectCommented:
any patching done recently?
0
 
rpribanicAuthor Commented:
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
Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

 
Adam BrownSr Solutions ArchitectCommented:
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
 
Meir RivkinFull stack Software EngineerCommented:
Change line 9 to


ForEach ($Type in (Get-MailboxDatabaseCopyStatus -Server  em-dr-exmb01))
{
0
 
rpribanicAuthor Commented:
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
 
rpribanicAuthor Commented:
@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
 
rpribanicAuthor Commented:
@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
 
rpribanicAuthor Commented:
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
 
AmitIT ArchitectCommented:
Linux box??????????????????????????????????????????????Great!!!

I don't think MS supports or create PS for linux yet.
0
 
rpribanicAuthor Commented:
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
 
AmitIT ArchitectCommented:
Did you have remote PS enable.
0
 
rpribanicAuthor Commented:
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
 
rpribanicAuthor Commented:
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
 
rpribanicAuthor Commented:
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
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.

All Courses

From novice to tech pro — start learning today.