Create Testusers with Mailbox Exchange 2010

PSScript to create users with Mailbox

PSScript to create Users with Mailboxes in the following form:

 

Firstname: FirstnameX

Lastname: LastnameX

Alias: UserX

Mailaddress: UserX@mydomain.dom

 

##############################################################################################################
#
$SCRITPNAME = "MBX1CreateADUsersWithMailbox.ps1"
#
# Purpose:  
#           Creating a Range of Users in the given OU and
#        Mailbox for the user on the given Mailboxstore
#        Default falues can directly be used to generate users in OU Perftest\Test1
#           with Mailboxes for Datastore 'Mailbox Database 0123217039' on MBX1
#
#
# Creation 22.3.2012
# VERSION: 1.0
# Creation 22.3.2012
# Last Change:
#
#  
# Script was created for Powershell 1.0
# This Script was tested on Windows 2k8R2 Enterprise Server SP1
# and actual Hotfixes of End of February 2012
# Exchange 2010 Server SP2
# Script was running with User that is a Member of following Groups
# Administrators, Domain Admins, Domain Users, EnterpriseAdmins,
# Group Policy Creator Owners, Organization Management, Remote Desktop Users
# and Schema Admins
#
#
# Special INFO:
#  
##############################################################################################################
#
#

# defaultfalues defined here

$FQDNdef = "dev3perf.test"
$SMTPDomaindef = "dev3perf.test"
$OUSiteDef = "PerfTest/Test1"        # test1 Ou for MBX1 Users
$MBXDBdef = "Mailbox Database 0123217039"     #DBName of Mailserver


function Run-Main {
    
    Write-Host " $SCRITPNAME on MBX1"

    Write-Host " First Script asks you needed data to create the users"

    #Password only one default for all users
    $password = Read-Host "Enter default password that will be used for all users" -AsSecureString

    #Get the OU
    $OUSite = Read-Host "Enter OU like format PerfTest/Test1 ...."
    if (!($OUSite -like "")) {
        # given OU is used
    }
    else {
        # Default value is used
        $OUSite = $OUSiteDef
    }

    #Get the AD-Domain
    $FQDN = Read-Host "Enter AD FQDN like format dev3perf.test ...."
    if (!($FQDN -like "")) {
        # given AD FQDN is used
    }
    else {
        # Default value is used
        $FQDN = $FQDNdef
    }

    #construction of OU in the AD-Domain
    $OurOrgUnit = "$FQDN/$OUSite"

    [int]$NoOfUsers = Read-Host "Enter Number of Users"
    [int]$NoFirstUser = Read-Host "User(i) Starts with Number=i"


    #Get the SMTP-Domain (sometimes it is the same like the AD-Domain) but not anytime
    $SMTPDomain = Read-Host "Enter SMTP-Domain like format dev3perf.test .... (!!! without the @ symbol !!!)"
    if (!($SMTPDomain -like "")) {
        # given SMTP-Domain is used
    }
    else {
        # Default value is used
        $SMTPDomain = $SMTPDomaindef
    }

    #Get the Exchange Mailbox Store DB
    $MBXDB = Read-Host "Enter the Name of the Exchange Mailbox Store DB like "Mailbox Database 0123217039" ...."
    if (!($MBXDB -like "")) {
        # given MBXDB is used
    }
    else {
        # Default value is used
        $MBXDB = $MBXDBdef
    }


    [int]$iStart = 0
    [int]$iLast = 0

    [int]$iStart = $NoFirstUser  
    [int]$iLast = $NoFirstUser + $NoOfUsers -1


    Write-Host "Proccessing  Creation of $NoOfUsers Users. "
    Write-Host "From User$iStart to User$iLast"
    Write-Host "Emailaddresses User$iStart@$SMTPDomain till User$iLast@$SMTPDomain"
    Write-Host "Database $MBXDB"



    [int]$i = $iStart     

    [int]$NoOfCreatedUsers = 0

    # Do - While Loop to iterrate the Usernames -lt $iLast
    Do {
        Write-Host "Proccessing of No $i"
        $FirstName ="FirstName" + $i
        $LastName = "LastName"+ $i
        $Name = "FirstName" + $i + " LastName" + $i
        $DisplayName = "FirstName"+ $i +" LastName"+ $i
        $Alias = "user" + $i
        $EmailAddress = "User" + $i + "@" + $SMTPDomain

        Write-Host "Proccessing of  $DisplayName with SMTP-Address: $EmailAddress"

        #Create the AD-User and the Mailbox, set the PW
        #and set some Attributes (can not change, does not expire) for the password
        New-Mailbox -UserPrincipalName $EmailAddress -PrimarySmtpAddress $EmailAddress  -Alias $Alias  -Database $MBXDB -Name $Name -OrganizationalUnit $OurOrgUnit -Password $password -FirstName $FirstName  -LastName $LastName -DisplayName $DisplayName -ResetPasswordOnNextLogon $false


        #Set the UserCanNotChangePassword by ADSI
        # not implementet till now
        #Set-UFUserCannotChangePassword($Alias)    

        #Process Nextone
        $i =$i+1
        $NoOfCreatedUsers = $NoOfCreatedUsers +1

    } While (($i -le $iLast))


    Write-Host "$NoOfCreatedUsers Users are created in  $MBXDB !"

}



