Script to Create Database and Database Copies

Scenario:  You would like to create Databases and Database Copies via a script. For each database, you want to set the Quotas for Send, Send/Receive, and Warning to unlimited as well.

Solution:

Create a db.csv file with the following column headers: DB,1,2,3,4.  In that CSV File, put the name of the DB in the DB column, and each server in its numerical location based on Activation Preference:

db,1,2,3,4
DB1,Ex2013Srv1,Ex2013Srv2,Ex2013Srv3,Ex2013Srv4
DB2,Ex2013Srv2,Ex2013Srv3,Ex2013Srv4,Ex2013Srv1
DB3,Ex2013Srv3,Ex2013Srv4,Ex2013Srv1,Ex2013Srv2
DB4,Ex2013Srv4,Ex2013Srv1,Ex2013Srv2,Ex2013Srv3

Then run the script:

#Import the CSV File into a Variable
$DBs = Import-CSV C:tempDb.csv

#Loop the Variable
$DBs | %{
#Declare variables for each column
$n = $_.DB
$1 = $_.1
$2 = $_.2
$3 = $_.3
$4 = $_.4

#Create the DB
Write-Host "Creating $n"
New-MailboxDatabase $n -EDBFilePath C:$nDB$n.edb -LogFolderPath C:$nLogs -Server $1 

#Wait for replication
Write-Host "Sleeping 3 minutes for replication"
Sleep 180

#Mount the Database
Write-Host "Mounting $n"
Mount-Database $n

#Wait for additional replication
Write-Host "Sleeping 3 minutes for replication"
Sleep 180

#Setting the DB Quotas
Write-Host "Setting DB Quotas to unlimited"
Set-MailboxDatabase $n -ProhibitSendReceiveQuota Unlimited -ProhibitSendQuota Unlimited -IssueWarningQuota Unlimited

#Create the additional Database Copies
Write-Host "Creating ActivationPreference2 for $n"
Add-MailboxDatabaseCopy $n -MailboxServer $2 -ActivationPreference 2
Write-Host "Creating ActivationPreference3 for $n"
Add-MailboxDatabaseCopy $n -MailboxServer $3 -ActivationPreference 3
Write-Host "Creating ActivationPreference4 for $n"
Add-MailboxDatabaseCopy $n -MailboxServer $4 -ActivationPreference 4
}

 

 

 

 

Configuring UploadReadAheadSize for Certificate Based Authentication for ActiveSync

Scenario: When configuring Certificate Based Authentication, you will have to configure the UploadReadAheadSize property in IIS to  allow message content greater than 48K.  Without the UploadReadAheadSize properly set, some symptoms you may experience are HTTP Status codes of 413 for ActiveSync Requests in the IIS logs  and mobile devices may experience size errors when attempting to send email.

Solution:

How to set the uploadReadAheadSize in IIS 7.5

  1. Launch “Internet Information Services (IIS) Manager”
  2. Expand the Server field
  3. Expand Sites
  4. Expand Default Web Site
  5. Click on Microsoft-Server-ActiveSync
  6. In the Features section, double click “Configuration Editor”
  7. Under “Section” select: system.webServer>serverRuntime
  8. Modify the “uploadReadAheadSize” section to 36700160 for 35MB.
  9. Click Apply

 

Exchange Script to collect the Alias and the LastLogonTime for a list of users.

Scenario:  You have a list of mailboxes in a csv file that you need to check the lastlogontime property for each mailbox.

Solution:

#Import CSV
$1 = Import-csv C:tempusers.csv

#Create the Variable
$final = @()

#Loop through your users.
$1 | %{
$alias = $_.name
$2 = Get-mailboxStatistics $_.name | Select DisplayName, LastLogonTime
$disp = $2.DisplayName
$LastLogon = $2.LastLogonTime

#Build the Array
 $ServerObj = New-Object PSObject
 $ServerObj | Add-Member NoteProperty -Name "Alias" -Value $alias
 $ServerObj | Add-Member NoteProperty -Name "DisplayName" -Value $disp
 $ServerObj | Add-Member NoteProperty -Name "LastLogonTime" -Value $lastlogon
     $Final += $ServerObj    
}

