Getting the error System.OutOfMemory Exception in some powershell scripts

I have a runbook designed for doing some tasks in specific AD accounts. Of many tasks , I am facing problem in three specific powershell scripts which are failing. The three scripts do the following tasks respectively:-

  • Disable ActiveSync
  • Check for out of office Status
  • Remove all rules and alerts
I get the error as "System.OutOfMemory" exception for the scripts , when considerably a higher number of records are processed by the orchestrator , say 30 records. Out of which 24 would get processed seamlessly and other 6 would faily for no reason.

The records are fetched from the database all at once , and all the subsequent tasks/ nodes are executed for all the records parallely and not by fetching records one by one and processing each record individually. I think the RAM consumption is getting very high whenever the number of records for processing is high.

I am attaching the snapshots of the three scripts for your reference :-

The snapshots attached below are of the Scripts being used , where I am facing the issue.

Any suggestion / observation would be of great help. Thank You!


(Disable Active Sync Powershell Script)

$Username = “XXXXXXXXXXX”

$Password = “XXXXX”

$ServerName = “Prodserver1”

$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force

$credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $Username,$securePassword

$PSSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://Prodserver1/PowerShell -Authentication Kerberos -Credential $credential

Import-PSSession $PSSession

$Script={

Set-CASMailbox -Identity EmpID -ActiveSyncEnabled $false

}

Invoke-Command -Session $PSSession -ScriptBlock $Script

Remove-PSSession $PSSession

#——————————————————————————————————————————–

#(Remove all rules and alerts Powershell Script)

$Username = “XXXXXXXXXX”

$Password = “XXXXX”

$ServerName = “Prodserver1”

$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force

$credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $Username,$securePassword

$PSSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://Prodserver1/PowerShell -Authentication Kerberos -Credential $credential

Import-PSSession $PSSession -DisableNameChecking

$Script={

Get-InboxRule -Mailbox EmpID | Remove-InboxRule -Confirm:$False -Force

}

Invoke-Command -Session $PSSession -ScriptBlock $Script

Remove-PSSession $PSSession

#————————————————————————————————————————————–

#(Check Out Of Office status)

$Username = “XXXXXXXXXX”

$Password = “XXXXX”

$ServerName = “Prodserver1”

$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force

$credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $Username,$securePassword

$PSSession2 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://Prodserver1/PowerShell -Authentication Kerberos -Credential $credential

Import-PSSession $PSSession2

$Script={

$OOO = Get-MailboxAutoReplyConfiguration -identity “Distinguished Name” | select AutoReplyState

$OutOfOffice = $OOO.AutoReplyState

}

Invoke-Command -Session $PSSession2 -ScriptBlock $Script

Remove-PSSession $PSSession2

Please format your code as code using the code tag button named “PRE”.

When you corosspost the same question at the same time to different forums you should at least post links to the other forums along with question to avoid people willing to help you making their work twice or even more.

https://social.technet.microsoft.com/Forums/en-US/9b69aa86-2cc3-45c4-bb0b-68a4dfb21361/systemoutofmemory-exception-occuring-intermittently?forum=winserverpowershell