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

Find the network driver version for multiple servers

Scenario:  You want to find the network driver information on multiple servers.  Run the following scriptlet below:

#Grab your collection variable

$servers = "ExServer1","ExServer2","ExServer3"

#For Cisco
$cisconetwork = $servers | %{get-WmiObject Win32_PnPSignedDriver -ComputerName $_ | ? Devicename -like "*Cisco VIC Ethernet*" | select devicename, driverversion, Driverdate, DriverProviderName, Manufacturer, PSCOMPUTERNAME}

#For HP
$HPnetwork = $HPservers | %{get-WmiObject Win32_PnPSignedDriver -ComputerName $_ | ? Devicename -like "*Ethernet*" | select devicename, driverversion, Driverdate, DriverProviderName, Manufacturer, PSCOMPUTERNAME}

 

 

 

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

 

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

PowerShell: Copy and rename file for to keep as a backup

Scenario:  You want to use PowerShell to make a backup of a specific file across many servers and store it locally.  We will perform this on a Web.Config file for IIS. The file once copied will be stored as C:SharebkupWebsvr1-wwwroot_web.config.

 

Script:

#Collect your Servers into a variable
$servers = "WebSvr1","WebSvr2","WebSvr3"

#Copy the file from the remote server to a local share

$Servers | %{
Copy-Item -Path "\$_c$inetpubwwwrootweb.config" -Destination "C:Sharebkup$_-wwwroot_web.config" 
}

Powershell: Increase the max size limit for ActiveSync Properties/Metadata in you Web.Config

Scenario:  You need to increase the ActiveSync default message size limit from 10MB to a larger value.  By default the RequestFilteringLimit set in IIS is 30,000,000 bytes (approx. 28MB) so we will operate within the limitations of that setting, meaning you cannot go higher than 28MB unless you set that property to a higher value.  So lets be safe and set that value to 15MB on all multirole exchange 2013 servers.  We need to edit the web.config files in the following directories:
Client Access: 

%ExchangeInstallPath%FrontEndHttpProxySyncweb.config – maxRequestLength=”10240″ kilobytes

Mailbox

%ExchangeInstallPath%ClientAccessSyncweb.config – maxRequestLength=”10240″ kilobytes

%ExchangeInstallPath%ClientAccessSyncweb.config  – <add key=”MaxDocumentDataSize” value=”10240000″>  bytes
Script:  IISReset is not needed!

#variables
$servers = (Get-ExchangeServer).name
$MRL = "15360"  #15MB in KB
$MDDS = "15728640" #15MB in Bytes


$Servers | %{
#ClientAccess
    #MaxRequestLength
    $webConfig = "\$_c$Program FilesMicrosoftExchange ServerV15FrontEndHttpProxysyncweb.config" 
    $doc = new-object System.Xml.XmlDocument 
    $doc.Load($webConfig) 
    $doc.get_DocumentElement()."system.web".httpruntime.maxrequestlength = $MRL 
    #Save Document
    $doc.Save($webConfig) 

#Mailbox
    #MaxRequestLength
    $webConfig = "\$_c$Program FilesMicrosoftExchange ServerV15ClientAccessSyncweb.config" 
    $doc = new-object System.Xml.XmlDocument 
    $doc.Load($webConfig) 
    $doc.get_DocumentElement()."system.web".httpruntime.maxrequestlength = $MRL 
    #MaxDocumentDataSize
    $key = "MaxDocumentDataSize"
    $node = $doc.SelectSingleNode('configuration/appSettings/add[@key="' + $key + '"]') 
    $node.Attributes['value'].Value = $MDDS 
    #Save Document
    $doc.Save($webConfig) 
}