#################################################################################################
# Additional Function UserFriendly  SetUserCannotChangePassword
#
# Parameter: Users Alias (logonname)
#################################################################################################
#
Function Set-UFUserCannotChangePassword
{
    Param
    (
        [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]
        [String]$UserAlias
    )

    [ADSI]$UsersLdapPath = Get_LdapPath ($UserAlias)
     Set-UserCannotChangePassword -ADPath $UsersLdapPath

}


#################################################################################################
# Additional Function that returns the LDAP Path
# Parameter the Users Alias (logonname)
#################################################################################################
#
Function Get_LdapPath
{
    Param
    (
        [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]
        [String]$UserAlias
    )
    $UserName = $UserAlias

    $searcher = new-object DirectoryServices.DirectorySearcher([ADSI]“”)

    $searcher.filter = “(&(objectClass=user)(sAMAccountName= $UserName))”

    $founduser = $searcher.findOne()

    $P = $founduser | select path
    $p.path
}






#################################################################################################
# Additional Function that sets the UserCannotChangePassword
# http://msdn.microsoft.com/en-us/library/aa746398%28VS.85%29.aspx
#################################################################################################
#
Function Set-UserCannotChangePassword
{
    <#
     .Synopsis
      Sets the attribute 'User Cannot Change Password' on a given account.
    .Description
     Sets the attribute 'User Cannot Change Password' on a given account.
    .Parameter ADPath
     The full AD Path of the User
     .Example
     PS> Set-UserCannotChangePassword -ADPath 'LDAP://cn=Adam,ou=TestOU,dc=Test,dc=Com'
    .Notes
     NAME:      Set-UserCannotChangePassword
     AUTHOR:    Allan Rogers
    #>
    Param
    (
        [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]
        [String]$ADPath
    )
    # --- Get the User
    $User = [ADSI]($ADPath)
    if ($User.Path)
    {
        # --- Set the Security Objects
        $Everyone = [System.Security.Principal.SecurityIdentifier]'S-1-1-0'
        $EveryoneDeny = New-Object System.DirectoryServices.ActiveDirectoryAccessRule ($Everyone, `
                                'Extendedright', 'Deny', [GUID]'ab721a53-1e2f-11d0-9819-00aa0040529b')
        $Self = [System.Security.Principal.SecurityIdentifier]'S-1-5-10'
        $SelfDeny = New-Object System.DirectoryServices.ActiveDirectoryAccessRule ($Self,`
                                 'Extendedright', 'Deny', [GUID]'ab721a53-1e2f-11d0-9819-00aa0040529b')
        # --- Apply the Settings to the User
        $User.psbase.get_ObjectSecurity().AddAccessRule($SelfDeny)
        $User.psbase.get_ObjectSecurity().AddAccessRule($EveryoneDeny)
        $User.psbase.CommitChanges()
    }
    else
    {
        throw "Function Set-UserCannotChangePassword failed to get user at '$ADPath'"
    }
}


$script:ErrorActionPreference = "silentlyContinue"
Write-Host " ********************************************************************** "
Write-Host " ********* PROCESSING SCRIPT $SCRIPTNAME ********* "
Write-Host " ********************************************************************** "
Run-Main
trap {
write-host ("ERROR 0xK7VD3AB occurred "+$_.exception.message)
}
Write-Host " ***************************************************************************** "
Write-Host " ********* END OF PROCESSING SCRIPT $SCRITPNAME ********* "
Write-Host " ***************************************************************************** "

 

 

 

 

2. PSScript to delete a range of Users and the mailbox

