microsoft / CSS-Exchange

Exchange Server support tools and scripts
MIT License
1.21k stars 333 forks source link

Prevent Divide By Zero #1983

Closed dpaulson45 closed 5 months ago

dpaulson45 commented 5 months ago

Issue: Engineer reported the following issue:

ForEach-Object : Attempted to divide by zero.
Inner Exception:    at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at <ScriptBlock>(Closure , FunctionContext )
   at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke clauseToInvoke, Boolean createLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)
   at System.Management.Automation.ScriptBlock.<>c__DisplayClass57_0.<InvokeWithPipe>b__0()
   at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThreadCheck(Action action)
   at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args)
   at System.Management.Automation.ScriptBlock.InvokeUsingCmdlet(Cmdlet contextCmdlet, Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Object[] args)
   at Microsoft.PowerShell.Commands.ForEachObjectCommand.ProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()
Position Message: At C:\Troubleshoot-ModernSearch.ps1:483 char:17
+                 ForEach-Object {
+                 ~~~~~~~~~~~~~~~~
Script Stack: at <ScriptBlock>, C:\Troubleshoot-ModernSearch.ps1: line 499
at <ScriptBlock><End>, C:\ServerFQDN.psm1: line 22220
at Get-MailboxStatisticsOnDatabase<Process>, C:\Troubleshoot-ModernSearch.ps1: line 482
at Main, C:\Troubleshoot-ModernSearch.ps1: line 2720
at <ScriptBlock>, C:\Troubleshoot-ModernSearch.ps1: line 2982
at <ScriptBlock>, <No file>: line 1
WARNING: Ran into an issue with the script. If possible please email 'ExToolsFeedback@microsoft.com' of the issue that you are facing

Reason: We should never attempt to divide by zero

Fix: Do the math of subtraction, check to see if that is zero before attempting to divide by that value.

Validation: N/A