Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Windows Powershell Script - Exchange 2007

Posted on 2010-08-17
8
Medium Priority
?
560 Views
Last Modified: 2012-05-10
I'm trying to create a Windows powershell script that will hide a user's mailbox and add one or three forwarding addresses depending upon the users Office and Department as specified in AD.  I started off with three different scripts that were run manually without accounting for Office or Department.  They worked as follows:

.\expired.ps1 "username"

 foreach ($i in $args)
{Set-Mailbox $i -ForwardingAddress ExpiredAccounts1 -HiddenFromAddressListsEnabled $true}

.\expired2.ps1 "username"

foreach ($i in $args)
{Set-Mailbox $i -ForwardingAddress ExpiredAccounts2 -HiddenFromAddressListsEnabled $true}

.\expired3.ps1 "username"

foreach ($i in $args)
{Set-Mailbox $i -ForwardingAddress ExpiredAccounts3 -HiddenFromAddressListsEnabled $true}

I tried using the get-user cmdlet along with the Department and Office properties of this cmdlet to combine all three scripts as follows:

.\expiredcombined.ps1 "username"

#Start of Script
$a = get-user $i |select department
$b = get-user $i |select office

foreach ($i in $args)

{
    if (($a -match "Dept1") -and ($b -match "Office1"))
    {
    Set-Mailbox $i -ForwardingAddress ExpiredAccounts2 -HiddenFromAddressListsEnabled $true
    }
        elseif (($a -match "Dept1") -and ($b -match "Office2"))
        {
        Set-Mailbox $i -ForwardingAddress ExpiredAccounts3 -HiddenFromAddressListsEnabled $true
        }
          else  
            {
            Set-Mailbox $i -ForwardingAddress ExpiredAccounts1 -HiddenFromAddressListsEnabled $true
            }
}            

#End of Script

This would work if the user had Office1 in the Office field and Dept1 in the department field but would not run for any other scenario - the elseif and else statements would not run.  I'm a noobie to writing scripts and may be way off base so any help would be greatly appreciated.
0
Comment
Question by:RKEE2008
  • 3
  • 3
  • 2
8 Comments
 
LVL 32

Accepted Solution

by:
endital1097 earned 1800 total points
ID: 33454677
you want to try the formally known as case command

$a = get-user $i |select department
$b = get-user $i |select office

switch ($a)
    {
        "HR" { switch ($b)
                      {   "Branch1" { Set-Mailbox.....}
                           "Branch2" { Set-Mailbox.....}
                       }
                 }
        "IT" { switch ($b)
                   { "Branch3" { Set-Mailbox...}
                    }
 {     default { Set-Mailbox ...}
    }

it may be easier to do it off of office first, then department
0
 

Author Comment

by:RKEE2008
ID: 33454828
Thanks for the quick response!  So, if I'm understanding correctly my script would look like this:

#Start of script

$a = get-user $i |select department
$b = get-user $i |select office

foreach ($i in $args)
{
    switch ($a)
    {
        "Dept"{switch($b)
                { "Office1" {Set-Mailbox $i -ForwardingAddress ExpiredAccounts2-HiddenFromAddressListsEnabled $true}
                  "Office2" {Set-Mailbox $i -ForwardingAddress ExpiredAccounts3 -HiddenFromAddressListsEnabled $true}
                 }
              }
                   
    { default {Set-Mailbox $i -ForwardingAddress ExpiredAccounts1 -HiddenFromAddressListsEnabled $true}
    }
}

#End        
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33454842
you got it
0
New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

 
LVL 13

Assisted Solution

by:soostibi
soostibi earned 200 total points
ID: 33455066
I do not really understan your $i variable at the beginning, it is not defined...

But I would do something like this, assuming that the $args contains the usernames:

$h = @{
       "Department1---Office1" = "emailoption1@something.com";
       "Department2---Office1" = "emailoption2@something.com";
       "Department1---Office2" = "emailoption3@something.com";
       "Department2---Office2" = "emailoption4@something.com";
     }

foreach($i in $args)
{
    $user = get-user $i
    $target = $h.("$($user.department)---$($user.office)")
    if($target) {
        set-mailbox -identity $user -ForwardingAddress () -HiddenFromAddressListsEnabled $true
    }
}

Open in new window

0
 
LVL 13

Expert Comment

by:soostibi
ID: 33455078
Sorry, I forgot the $target at the end.
$h = @{
       "Department1---Office1" = "emailoption1@something.com";
       "Department2---Office1" = "emailoption2@something.com";
       "Department1---Office2" = "emailoption3@something.com";
       "Department2---Office2" = "emailoption4@something.com";
     }

foreach($i in $args)
{
    $user = get-user $i
    $target = $h.("$($user.department)---$($user.office)")
    if($target) {
        set-mailbox -identity $user -ForwardingAddress $target -HiddenFromAddressListsEnabled $true
    }
}

Open in new window

0
 

Author Comment

by:RKEE2008
ID: 33455470
The issue I have is instead of doing one to one mappings with Department---------Office for all possible combinations, I only have one department and two offices that require a different forwarding address.  All others should go to one default address.  How would I use the last code snipet to account for this?
0
 
LVL 32

Expert Comment

by:endital1097
ID: 33455508
that's why i like to use the case statement, it is more dynamic
0
 

Author Comment

by:RKEE2008
ID: 33455822
ok!!! got it!  As soostibi said, the $i variable at the begining of the script was not defined and was causing issues.  I made the following modification and all is working great!  Thanks to you both for the great suggestions.  The help is appreciated.


foreach ($i in $args)
{

$a = get-user $i
$office = $a.Office
$department = $a.Department

    switch ($department)
    {
        "Department"{switch($Office)
                { 
                    "Office1" {Set-Mailbox $i -ForwardingAddress ExpiredAccounts2 -HiddenFromAddressListsEnabled $true}
                    "Office2" {Set-Mailbox $i -ForwardingAddress ExpiredAccounts3 -HiddenFromAddressListsEnabled $true}
                }
              }
                   
        default {Set-Mailbox $i -ForwardingAddress ExpiredAccounts1 -HiddenFromAddressListsEnabled $true}
     }
}

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Want to know how to use Exchange Server Eseutil command? Go through this article as it gives you the know-how.
If you have come across a situation where you need to find some EDB mailbox recovery techniques, then here you will find the same. In this article, we will take you through three techniques using which you will be able to perform EDB recovery. You …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…
Suggested Courses
Course of the Month12 days, 7 hours left to enroll

972 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