##############################################################################################################
#
$SCRITPNAME = "MBX1DeleteADUsersWithMailbox.ps1"
#
# Purpose:
#           Deletes a Range of Users and the Mailbox of the User
#              Default falues are set for Users in OU 'PerfTest/Test1'
#              in Mailboxstore     'Mailbox Database 0123217039' of MBX1
#
# Creation 22.3.2012
# VERSION: 1.0
# Creation 22.3.2012
# Last Change:
#
# Script was created for Powershell 1.0
# This Script was tested on Windows 2k8R2 Enterprise Server SP1
# and actual Hotfixes of End of February 2012
# Exchange 2010 Server SP2
# Script was running with User that is a Member of following Groups
# Administrators, Domain Admins, Domain Users, EnterpriseAdmins,
# Group Policy Creator Owners, Organization Management, Remote Desktop Users
# and Schema Admins
#
#
# Special INFO:
#
#  The automatic variable $ConfirmPreference determines the ConfirmImpact level
#  that will trigger automatic confirmation. Valid values are None, Low, Medium and High.
#  If you set this variable to "Low", you will get confirmations for all cmdlets and
#  all functions supporting confirmation. If you set it to "None",
#  you will never get automatic confirmations.
#   
#  If you must run commands unattended and want to make sure no implicit confirmation
#  gets in your way and blocks the script, you can either set $ConfirmPreference to "None",
#  or you can explicitly assign a $false to your -Confirm parameter.
##############################################################################################################
#
#


# defaultfalues defined here

$FQDNdef = 'dev3perf.test'
$SMTPDomaindef = 'dev3perf.test'
$OUSiteDef = 'PerfTest/Test1'        # test1 Ou for MBX1 Users
$MBXDBdef = 'Mailbox Database 0123217039'     #DBName of Mailserver

function Run-Main {

    Write-Host " $SCRITPNAME on MBX1"

    Write-Host " First Script asks you needed data to delete the users"

    #Password only one default for all users
    #$password = Read-Host "Enter default password that will be used for all users" -AsSecureString

    #Get the OU
    $OUSiteDef = Read-Host "Enter OU like format PerfTest/Test1 ...."
    if (!($OUSiteDef -like "")) {
        # given OU is used
    }
    else {
        # Default value is used
        $OUSiteDef = $OUSiteDef
    }

    #Get the AD-Domain
    $FQDN = Read-Host "Enter AD FQDN like format dev3perf.test ...."
    if (!($FQDN -like "")) {
        # given AD FQDN is used
    }
    else {
        # Default value is used
        $FQDN = $FQDNdef
    }

    #construction of OU in the AD-Domain
    $OurOrgUnit = "$FQDN/$OUSiteDef"

    [int]$NoOfUsers = Read-Host "Enter Number of Users to delete"
    
    [int]$NoFirstUser = Read-Host "First User(i) to delete Starts with Number=i"

    #Get the SMTP-Domain (sometimes it is the same like the AD-Domain) but not anytime
    $SMTPDomain = Read-Host "Enter SMTP-Domain like format dev3perf.test .... (!!! without the @ symbol !!!)"
    if (!($SMTPDomain -like "")) {
        # given SMTP-Domain is used
    }
    else {
        # Default value is used
        $SMTPDomain = $SMTPDomaindef
    }

    #Get the Exchange Mailbox Store DB
    $MBXDB = Read-Host "Enter the Name of the Exchange Mailbox Store DB like "Mailbox Database 0123217039" ...."
    if (!($MBXDB -like "")) {
        # given MBXDB is used
    }
    else {
        # Default value is used
        $MBXDB = $MBXDBdef
    }


    [int]$iStart = 0
    [int]$iLast = 0

    [int]$iStart = $NoFirstUser  
    [int]$iLast = $NoFirstUser + $NoOfUsers -1


    Write-Host "Proccessing  Deletion of $NoOfUsers Users. "
    Write-Host "From User$iStart to User$iLast"
    Write-Host "Emailaddresses User$iStart@$SMTPDomain till User$iLast@$SMTPDomain"
    Write-Host "Database $MBXDB"


    [int]$i = $iStart     

    [int]$NoOfDeletedUsers = 0

    # Do - While Loop to iterrate the Usernames -lt $iLast
    Do {
        Write-Host "Proccessing of No $i"
        $FirstName ="FirstName" + $i
        $LastName = "LastName"+ $i
        $Name = "FirstName" + $i + " LastName" + $i
        $DisplayName = "FirstName"+ $i +" LastName"+ $i
        $Alias = "user" + $i
        $EmailAddress = "User" + $i + "@" + $SMTPDomain

        Write-Host "Proccessing of  $DisplayName with SMTP-Address: $EmailAddress"

        #Delete the AD-User and the Mailbox
        #Remove-Mailbox -Identity $EmailAddress -Confirm:$false -Permanent $true

        #Remove-Mailbox -UserPrincipalName $EmailAddress -Database  -Name $Name -OrganizationalUnit $OurOrgUnit -Password $password -FirstName $FirstName -LastName $LastName -DisplayName $DisplayName -ResetPasswordOnNextLogon $false
        #$Temp = Get-MailboxStatistics | Where {$_.DisplayName -eq '$DisplayName'}
        #Remove-Mailbox -Database $MBXDB -StoreMailboxIdentity $Temp.MailboxGuid -Permanent $true
        #dsrm /?
        $UserInfo = Get-User -identity $Alias
        $ObjectDN = $UserInfo.DistinguishedName
        Write-Host "Proccessing of Deletion $ObjectDN"
        dsrm $ObjectDN -noprompt
        
        
        #Process Nextone
        $i =$i+1
        $NoOfDeletedUsers = $NoOfDeletedUsers +1

    } While (($i -le $iLast))


    Write-Host "$NoOfDeletedUsers Users are deleted from  $MBXDB and in Domain $FQDN !"

}

