Solved

Powershell Exchange read department from AD and use the correct database

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

This article runs through the process of deploying a single EXE application selectively to a group of user.
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…

777 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