Solved

Powershell Exchange read department from AD and use the correct database

Posted on 2013-05-21
3
216 Views
Last Modified: 2013-05-24
Hi,

If there are three letters (EMC) stored in the AD under Department, then use the Database  DBEMC0001 to DBEMC0045. If the 3 letters EMC not found then the database parameter not needed. pls see the code.

Also in my code the passwordreset isn't  functional. here is the CSV

UserId,OrderID,Action,Version
Z851037,CX151828001,new,C2010
Z851037,CX151828001,reset,C2010
Z333333,CX151828001,delete,C2010

1.Action=new and C2010=enable then enable 2010 MBX of existing AD-user
2.Action=reset create new password  (not functional dont know why)
3.Action=delete disable Mailbox and write used database to Description

Open in new window



Import-Module activedirectory
ForEach ($Entry in Import-Csv 'c:\import.csv'){} 

  
  If($Entry.Action -like "new") {
  If($Entry.Version -like "C2010") {
######################################################

$DBuser = Get-ADUser -identity $Entry.UserID -Properties department
If ?????? EMC
$db = "DBEMC00$("{0:00}" -f (1..45 | Get-random))"
Enable-Mailbox -Identity $Entry.UserId -Database $db

else
Enable-Mailbox -Identity $Entry.UserId
##########################################################            
  } # end if create

  If($Entry.Action -like "delete") {         
   Disable-Mailbox -Identity $entry.UserID 
        } # end if delete

  If($Entry.Action -like "reset") { 

$NewPassword = $Entry.UserId.Insert(5,"!").Insert(3,"k").Remove(0, 1).Insert(0,"P") 
write-host -ForegroundColor yellow -backgroundcolor blue "The new password called $newpassword"
Set-ADAccountPassword -Identity $Entry.UserId -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "$newpassword" -Force)	  
}    

#set decription
	   
$desccurrent = (Get-ADUser -identity $Entry.userid -Properties Description).Description
$desccurrent = "{0}, OrderID: {1}, DB: {2}" -f $entry.orderid	, $descnew, $db 
set-aduser -identity $Entry.userid -description $desccurrent

Open in new window


Maybe anybody can help me?

appreciate for your help
Mandy
0
Comment
Question by:Mandy_
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
mouseware earned 500 total points
ID: 39186158
I think this is what you are asking:
...
            $DBuser = (Get-User -identity $Entry.UserID).department
            If ($DBuser -like "*EMC*") {
                $db = "DBEMC00$("{0:00}" -f (1..45 | Get-random))"
                Enable-Mailbox -Identity $Entry.UserId -Database $db
                }
            else {
                Enable-Mailbox -Identity $Entry.UserId
                } 

Open in new window

...

and

...
        If($Entry.Action -like "reset") { 
            $NewPassword = $Entry.UserId.Insert(5,"!")
            $NewPassword = $newPassword.Insert(3,"k")
            $NewPassword = $newPassword.Remove(0, 1)
            $newPassword = $newPassword.Insert(0,"P")          
            write-host -ForegroundColor yellow -backgroundcolor blue "The new password called $newpassword"
            Set-ADAccountPassword -Identity $Entry.UserId -Reset -NewPassword (ConvertTo-SecureString -AsPlainText $newPassword -Force)	  
            } 

Open in new window

...
0
 
LVL 2

Author Comment

by:Mandy_
ID: 39194420
thank you so much for your help
0
 
LVL 4

Expert Comment

by:mouseware
ID: 39194553
you got it. The only thing to keep in mind is the length of the username/id. If it's less than 5 you'll get an error.

you can pad the username or change the first insert to something less than 5.
0

Join & Write a Comment

Exchange server is not supported in any cloud-hosted platform (other than Azure with Azure Premium Storage).
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
To show how to generate a certificate request 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 Servers >> Certificates…
how to add IIS SMTP to handle application/Scanner relays into office 365.

747 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now