$script:ErrorActionPreference = "silentlyContinue"
Write-Host " ********************************************************************** "
Write-Host " ********* PROCESSING SCRIPT $SCRITPNAME ********* "
Write-Host " ********************************************************************** "
Run-Main
trap {
write-host ("ERROR 0xK7VD3AB occurred "+$_.exception.message)
}
Write-Host " ***************************************************************************** "
Write-Host " ********* END OF PROCESSING SCRIPT $SCRITPNAME ********* "
Write-Host " ***************************************************************************** "

 

 

 

 

Create Users with filled 0

##############################################################################################################
#
$SCRITPNAME = "MBX1CreateADUsersWithMailbox.ps1"
#
# Purpose:  
#           Creating a Range of Users in the given OU and
#        Mailbox for the user on the given Mailboxstore
#        Default falues can directly be used to generate users in OU Perftest\Test1
#           with Mailboxes for Datastore 'Mailbox Database 01234567891' on MBX1
#
#
# Creation 22.3.2012
# VERSION: 1.0
# Creation 22.3.2012
# Last Change: 17.4.2012 by Me
#            added function "strFuellenMitNullen" to make the User more readable and searchable
#
#  
# Script was created for Powershell 1.0
# This Script was tested on Windows 2k8R2 Enterprise Server SP1
# and actual Hotfixes of End of February 2012
# Exchange 2010 Server SP2
# Script was running with User that is a Member of following Groups
# Administrators, Domain Admins, Domain Users, EnterpriseAdmins,
# Group Policy Creator Owners, Organization Management, Remote Desktop Users
# and Schema Admins
#
#
# Special INFO:
#  
##############################################################################################################
#
#

# defaultfalues defined here

$FQDNdef = "mydom.test"
$SMTPDomaindef = "mydom.test"
$OUSiteDef = "PerfTest/Test1"        # test1 Ou for MBX1 Users
$MBXDBdef = "Mailbox Database 01234567891"     #DBName of Mailserver


