Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell Exchange read department from AD and use the correct database

Posted on 2013-05-21
3
Medium Priority
?
238 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
mouseware earned 2000 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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

A couple of months ago we ran into an issue that necessitated re-creating our Edge Subscriptions. However, when we attempted to execute the command: New-EdgeSubscription -filename C:\NewEdgeSub_01.xml we received an error indicating that the LDAP se…
Wouldn't it be nice if objects in Active Directory automatically moved into the correct Organizational Units? This is what AutoAD aims to do and as a plus, it automatically creates Sites, Subnets, and Organizational Units.
This video discusses moving either the default database or any database to a new volume.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

722 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