Set the Paging File Size for all Exchange Servers remotely via PowerShell

Scenario:  You want a script to set the Paging File Size to 32GB on multiple servers remotely.

Solution:  Run this script from a computer that has the ActiveDirectory Module for PowerShell installed.

#Query AD for your Servers
Import-Module ActiveDirectory
$strOU = "OU=Exchange2016,DC=XYZ,DC=COM"
$servers = get-adcomputer -searchbase $strOU -properties Name -Filter *|  where {$_.name -like "Ex16-*"} | Select -ExpandProperty Name

#Loop through all Exchange Servers and set the Custom Paging size to 32GB 
$servers | Sort Name | %{"Setting Page File Size on $_"; Invoke-Command -Computer $_ -ScriptBlock {
[int]$InitialSize = 32778
[int]$MaximumSize = 32778
$ComputerSystem = $null
$CurrentPageFile = $null
$modify = $false
$ComputerSystem = Get-WmiObject -Class Win32_ComputerSystem -EnableAllPrivileges
if ($ComputerSystem.AutomaticManagedPagefile) {$ComputerSystem.AutomaticManagedPagefile = $false; $ComputerSystem.Put()}
$CurrentPageFile = Get-WmiObject -Class Win32_PageFileSetting
if ($CurrentPageFile.InitialSize -ne $InitialSize) {$CurrentPageFile.InitialSize = $InitialSize;$modify = $true}
if ($CurrentPageFile.MaximumSize -ne $MaximumSize) {$CurrentPageFile.MaximumSize = $MaximumSize;$modify = $true}
if ($modify) { $CurrentPageFile.Put()}
}}

Find Exchange Recipients that do not have a specific EmailDomain as an Email Alias/Proxy Address

Scenario:  You want to query all Exchange recipients that do not have a email alias with the @XYZ.com domain.

Script: Use this command to export the recipients to a .csv.

get-recipient * -filter {emailaddresses -notlike ‘*@xyz.com’} -ResultSize unlimited | Export-csv C:tempxyz.csv

EWS Script for Searching the Dumpster and exporting message information

Scenario:  You want a logical way of exporting message information for all messages in a mailbox dumpster. In this scenario we are going to target the date of which items show  ‘deleted on’ in Outlook when in the ‘Recover Deleted Items’ folder.  The message property for this ‘Deleted On’ date is LastModifiedTime.

Script:  The following EWS script runs in 5 day increments as you are maxed out  when performing a filter/view against a dataset with EWS at a specific number.  The mailbox had a large number of recently deleted items. If you are able to, feel free to adjust the values for  $startdate.adddays(x) and $enddate.adddays(x) so you do not end up with 1000’s of files like me.

#Connect to Exchange Service
        Import-Module -Name "C:Program FilesMicrosoftExchange ServerV15BinMicrosoft.Exchange.WebServices.dll"
        $service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.Exchangeversion]::exchange2013)
        $service.Url = new-object System.Uri("https://ex2013svc1/EWS/Exchange.asmx")
   
 #Pick the Mailbox
        $mailboxname = "baduser@domain.com"
 
#Bind to the RootFolder
        $folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::RecoverableItemsDeletions,$mailboxname) 
        $RecoverFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)

#starter Variables
        [datetime]$StartDate  = "1/1/2005"
        [datetime]$EndDate = "1/5/2005 23:59"
        $result = @() 
        $temp = @()
        $Today = Get-date