function Run-Main {
    
    Write-Host " $SCRITPNAME on MBX1"

    Write-Host " First Script asks you needed data to create the users"

    #Password only one default for all users
    $password = Read-Host "Enter default password that will be used for all users" -AsSecureString

    #Get the OU
    $OUSite = Read-Host "Enter OU like format PerfTest/Test1 ...."
    if (!($OUSite -like "")) {
        # given OU is used
    }
    else {
        # Default value is used
        $OUSite = $OUSiteDef
    }

    #Get the AD-Domain
    $FQDN = Read-Host "Enter AD FQDN like format mydom.test ...."
    if (!($FQDN -like "")) {
        # given AD FQDN is used
    }
    else {
        # Default value is used
        $FQDN = $FQDNdef
    }

    #construction of OU in the AD-Domain
    $OurOrgUnit = "$FQDN/$OUSite"

    [int]$NoOfUsers = Read-Host "Enter Number of Users"
    [int]$NoFirstUser = Read-Host "User(i) Starts with Number=i"


    #Get the SMTP-Domain (sometimes it is the same like the AD-Domain) but not anytime
    $SMTPDomain = Read-Host "Enter SMTP-Domain like format mydom.test .... (!!! without the @ symbol !!!)"
    if (!($SMTPDomain -like "")) {
        # given SMTP-Domain is used
    }
    else {
        # Default value is used
        $SMTPDomain = $SMTPDomaindef
    }

    #Get the Exchange Mailbox Store DB
    $MBXDB = Read-Host "Enter the Name of the Exchange Mailbox Store DB like "Mailbox Database 01234567891" ...."
    if (!($MBXDB -like "")) {
        # given MBXDB is used
    }
    else {
        # Default value is used
        $MBXDB = $MBXDBdef
    }


    [int]$iStart = 0
    [int]$iLast = 0

    [int]$iStart = $NoFirstUser  
    [int]$iLast = $NoFirstUser + $NoOfUsers -1


    Write-Host "Proccessing  Creation of $NoOfUsers Users. "
    Write-Host "From User$iStart to User$iLast"
    Write-Host "Emailaddresses User$iStart@$SMTPDomain till User$iLast@$SMTPDomain"
    Write-Host "Database $MBXDB"



    [int]$i = $iStart     

    [int]$NoOfCreatedUsers = 0

    # Do - While Loop to iterrate the Usernames -lt $iLast
    Do {
        $strFueller = strFuellenMitNullen($i)
        Write-Host "Proccessing of No $i"
        $FirstName ="FirstName" + $strFueller
        $LastName = "LastName"+ $strFueller
        $Name = "FirstName" + $strFueller + " LastName" + $strFueller
        $DisplayName = "FirstName"+ $strFueller +" LastName"+ $strFueller
        $Alias = "user" + $strFueller
        $EmailAddress = "User" + $strFueller + "@" + $SMTPDomain

        Write-Host "Proccessing of  $DisplayName with SMTP-Address: $EmailAddress"

        #Create the AD-User and the Mailbox, set the PW
        #and set some Attributes (can not change, does not expire) for the password
        New-Mailbox -UserPrincipalName $EmailAddress -PrimarySmtpAddress $EmailAddress  -Alias $Alias  -Database $MBXDB -Name $Name -OrganizationalUnit $OurOrgUnit -Password $password -FirstName $FirstName  -LastName $LastName -DisplayName $DisplayName -ResetPasswordOnNextLogon $false


        #Set the UserCanNotChangePassword by ADSI
        # not implementet till now
        #Set-UFUserCannotChangePassword($Alias)    

        #Process Nextone
        $i =$i+1
        $NoOfCreatedUsers = $NoOfCreatedUsers +1

    } While (($i -le $iLast))


    Write-Host "$NoOfCreatedUsers Users are created in  $MBXDB !"

}



#################################################################################################
# Additional Function UserFriendly  SetUserCannotChangePassword
#
# Parameter: Users Alias (logonname)
#################################################################################################
#
Function Set-UFUserCannotChangePassword
{
    Param
    (
        [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]
        [String]$UserAlias
    )

    [ADSI]$UsersLdapPath = Get_LdapPath ($UserAlias)
     Set-UserCannotChangePassword -ADPath $UsersLdapPath

}


#################################################################################################
# Additional Function that returns the LDAP Path
# Parameter the Users Alias (logonname)
#################################################################################################
#
Function Get_LdapPath
{
    Param
    (
        [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]
        [String]$UserAlias
    )
    $UserName = $UserAlias

    $searcher = new-object DirectoryServices.DirectorySearcher([ADSI]“”)

    $searcher.filter = “(&(objectClass=user)(sAMAccountName= $UserName))”

    $founduser = $searcher.findOne()

    $P = $founduser | select path
    $p.path
}






