Solved

Task problem for Exchange powershell script

Posted on 2011-03-04
5
914 Views
Last Modified: 2012-05-11
I have created a script that is going to update the mailbox RpcClientAccessServer if a failover occur. This is my first script ever so bear with me on this. The script works great if run from cmd or from run field or powershell of course.

However when I add it as an scheduled task to start with the computer it looks like it freezes on Get-MailboxDatabaseCopyStatus. Can somebody help me with what I'm doing wrong?

I run the scheudled task as: powershell.exe -command "&{C:\Script\FailoverMon.ps1}"

 
Set-PSDebug -Trace 0

$MailTo = "Name <first.lastname@comapny.com>"	# E-mail address where the failover e-mails will be send to
$MailFrom = "IT <irst.lastname@comapny.com"	# E-mail address shown in the from field
$UserName = "username"
$UserPassword = "Password"
$MailDB = "MAILDB"

$MailText = "Failover fix start on server: "
$MailTextEnd = "Failover fix ended on server: "

$secpasswd = ConvertTo-SecureString $UserPassword -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($UserName, $secpasswd)

$objIPProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()
$hostName = "{0}.{1}" -f $objIPProperties.HostName, $objIPProperties.DomainName

$File ="C:\Script\FailoverMon.txt"

$currentSite = [System.DirectoryServices.ActiveDirectory.ActiveDirectorySite]::GetComputerSite().name

# Build Exchange 2010 Powershell session
$Session = New-PSSession -Configurationname Microsoft.Exchange -ConnectionUri "http://$hostName/powershell"
Import-PSSession $Session

Get-Date | Out-File $File
Write-Output "FailoverMon started" | Out-File $File -append 

# The actual function doing all the work
Function Do-Check (){
		If ((Get-MailboxDatabaseCopyStatus).Status -eq "Mounted") {
			Write-Output "Mailbox Mounted!" | Out-File $File -append
			$siteCAS = (Get-ClientAccessArray -Site $currentSite | Format-wide -Property Fqdn -Column 1 | Out-String).TrimStart().TrimEnd()
			$curentCAS = (Get-MailboxDatabase | Format-wide -Property RpcClientAccessServer -Column 1 | Out-String).TrimStart().TrimEnd()
			if ($siteCAS.CompareTo($curentCAS) -eq 0) {
				Write-Output "CAS array is set corectlly" | Out-File $File -append
			} else {
				Write-Output "CAS array is NOT set corectlly!" | Out-File $File -append
				Send-MailMessage -To $MailTo -Subject "Exchange Failover Fix: start" -From $MailFrom -Body ($MailText + $hostName +" ("+$curentCAS+")") -SmtpServer $hostName -Priority High -Credential $mycreds -UseSsl
				Write-Output "Updating array!" | Out-File $File -append
				Set-MailboxDatabase -Identity $MailDB -RpcClientAccessServer $siteCAS
				Get-Mailbox | Update-Recipient
				$newCurentCAS = (Get-MailboxDatabase | Format-wide -Property RpcClientAccessServer -Column 1 | Out-String).TrimStart().TrimEnd()
				Send-MailMessage -To $MailTo -Subject "Exchange Failover Fix: end" -From $MailFrom -Body ($MailTextEnd + $hostName +" ("+$newCurentCAS+")") -SmtpServer $hostName -Priority High -Credential $mycreds -UseSsl
				Write-Output "Updating array: Done" | Out-File $File -append
			}
		} Else {
			Write-Output "Not Mounted!" | Out-File $File -append
		}
	
        Sleep 60
		# Do check again
		Do-Check
}

# Starting the function
Do-Check

Open in new window

0
Comment
Question by:Findwise
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 9

Accepted Solution

by:
Dan Arseneau earned 500 total points
ID: 35037142
Looks like you're trying to run Exchange commands in vanilla Powershell.  Won't work.

Insert a first line of

Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
0
 

Author Comment

by:Findwise
ID: 35037743
Wow that easy. So strange that it worked when I started the script from cmd. However adding your line solved everything. Thanks.

Now hope more people can enjoy this script since I've understood that this something that Microsoft missed. Just place it on both your DAG members and it will automatically switch.
0
 

Author Closing Comment

by:Findwise
ID: 35037744
thanks for your help
0
 
LVL 9

Expert Comment

by:Dan Arseneau
ID: 35039148
FYI.  Creating a CAS array negates the need for this.
0
 

Author Comment

by:Findwise
ID: 35040074
My dag is cross site. And you can't do this over sites sadly. So when failover occurs to the other site this updates so that all users point to the cas array for that site.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Marketers need statistics and metrics like everybody else needs oxygen. In this article we explain how to enable marketing campaign statistics for Microsoft Exchange mail.
MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
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…
To show how to create a transport rule 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 >> Rules tab.:  To cr…

740 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