Solved

Powershell Exchange read department from AD and use the correct database

Posted on 2013-05-21
3
219 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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Issue with server 2012 R2 and access to folders 20 44
Nano server Question 2 17
GPO help required 12 37
EXCHANGE 2007, EXCHANGE 2013 8 32
This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
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.
how to add IIS SMTP to handle application/Scanner relays into office 365.
This video discusses moving either the default database or any database to a new volume.

912 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

20 Experts available now in Live!

Get 1:1 Help Now