#################################################################################################
# Additional Function that sets the UserCannotChangePassword
# http://msdn.microsoft.com/en-us/library/aa746398%28VS.85%29.aspx
#################################################################################################
#
Function Set-UserCannotChangePassword
{
    <#
     .Synopsis
      Sets the attribute 'User Cannot Change Password' on a given account.
    .Description
     Sets the attribute 'User Cannot Change Password' on a given account.
    .Parameter ADPath
     The full AD Path of the User
     .Example
     PS> Set-UserCannotChangePassword -ADPath 'LDAP://cn=Adam,ou=TestOU,dc=Test,dc=Com'
    .Notes
     NAME:      Set-UserCannotChangePassword
     AUTHOR:    Allan Rogers
    #>
    Param
    (
        [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]
        [String]$ADPath
    )
    # --- Get the User
    $User = [ADSI]($ADPath)
    if ($User.Path)
    {
        # --- Set the Security Objects
        $Everyone = [System.Security.Principal.SecurityIdentifier]'S-1-1-0'
        $EveryoneDeny = New-Object System.DirectoryServices.ActiveDirectoryAccessRule ($Everyone, `
                                'Extendedright', 'Deny', [GUID]'ab721a53-1e2f-11d0-9819-00aa0040529b')
        $Self = [System.Security.Principal.SecurityIdentifier]'S-1-5-10'
        $SelfDeny = New-Object System.DirectoryServices.ActiveDirectoryAccessRule ($Self,`
                                 'Extendedright', 'Deny', [GUID]'ab721a53-1e2f-11d0-9819-00aa0040529b')
        # --- Apply the Settings to the User
        $User.psbase.get_ObjectSecurity().AddAccessRule($SelfDeny)
        $User.psbase.get_ObjectSecurity().AddAccessRule($EveryoneDeny)
        $User.psbase.CommitChanges()
    }
    else
    {
        throw "Function Set-UserCannotChangePassword failed to get user at '$ADPath'"
    }
}


#################################################################################################
# Additional Function that delivers a string with no of 0 to fill the Username and
#         make the User more readable and searchable
#
#################################################################################################
#

function strFuellenMitNullen([int]$i)  
{
    if ($i -lt 10){
        $strFuellen = "00000" + $i  # user000001-user000009
    }
    elseif ($i -lt 100){
                $strFuellen = "0000" + $i  # user000010-user000099
    }
    elseif ($i -lt 1000){
         $strFuellen = "000" + $i # user000100-user000999
    }
        elseif ($i -lt 10000){
              $strFuellen = "00" + $i  # user001000-user009999
    }
    elseif ($i -lt 100000){
                 $strFuellen = "0" + $i     # user010000-user099999
    }
    else {
                        $strFuellen = "" + $i  # user100000-user999999
    }
    
    return $strFuellen    
}



$script:ErrorActionPreference = "silentlyContinue"
Write-Host " ********************************************************************** "
Write-Host " ********* PROCESSING SCRIPT $SCRIPTNAME ********* "
Write-Host " ********************************************************************** "
Run-Main
trap {
write-host ("ERROR 0xK7VD3AB occurred "+$_.exception.message)
}
Write-Host " ***************************************************************************** "
Write-Host " ********* END OF PROCESSING SCRIPT $SCRITPNAME ********* "
Write-Host " ***************************************************************************** "

HowTo get the Exchange Server Version (2007/2010)

Get-ExchangeServer | fl Name,AdminDisplayVersion

 

oder besser mit dem folgenden PS Skript:

 

---------------------------------------------------------------------------------------------------------------------------

# Get Exchange Version
# Useable for Exchange 2k7/2k10
 



#$hklmkey = Get-Item HKLM:\Software\Microsoft\Exchange\Setup\
#$hklmkey = Get-Item HKLM:\Software\Microsoft\ExchangeServer\v14\Setup\

#Check Registry Keys
If (test-path HKLM:\Software\Microsoft\ExchangeServer\v14\Setup\)
    {
        $hklmkey = Get-Item HKLM:\Software\Microsoft\ExchangeServer\v14\Setup\
    }
else
    {
        If (test-path HKLM:\Software\Microsoft\Exchange\Setup\)
            {
                $hklmkey = Get-Item HKLM:\Software\Microsoft\Exchange\Setup\
            }
       Else
       {
       Write-Host ("Exchange Path in Regsistry nicht gefunden :(")
       }
    }

#Get Installation Path
$values = Get-ItemProperty $hklmkey.PSPath
$Path = $values.MsiInstallPath
$FullPath = $Path + "bin\exsetup.exe"

Write-Host ("Pfad: " + $FullPath)

#Get Version
$FileInfo = GCM $FullPath |%{$_.Fileversioninfo}
$ProductVersion = $FileInfo.ProductVersion

Write-Host("Exchange Version von exsetup.exe: " + $ProductVersion)

---------------------------------------------------------------------------------------------------------------------------

Buildnummern des Exchange Servers:

http://social.technet.microsoft.com/wiki/contents/articles/240.exchange-server-and-update-rollups-build-numbers.aspx

HowTo get the number of private Contact Items of all Users

