Solved

Powershell Exchange read department from AD and use the correct database

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
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…

756 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