$final | Export-csv C:tempresults.csv

 

Exchange PowerShell to check to see if a list of mailboxes exist.

Scenario:  You have a list of mailboxes that you want to check to see if a mailbox exists for each user.  You want to output it to a table with a true or false.

Resolution: In my users.csv that I import, the column that I reference in this script has a column name labeled ‘name’. Note: If a mailbox doesn’t exist, it may display with “The operation couldn’t be performed because object couldn’t be found” in the Powershell window.  The $Final variable will have a clean list with true or false for each mailbox.

#Create the Variable
$final = @()

#Import the CSV file with a column labeled 'name'.
$1 = Import-CSV C:tempusers.csv
 
#Loop it for each entry
$1 | %{
$n = $_.name
$r = [bool](get-mailbox "$n")

#Build the Array
 $ServerObj = New-Object PSObject
 $ServerObj | Add-Member NoteProperty -Name "User" -Value $n
 $ServerObj | Add-Member NoteProperty -Name "Exists" -Value $r
     $Final += $ServerObj    
}

#Display the Results
$Final

#Export the CSV to a file
$Final | Export-csv C:tempResults_Users.csv

Use PowerShell to determine lastbootup time of a server or multiple servers

scenarios:  You want to quickly determine the bootup time for your Exchange Servers via Powershell.

Resolution: Run the following:

#For a Single Server:
Get-WmiObject win32_operatingsystem -computername ExSvr1 | select csname, @{LABEL='LastBootUpTime';EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}


#For Multiple Servers:
$1 = Get-ExchangeServer ExSvr*
$1 | %{Get-WmiObject win32_operatingsystem -computername $_.name | select csname, @{LABEL='LastBootUpTime';EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}}

Setting AD properties via PowerShell for Active Directory

Scenario:  You want to set the Office property for a user account via PowerShell for Active Directory

Solution:

To set a single User for the property Office:
Get-ADUser testusr1 -Properties * | Set-ADObject -Replace @{Office = “test”}

To check a single User for the property Office:
Get-ADUser testusr1 -Properties * | Select name, Office

To clear the value for Office:
Get-ADUser testusr1 -Properties * | Set-ADObject -Clear Office
To set multiple users via script: In the CSV file, have two columns; 1. One column for Name and 2. One column for the Office.

$1 = Import-Csv C:tempOfficeDataFile.csv
$1 | %{ $2 = $_.Office; Get-ADUser $_.name -Properties Office | Set-ADObject -Replace @{Office =”$2”} }

To check multiple users via a script

$1 = Import-Csv C:tempOfficeDataFile.csv
$1 | %{ Get-ADUser $_.name -Properties Office | Select name, Office}

Use PowerShell to search through multiple log files for specific text and export the results

Scenario:  You have multiple log/txt files you need to search through for specific text.  You would like to export/dump the text into another file.

Solution:  Run the following PowerShell Script.  Note the pattern contains the word you are looking for.  Unlike the “FIND” function in the command prompt, the PowerShell search does not require an exact case sensitive match.

#Look for any lines that has a text Pattern of "Fail" on it.
$1 = Get-ChildItem c:Temp*.log | Select-String -Pattern "Fail"

#For each line, export it to a csv.
$1 | Select line | Export-csv C:temptestline.csv

Copy and then Rename files via PowerShell

Scenario:  You want to collect logs from various servers and place the copied logs into a single directory.  Due to the log names being the same on each server, we want avoid overwriting existing logs. We also want to know the server from where each log was copied from.

Solution:  The following will copy the IMAP logs from 4 servers into 1 local directory. Each file when copied to the directory will be renamed by prefixing the file with the server name.  It will also add the counter to the end of the file.