##############################################################################################################
#
$SCRITPNAME = "CountNumberOfContacts.ps1"
#
# Purpose:  
#           Counts the number of Contacts for each User
#
#
# Creation 19.06.2012
# VERSION: 1.0
# Last Change: 19.06.2012 by Norbert
#
#
#  
# Script was created for Powershell 1.0
# This Script was tested on Windows 2k8R2 Enterprise Server SP1
# and actual Hotfixes of End of February 2012
# Exchange 2010 Server SP2
# Script was running with User that is a Member of following Groups
# Administrators, Domain Admins, Domain Users, EnterpriseAdmins,
# Group Policy Creator Owners, Organization Management, Remote Desktop Users
# and Schema Admins
#
#
# Special INFO:
#  
##############################################################################################################
#
#

# defaultfalues defined here
$FQDNdef = "mydom.test"
$SMTPDomaindef = "mydom.test"
$OUSiteDef = "PerfTest/Test1"        # test1 Ou for MBX1 Users
$MBXDBdef = "Mailbox Database 01234567891"     #DBName of Mailserver


function Run-Main {
    
    Write-Host " $SCRITPNAME on MBX1"

    Write-Host " First Script asks you needed data to create the users"

    #Password only one default for all users
    $password = Read-Host "Enter default password that will be used for all users" -AsSecureString

    #Get the OU
    $OUSite = Read-Host "Enter OU like format PerfTest/Test1 ...."
    if (!($OUSite -like "")) {
        # given OU is used
    }
    else {
        # Default value is used
        $OUSite = $OUSiteDef
    }

    #Get the AD-Domain
    $FQDN = Read-Host "Enter AD FQDN like format dev3perf.test ...."
    if (!($FQDN -like "")) {
        # given AD FQDN is used
    }
    else {
        # Default value is used
        $FQDN = $FQDNdef
    }

    #construction of OU in the AD-Domain
    $OurOrgUnit = "$FQDN/$OUSite"

    [int]$NoOfUsers = Read-Host "Enter Number of Users"
    [int]$NoFirstUser = Read-Host "User(i) Starts with Number=i"


    #Get the SMTP-Domain (sometimes it is the same like the AD-Domain) but not anytime
    $SMTPDomain = Read-Host "Enter SMTP-Domain like format dev3perf.test .... (!!! without the @ symbol !!!)"
    if (!($SMTPDomain -like "")) {
        # given SMTP-Domain is used
    }
    else {
        # Default value is used
        $SMTPDomain = $SMTPDomaindef
    }

    #Get the Exchange Mailbox Store DB
    $MBXDB = Read-Host "Enter the Name of the Exchange Mailbox Store DB like "Mailbox Database 0123217039" ...."
    if (!($MBXDB -like "")) {
        # given MBXDB is used
    }
    else {
        # Default value is used
        $MBXDB = $MBXDBdef
    }


    [int]$iStart = 0
    [int]$iLast = 0

    [int]$iStart = $NoFirstUser  
    [int]$iLast = $NoFirstUser + $NoOfUsers -1




    [int]$i = $iStart     

    [int]$NoOfCreatedUsers = 0


    $mbxContactscombCollection = @()

    # Do - While Loop to iterrate the Usernames -lt $iLast
    Do {
        
        
        $strFueller = strFuellenMitNullen($i)
        Write-Host "Proccessing of No $i"
        $FirstName ="FirstName" + $strFueller
        $LastName = "LastName"+ $strFueller
        $Name = "FirstName" + $strFueller + " LastName" + $strFueller
        $DisplayName = "FirstName"+ $strFueller +" LastName"+ $strFueller
        $Alias = "user" + $strFueller
        Write-Host "Proccessing of  $DisplayName with Alias: $Alias"
        Get-MailboxFolderStatistics -Identity $Alias -FolderScope Contacts | Select identity, name, itemsInFolder
        $mbxContactsFolderStats = Get-MailboxFolderStatistics -Identity $Alias -FolderScope Contacts |Select identity, name, itemsInFolder
        $mbcomb = "" | Select "alias", "identity" , "name", "itemsInFolder"
        $mbcomb.alias = $Alias
        $mbcomb.identity = $mbxContactsFolderStats.identity
        $mbcomb.name = $mbxContactsFolderStats.name
        $mbcomb.itemsInFolder = $mbxContactsFolderStats.itemsInFolder
        
        $mbxContactscombCollection += $mbcomb

        #Process Nextone
        $i =$i+1
        $NoOfCreatedUsers = $NoOfCreatedUsers +1

    } While (($i -le $iLast))

    #$$mbxContactscombCollection
    $mbxContactscombCollection | Export-Csv E:\Reports\"ArchiveStats_$(Get-Date -f 'yyyyMMdd').csv" -NoType
    Write-Host " Statistics for $NoOfCreatedUsers Users are created in  csv-file ArchiveStats_$yyyyMMdd.csv!"

}