#loop it
Do{
        #Define Loop Variables
        $file_startdate = ($startdate).tostring("MMddyyyy")
        $file_enddate = ($enddate).tostring("MMddyyyy")
        $file_Datestring = $file_Startdate +"_"+$file_Enddate
        "Searching $file_DateString"
        #Create Collection and Query upon your Filter
        $sfCollection = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+SearchFilterCollection([Microsoft.Exchange.WebServices.Data.LogicalOperator]::And);
        $Sfgt = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsGreaterThan([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived, $StartDate)
        $Sflt = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsLessThan([Microsoft.Exchange.WebServices.Data.ItemSchema]::DateTimeReceived, $EndDate)
        $sfCollection.add($Sfgt)
        $sfCollection.add($Sflt)
        $view = new-object Microsoft.Exchange.WebServices.Data.ItemView(2000000)
        $frFolderResult = $RecoverFolder.FindItems($sfCollection,$view) 
        $temp = @($frFolderResult)
        $result+= $frFolderResult
        "Current: $($temp.count)"
        "Total Count: $($result.count)"
        
#Export Results
        $temp |Select LastModifiedTime, LastModifiedName, Subject, From, DateTimeReceived  | Export-csv C:tempuser_$file_datestring.csv
        $StartDate = $StartDate.AddDays(5)
        $EndDate = $EndDate.AddDays(5)
        }While($StartDate -gt $today)

#View Results:
$result

Remove a users photo in Exchange

Scenario:  You need to remove a user’s photo in Exchange. You have verified that the thumbprintphoto attribute is <not set> in Active Directory, but the photo is still showing in Exchange.

Cause:  The mailbox stores a high-resolution photo in the mailbox in additional to the photo that is stored in AD.  You will need to remove it from the mailbox.

Solution:  Run the following PowerShell command to remove the photo for testuserA.

Remove-UserPhoto testuserA

 

Queue Buildups, Event IDs16025 and 205, and “You do not have permission to perform this action” when sending a message

Scenario:  You are troubleshooting an issue where you have the following symptoms:

  • When sending a message via any client the message is getting stuck in the Outbox/Drafts.
  • In Outlook Web it states “You do not have permission to perform this action” when attempting to send a message.
  • The Queues are backed up and not draining
  • Event IDs 16025 and 205 are in the event log for MSExchange Common and MSExchange Transport

Solution:  Make sure the ‘Register this connection’s addresses in DNS checkbox is checked.  This is required in order for Exchange Transport to operate correctly.  Once this was set, all was well.

Powershell: You want to determine which Exchange admin ran a PowerShell command edited a specific mailbox

Scenario: You want to determine which Exchange admin edited/altered the mailbox of a specific user HarryJ.  HarryJ could no longer access OWA as someone disabled it.  Now to determine  the admin that made the change.

Solution: Run the following:

Search-AdminAuditLog -cmdlets Set-CasMailbox -startdate “2/28/201 7:00PM” -enddate “3/1/2017  9:00AM” | Where ObjectModified -like *HarryJ* | Out-gridview

Install Windows Updates and Hotfixes from Command Line

Scenario:  You have a lot of Windows Updates and Hotfixes that you need to install manually.  You want to do the following:

  1. Script the install
  2. Do not restart the server after the update is installed — (so you can manually restart it when you are ready)

Scriptlets:

Download the updates to a folder and copy that folder to the servers requiring the update.

#Collect your Servers into a variable
$Servers = Get-exchangeserver ex*

#Create an folder to copy your updates to
$Servers.name | %{ MD \$_c$updatesCluster_Updates }

#Copy your updates to that new folder
$servers.name | %{ Copy-item "C:Cluster_updates*.msu" "\$_c$updatesCluster_updates"

Install the updates on each server by running the following command from an elevated command prompt:

FOR %h IN (*.msu) DO START /WAIT WUSA %h /QUIET /NORESTART

To verify the updates are installed on each one of your servers, run the following PowerShell commandlet:

$servers.name | %{ get-hotfix -computername $_ | Where InstalledOn -gt 2/5/2017}

 

 

 

Powershell: Run a Scheduled Task Remotely

Scenario:  You want to run a Scheduled Task remotely without needing to remote into the server, open task scheduler, and execute the task.

Solution:  Run the following in Powershell with appropriate permissions:

schtasks /run /s exchsvr1 /tn "exchange monitor"

If you want to loop it so it runs manually every 30 minutes, run the following:

#When the counter reaches 30, that’s 15 hours

$counter  = 0

Do{
schtasks /run /s exchsvr1 /tn "exchange monitor"
sleep 1800
$counter++
“$counter – Running script”
} While ($counter –lt 30)

 

Remove an Exchange Server from Autodiscover lookups

Scenario:  You have a server where you need to change the namespaceconfiguration settings of your client access virtual directories and you do not wish for this server to hand these server settings out with autodiscover.

Solution:  Run the following Powershell so it will not participate in the SCP lookup:

Set-ClientAccessServer  2016ExSrv1 -autodiscoverserviceinternaluri $null