PowerShell Scripting for Exchange 2007

Vivek has been doing a lot of work to spread the word on the Exchange Management Shell and the wiki has A LOT of stuff as well (thanks to Vivek….go enjoy the best part of Seattle summer). I have noticed more people contributing to the effort of teaching the way of the shell and individual commands. This is all good as it takes some time to get used to the new format and nomenclature. Personally I am still a bit behind, there was the big warning on the beta 1 monad staid this is going to change by RTM so I didn’t do as much as I should have to learn it, of course it hasn’t changed that much but I am still behind :(

Today I saw Ben Winzenz’s great EMS post along with all the others and was inspired by ProBlogger‘s group writing project. So I would like to invite everyone to put up a post or comment on this post with a unique Exchange 2007 powershell script (sorry no prizes unless Manning Publications wants to sponsor it with a free copy of Bruce Payette‘s book). I have created a new tag that we can file them under so they will be easily searchable, “How To: Exchange Management Shell”. When your post is up, send me a link and I will combine all the posts that come in next week and post The List first thing in September

UPDATE 08/26 9:00am: Jeffrey Snover is willing to pitch in a signed copy of Bruce’s Book as a raffle type prize for this. If anyone else has a great prize, book, powershell shirt, etc, I will be happy to raffle them off to the list as well.   Again, to officially enter (now that there is a prize), send me an email with a link to your post josh . maher at gmail dot com OR put the link to your post as a comment to this entry. Oh and don’t forget the tag…..

Now we just need to get KC to convince the interns to stick around for another week and enter them into the wiki or the Exchange 2007 script repository so they will live on a little longer.

Wait!! where is my shell post? Mine will be a bit more brief then Ben’s but hopefully useful….

I do a lot of testing with different code releases and need to build and rebuild exchange servers a lot (too much already and the product isn’t even out yet!). Amazingly every time I rebuild my environment with a new drop of code I learn something new. One of those things I have been using for some time is a neat trick for creating multiple storage groups with the foreach command……

foreach (

$i in (3,4)

) {

new-storagegroup -name SG$i -server “E2k7-CCR-B2” -logfolderpath “C:\mounts\SG$i\Log” -SystemfolderPath “C:\mounts\SG$i\log”

}

foreach (

$i in (3,4)

) {

new-mailboxdatabase -storageGroup E2k7-CCR-B2\SG$i -Name MDB -edbFilePath “C:\mounts\SG$i\db\MDB.edb”

}

The first one will create 2 storage groups named SG3 & SG4. You can use this for up to 50 storage groups of course, but it was easier to show just two. The second one just takes those storage groups and creates databases inside of them. Usually I end up configuring my storage groups differently so it was easier to separate the commands…… If there is a better way to do this, please let me know….

Here is the screen shot of these in action!!

btw…. this is on VMWare Server :p

Digg this post Del.icio.us Furl

11 Responses to “PowerShell Scripting for Exchange 2007”

  1. Jeffrey Snover Says:

    > So I would like to invite everyone to put up a post or comment on this post with a unique Exchange 2007 powershell script (sorry no prizes unless Manning Publications wants to sponsor it with a free copy of Bruce Payette’s book).

    If you work out the logistics and can get 30 decent entries, I’ll donate a copy of Bruce’s book. I’ll even get Bruce to autograph it!

    BTW – I’m reviewing it and it is awesome!

    Deal?

    Jeffrey Snover [MSFT]
    Windows PowerShell/Aspen Architect
    Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
    Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

  2. Windows PowerShell : Exchange 2007 Windows PowerShell Script Contest Says:

    […] Exchange 2007 Windows PowerShell Script Contest Josh Maher has an exchange oriented blog at https://joshmaher.wordpress.com/ .  He is running a contest to for the best Exchange 2007 script ( https://joshmaher.wordpress.com/2006/08/25/powershell-scripting-for-exchange-2007/ ) and I offered to donate the prize of a copy of Bruce Payette’s book PowerShell in Action ( http://www.manning.com/payette/ ).  I’m doing the final review of Bruce’s book and it is awesome so I encourage you to enter and win! Also don’t forget that Exchange has a PowerShell 1-liner contest still going ( http://blogs.msdn.com/powershell/archive/2006/06/23/644175.aspx ).  If you have a one liner- there is no reason not to enter it in both! Enjoy Jeffrey Snover [MSFT]Windows PowerShell/Aspen ArchitectVisit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShellVisit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx     Published Monday, August 28, 2006 4:37 PM by PowerShellTeam […]

  3. How To: Exchange Management Shell - entrants « Messaging….. Technology…… Life….. Says:

    […] Well I’m not sure about entries but the Exchange team had a great post…………. their one-liner was better then what I was doing […]

  4. viveksharma.com: techlog Says:

    […] Thanks to Josh for organizing this. In addition to the offical Microsoft One-Liner contest for Exchange 2007, Josh has started his own scripting contest for PowerShell and Exchange. The best part about it is, that the winner gets a signed copy of the upcoming PowerShell book from Bruce Payette, one of the leading designers of PowerShell! Wow, nice job Jeffrey and Josh. […]

  5. Nick Smith Says:

    Here is a script to dynamically detect all databases on an Exchange 2007 server and backup each database with ntbackup.

    function perform_backup ([string] $ServerName, [string] $BackupRootDrive) {
    $BackupScriptPath = $BackupRootDrive + “BackupScripts\”
    $BackupPath = $BackupRootDrive + “DailyBackups\”

    $BackupScript = $BackupScriptPath + $ServerName + “BackupScript.bat”
    $BackupLog = $BackupScriptPath + “ExchangeBackupLogs.log”
    new-item $BackupScript -type file -force
    new-item $BackupLog -type file -force

    Add-Content $BackupScript “@ECHO off”
    #Add registry keys to improve ntbackup performance
    #See http://www.microsoft.com/technet/itsolutions/msit/operations/exchbkup.mspx
    Add-Content $BackupScript “reg add `”HKCU\Software\Microsoft\Ntbackup\Backup Engine`” /v `”Logical Disk Buffer Size`” /t REG_SZ /d 64 /f”
    Add-Content $BackupScript “reg add `”HKCU\Software\Microsoft\Ntbackup\Backup Engine`” /v `”Max Buffer Size`” /t REG_SZ /d 1024 /f”
    Add-Content $BackupScript “reg add `”HKCU\Software\Microsoft\Ntbackup\Backup Engine`” /v `”Max Num Tape Buffers`” /t REG_SZ /d 16 /f”

    Get-MailboxDatabase | where {$_.ServerName -eq $ServerName} | foreach{
    $JetBackupSyntax = “JET ” + $_.ServerName + “\Microsoft Information Store\” + $_.StorageGroupName + “\” + $_.AdminDisplayName
    $BksFileName = $BackupScriptPath + $_.ServerName + “” + $_.StorageGroupName + “” + $_.AdminDisplayName + “.bks”
    $JetBackupSyntax | out-file -filepath $BksFileName -encoding unicode
    Add-Content $BksFileName “”
    $BksDescriptiveName = $_.ServerName + “_” + $_.StorageGroupName + “_” + $_.AdminDisplayName
    $NTBackupLine = “C:\WINDOWS\system32\ntbackup.exe backup `”@” + $BksFileName + “`” /n `”” + $BksDescriptiveName + “`” /d `”” + $BksDescriptiveName + “`” /v:no /r:no /rs:no /hc:off /m normal /fu /j `”” + $BksDescriptiveName + “`” /l:s /f `”” + $BackupPath + $BksDescriptiveName + “.bkf`” ”
    Add-Content $BackupScript $NTBackupLine
    }
    Get-PublicFolderDatabase | where {$_.ServerName -eq $ServerName} | foreach{
    $JetBackupSyntax = “JET ” + $_.ServerName + “\Microsoft Information Store\” + $_.StorageGroupName + “\” + $_.AdminDisplayName
    $BksFileName = $BackupScriptPath + $_.ServerName + “” + $_.StorageGroupName + “” + $_.AdminDisplayName + “.bks”
    $JetBackupSyntax | out-file -filepath $BksFileName -encoding unicode
    Add-Content $BksFileName “”
    $BksDescriptiveName = $_.ServerName + “_” + $_.StorageGroupName + “_” + $_.AdminDisplayName
    $NTBackupLine = “C:\WINDOWS\system32\ntbackup.exe backup `”@” + $BksFileName + “`” /n `”” + $BksDescriptiveName + “`” /d `”” + $BksDescriptiveName + “`” /v:no /r:no /rs:no /hc:off /m normal /fu /j `”” + $BksDescriptiveName + “`” /l:s /f `”” + $BackupPath + $BksDescriptiveName + “.bkf`” ”
    Add-Content $BackupScript $NTBackupLine
    }
    & $BackupScript
    }

    perform_backup “ServerName” “R:\”

  6. joshmaher Says:

    Great script Nick, thanks for the entry!!

  7. How To: Exchange Management Shell - entries - last call « Messaging….. Technology…… Life….. Says:

    […] Alright, everyone has been asking about the how to powershell contest, so let’s wrap it up! There have not been a lot of entries (although there have been some new ones lately). So get your entries in and tag it with “How To: Exchange Management Shell” […]

  8. Digital Bricklayer » Blog Archive » Microsoft PowerShell: the next big thing for Windows admins Says:

    […] PowerShell manageability at their core, then PowerShell will go main stream in a big way. Microsoft Exchange 2007 already has PowerShell built in. The next version of Windows Server will have it built in too. In fact, Exchange 2007’s own […]

  9. Ski Lomax Says:

    Do you have a script that will tell me how much data in the entire system (or just one message server) is older than 180 days? Thanks, ski

  10. forex steam Says:

    This design is incredible! You certainly know how to keep a reader entertained.
    Between your wit and your videos, I was almost moved to start my own blog (well,
    almost…HaHa!) Fantastic job. I really enjoyed what you had
    to say, and more than that, how you presented it.
    Too cool!

  11. A single of the methods that is utilised by winning traders is that they have focus on what they are undertaking and will start off off by trading only 1 or two forex pairs so that they can very easily keep an eye on how that currency pair is performing a Says:

    I have to thank you for the efforts you have put in penning
    this blog. I am hoping to check out the same high-grade content from you
    later on as well. In truth, your creative writing abilities
    has motivated me to get my very own blog now ;)

Leave a comment