#Exchange2010 #MsExchange #Powershell

Hello every Happy New Year and all that .. long time to talk.

Wanted to share this.  Basically had a Cisco UCS Blade failure today, where it took 2 nodes of a 3 node Exchange 2010 dag out.

Its been a fun day! NOT!

Anyway, I knocked this script up to monitor the database copy status when we put everything back.

PARAM([String]$Server = (HOSTNAME),
[int]$time            = 30)

Write-Host "Server:.."$Server
Write-Host "Timer:..."$time
$position = $host.ui.rawui.cursorposition
$position.y = $position.y+4
  Get-MailboxDatabaseCopyStatus -Server $Server
  $endpos = $host.ui.rawui.cursorposition
  for($i=1;$i-le $time;$i++){write-host "." -nonewline -f Yellow;sleep 1}
  Write-Host (" "*$time)

Find Exchange Databases using Powershell

A small change in $strFilter=”(objectClass=msExchPrivateMDB)” and you get all the mailbox databases ;-)

$forest    = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$Dom  = "LDAP://CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=" + $Forest.Name.Replace(".",",DC=")
$Root       = New-Object DirectoryServices.DirectoryEntry $Dom 
$selector   = New-Object DirectoryServices.DirectorySearcher 
$selector.PageSize    = 1000 
$selector.Filter      = $strFilter 
$selector.SearchRoot  = $root 
$selector.SearchScope = "Subtree" 
$Objs = $selector.findall() 

Find Exchange Servers using Powershell

#Powershell #MsExchange

I have a suite of discovery scripts that I use every now and then.  I adapted this to look in the AD and get a list of the exchange servers!

$forest    = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$Dom = "LDAP://CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=" + $Forest.Name.Replace(".",",DC=")
$Root       = New-Object DirectoryServices.DirectoryEntry $Dom
$selector   = New-Object DirectoryServices.DirectorySearcher
$selector.PageSize   = 1000
$selector.Filter     = $strFilter
$selector.SearchRoot = $root
$selector.SearchScope = "Subtree"
$Objs = $selector.findall()

Backing up Exchange Server 2010 with Windows Server Backup and Powershell

#powershell #msexchange

This was fun, or not as the case may be.So I have a temporary Exchange 2010 server that I am using to migrate users on to, and then off at a later date.

I slapped a 2TB external usb disk in the back of the server and wanted to back it up.  Using the GUI is easy, but I wanted to use Powershell do it!

Now you can use wbadmin to run a backup, but this for some reason doesn’t work if you run it in powershell.  After some digging I found the powershell snapin windows.serverbackup

So you can start powershell and run Add-PsSnapin windows.serverbackup

You then find a whole load of cmdlets you can use.  Check it them here:<

I’m not going to go in to real detail, but this is basics:

$policy = New-WBPolicy
$fileSpec = New-WBFileSpec -FileSpec D:\exchange.databases\database.swing01 
Add-WBFileSpec -Policy $policy -FileSpec $filespec
$fileSpec = New-WBFileSpec -FileSpec D:\exchange.databases\database.swing02 
Add-WBFileSpec -Policy $policy -FileSpec $filespec
$fileSpec = New-WBFileSpec -FileSpec D:\exchange.databases\database.swing03 
Add-WBFileSpec -Policy $policy -FileSpec $filespec

Set-WBVssBackupOptions -Policy $policy -VssFullBackup   
$backupLocation = New-WBBackupTarget -NetworkPath $BackupTarget 
Add-WBBackupTarget -Policy $policy -Target $backupLocation   
Start-WBBackup -Policy $policy

#Powershell Export-Clixml and Import-Clixml

I had a need to store some user data.  Using the Exchange 2010 Management Shell I could use Get-User and Get-Mailbox to get what I needed, but when you export it to CSV, some of the fields don’t export very well at all.I was looking at all kinds of funky ways to export the data, but then stumbled upon Export-Clixml.

I was amazed.  I could run say Get-Mailbox bob | Export-Cixml bob.xml

Then I could run $x = Import-Clixml bob.xml anywhere and I would have a variable ($x) with bob’s mailbox information in!

How cool is that!

#NetApp DataOnTap and Invoke-NaSysstat

So I have been play with the NetApp DataOnTap add in for a while now want to share this.  Essentially all it does is run Invoke-NaSysstat at timed intervals and saves it to an hourly csv file

$filer   = [Microsoft.VisualBasic.Interaction]::InputBox("Enter the comma seperated filer name", "Enter Filer Name", "")

#Change to match you path
Import-Module E:\PsMON\netapp\DataONTAP.psd1
Connect-NaController $Filer -Credential root

$matrix = @()
$doit = $True

$timer = 10

$LastHour = (Get-Date -format "HH")

  $ThisHour = (Get-Date -format "HH")
  $tSysStat = Invoke-NaSysstat -Count 1 | Select
  $matrix += $tSysStat
  If($ThisHour -ne $LastHour){
    $outFile = $filer + "_" + (Get-Date -format "yyyy-MM-dd_HHmm") + ".csv";
    $Matrix | Export-Csv $outFile -NoTypeInformation;$matrix = @();
  $LastHour = $ThisHour

  For($i=0;$i-le $timer;$i++){Sleep 1 }

$outFile = $filer + "_" + (Get-Date -format "yyyy-MM-dd_HHmm") + ".csv";$Matrix | Export-Csv $outFile -NoTypeInformation

Exchange Server 2010 Group Usage with Windows Powershell

#MsExchange #IAMMEC

So I have been running some discovery scripts on an M&A and wanted to check DL’s and when they were last used.  Came up with this ..

$days = 90
Set-ADServerSettings -ViewEntireForest:$true
$results = Get-TransportServer | get-messagetrackinglog -eventid expand -resultsize unlimited -start (get-date).addDays(-$days) | sort timestamp -desc

IF(![string]::IsNullOrEmpty($results)) {
  $report = @()
  $lists = @()
  $data = $results | group relatedrecipientaddress | sort name
  $lists = get-distributiongroup -resultsize unlimited | Select Alias, primarysmtpaddress, Name, RecipientType, OrganizationalUnit, @{Expression={""};Label="Count"}, @{Expression={""};Label="LastUsed"}
  $lists += Get-DynamicDistributionGroup -ResultSize unlimited | Select Alias, primarysmtpaddress, Name, RecipientType, OrganizationalUnit, @{Expression={""};Label="Count"}, @{Expression={""};Label="LastUsed"}
  $lists = $lists | sort alias
  foreach ($list in $lists) {
    $check = $null
    $check = $data | ?{$ -like "$($list.primarySMTPaddress.tostring())"}
    if ($check) {
      $List.Count = $Check.Count
      $List.LastUsed = ($check | select -expand group | select -first 1).TimeStamp

$lists | Export-csv GroupsWithUsage.csv -NoTypeInformation -Encoding  Unicode