Solved

problem running exchange powershell script

Posted on 2013-06-13
15
1,923 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 42

Expert Comment

by:Amit
ID: 39245862
any patching done recently?
0
 
LVL 1

Author Comment

by:rpribanic
ID: 39245894
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 39

Expert Comment

by:Adam Brown
ID: 39245919
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 42

Expert Comment

by:sedgwick
ID: 39245947
Change line 9 to


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

Author Comment

by:rpribanic
ID: 39245949
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
ID: 39245954
@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 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39246892
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
 
LVL 1

Author Comment

by:rpribanic
ID: 39247608
@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
ID: 39247635
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 42

Expert Comment

by:Amit
ID: 39247659
Linux box??????????????????????????????????????????????Great!!!

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

Author Comment

by:rpribanic
ID: 39247752
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 42

Expert Comment

by:Amit
ID: 39247773
Did you have remote PS enable.
0
 
LVL 1

Author Comment

by:rpribanic
ID: 39247781
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
ID: 39248603
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
ID: 39265916
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This article explains how to install and use the NTBackup utility that comes with Windows Server.
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…
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

831 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