$Servers = "ExSvr1","ExSvr2","ExSvr3","ExSvr4"
$servers | %{$File = Get-ChildItem -Path "\$_c$Program FilesMicrosoftExchange ServerV15LoggingIMAP4" -Recurse;$i=1;Foreach ($f in $File) {Copy-Item $f.FullName ("C:TempIMAPPOP$_" + $f.BaseName + $i +".log");$i++}}
#End

 

 

 

Export a list of Distribution Group Members for multiple Distribution Groups into a single file.

Scenario:  You are asked to find all distribution group that starts with  ACCM* and then export the distribution group name and its members of each distribution group into a single file.

Solution: The script below finds each member of each distribution group and exports to a csv the groups name, mbx alias, first name, last name, department, and title on each line.

$final = @()

$1 = Get-DistributionGroup ACCM*

$1 | %{ 
write-host $_.DisplayName
$ListName = $_.Name
$ListDisplayName = $_.DisplayName
$2 = Get-distributionGroupMember $listName
 $2 | %{
 $MbxAlias = $_.Alias
 $MBxFirst = $_.FirstName
 $MBxLast = $_.LastName
 $MBxDept = $_.Department
 $MbxTitle = Get-aduser $_.alias -properties Title | Select Title
 $MbxOffice = Get-aduser $_.alias -properties Office | Select Office
 $returnobj = new-object psobject
 $returnobj |Add-Member -MemberType NoteProperty -Name "ListDisplayName" -Value $ListDisplayName
 $returnobj |Add-Member -MemberType NoteProperty -Name "FirstName" -Value $MbxFirst
 $returnobj |Add-Member -MemberType NoteProperty -Name "LastName" -Value $MbxLast
 $returnobj |Add-Member -MemberType NoteProperty -Name "Department" -Value $MbxDept
 $returnobj |Add-Member -MemberType NoteProperty -Name "Title" -Value $MbxTitle
 $returnobj |Add-Member -MemberType NoteProperty -Name "Office" -Value $MbxOffice
 $final += $returnObj
  }
}

$final | Export-csv C:tempAccM.csv

Delete Calendar entries by Start date and End Date

Scenario:  When exporting a Calendar to a PST via New-MailboxExportRequest, there no good content filters to export via Start Date and End Date for exporting calendar entries to a date range.  The Created/Modified/Received dates which are saved into Indexing may not produce the correct result if those items are queried.  Instead follow these steps:

1. Export the entire calendar and import it into another mailbox.

2. On the new mailbox, give your account that you are running Exchange Powershell from FULL permissions to the mailbox you created (or the mailbox with the calendar you want to edit).

3. Run the following script.  Change that which is in blue:

*Note: You can only do up to 2 year increments.  You can also comment out (#) the delete lines to double check your work.

Import-Module -Name "C:Program FilesMicrosoftExchange ServerV15BinMicrosoft.Exchange.WebServices.dll"
$mailboxname = "TEST222@domain.com"
$service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.Exchangeversion]::exchange2013)
$service.Url = new-object System.Uri("https://Ex2013Svr1.domain.com/EWS/Exchange.asmx")
$folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$MailboxName) 
$Calendar = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)    
#Define Date to Query 
#running 2 Year Increment
$StartDate = "1/1/2006"
$EndDate = "12/31/2007"
$CalendarView = New-Object Microsoft.Exchange.WebServices.Data.CalendarView($StartDate,$EndDate,100000)    
$fiItems = $service.FindAppointments($Calendar.Id,$CalendarView)    
foreach($Item in $fiItems.Items){      
 "Start    : " + $Item.Start  
        #"ENd    : " + $Item.END
        "Subject     : " + $Item.Subject  
 $Item.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)
    }    
#running 1 Year Increment
$StartDate = "1/1/2008"
$EndDate = "12/31/2008"
$CalendarView = New-Object Microsoft.Exchange.WebServices.Data.CalendarView($StartDate,$EndDate,100000)    
$fiItems = $service.FindAppointments($Calendar.Id,$CalendarView)    
foreach($Item in $fiItems.Items){      
 "Start    : " + $Item.Start  
        #"ENd    : " + $Item.END
        "Subject     : " + $Item.Subject  
 $Item.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)