#################################################################################################
# Additional Function UserFriendly  SetUserCannotChangePassword
#
# Parameter: Users Alias (logonname)
#################################################################################################
#
Function Set-UFUserCannotChangePassword
{
    Param
    (
        [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]
        [String]$UserAlias
    )

    [ADSI]$UsersLdapPath = Get_LdapPath ($UserAlias)
     Set-UserCannotChangePassword -ADPath $UsersLdapPath

}


#################################################################################################
# Additional Function that returns the LDAP Path
# Parameter the Users Alias (logonname)
#################################################################################################
#
Function Get_LdapPath
{
    Param
    (
        [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]
        [String]$UserAlias
    )
    $UserName = $UserAlias

    $searcher = new-object DirectoryServices.DirectorySearcher([ADSI]“”)

    $searcher.filter = “(&(objectClass=user)(sAMAccountName= $UserName))”

    $founduser = $searcher.findOne()

    $P = $founduser | select path
    $p.path
}






#################################################################################################
# Additional Function that sets the UserCannotChangePassword
# http://msdn.microsoft.com/en-us/library/aa746398%28VS.85%29.aspx
#################################################################################################
#
Function Set-UserCannotChangePassword
{
    <#
     .Synopsis
      Sets the attribute 'User Cannot Change Password' on a given account.
    .Description
     Sets the attribute 'User Cannot Change Password' on a given account.
    .Parameter ADPath
     The full AD Path of the User
     .Example
     PS> Set-UserCannotChangePassword -ADPath 'LDAP://cn=Adam,ou=TestOU,dc=Test,dc=Com'
    .Notes
     NAME:      Set-UserCannotChangePassword
     AUTHOR:    Allan Rogers
    #>
    Param
    (
        [Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()]
        [String]$ADPath
    )
    # --- Get the User
    $User = [ADSI]($ADPath)
    if ($User.Path)
    {
        # --- Set the Security Objects
        $Everyone = [System.Security.Principal.SecurityIdentifier]'S-1-1-0'
        $EveryoneDeny = New-Object System.DirectoryServices.ActiveDirectoryAccessRule ($Everyone, `
                                'Extendedright', 'Deny', [GUID]'ab721a53-1e2f-11d0-9819-00aa0040529b')
        $Self = [System.Security.Principal.SecurityIdentifier]'S-1-5-10'
        $SelfDeny = New-Object System.DirectoryServices.ActiveDirectoryAccessRule ($Self,`
                                 'Extendedright', 'Deny', [GUID]'ab721a53-1e2f-11d0-9819-00aa0040529b')
        # --- Apply the Settings to the User
        $User.psbase.get_ObjectSecurity().AddAccessRule($SelfDeny)
        $User.psbase.get_ObjectSecurity().AddAccessRule($EveryoneDeny)
        $User.psbase.CommitChanges()
    }
    else
    {
        throw "Function Set-UserCannotChangePassword failed to get user at '$ADPath'"
    }
}


#################################################################################################
# Additional Function that delivers a string with no of 0 to fill the Username and
#         make the User more readable and searchable
#
#################################################################################################
#

function strFuellenMitNullen([int]$i)  
{
    if ($i -lt 10){
        $strFuellen = "00000" + $i  # user000001-user000009
    }
    elseif ($i -lt 100){
                $strFuellen = "0000" + $i  # user000010-user000099
    }
    elseif ($i -lt 1000){
         $strFuellen = "000" + $i # user000100-user000999
    }
        elseif ($i -lt 10000){
              $strFuellen = "00" + $i  # user001000-user009999
    }
    elseif ($i -lt 100000){
                 $strFuellen = "0" + $i     # user010000-user099999
    }
    else {
                        $strFuellen = "" + $i  # user100000-user999999
    }
    
    return $strFuellen    
}










$script:ErrorActionPreference = "silentlyContinue"
Write-Host " ********************************************************************** "
Write-Host " ********* PROCESSING SCRIPT $SCRIPTNAME ********* "
Write-Host " ********************************************************************** "
Run-Main
trap {
write-host ("ERROR 0xK7VD3AB occurred "+$_.exception.message)
}
Write-Host " ***************************************************************************** "
Write-Host " ********* END OF PROCESSING SCRIPT $SCRITPNAME ********* "
Write-Host " ***************************************************************************** "