Skip to content

Commit 70ea971

Browse files
committed
Add checks for empty messages before telemetry logging
Telemetry logging now only occurs if the message is not null, empty, or whitespace. This prevents sending unnecessary or invalid log entries to telemetry.
1 parent d0b1e96 commit 70ea971

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

Modules/CIPPCore/Public/Tools/Enable-CippConsoleLogging.ps1

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ function Enable-CippConsoleLogging {
7777
[string[]]$Tags
7878
)
7979

80-
# Send to telemetry
81-
& $global:SendCippConsoleLog -Message ($MessageData | Out-String).Trim() -Level 'Information'
80+
# Send to telemetry if MessageData is string or can be converted to string and is not empty, null, or whitespace
81+
if ($MessageData -and -not [string]::IsNullOrWhiteSpace(($MessageData | Out-String).Trim())) {
82+
& $global:SendCippConsoleLog -Message ($MessageData | Out-String).Trim() -Level 'Information'
83+
}
8284

8385
# Call original function
8486
Microsoft.PowerShell.Utility\Write-Information @PSBoundParameters
@@ -92,8 +94,10 @@ function Enable-CippConsoleLogging {
9294
[string]$Message
9395
)
9496

95-
# Send to telemetry
96-
& $global:SendCippConsoleLog -Message $Message -Level 'Warning'
97+
# Send to telemetry if Message is not empty, null, or whitespace
98+
if ($Message -and -not [string]::IsNullOrWhiteSpace($Message)) {
99+
& $global:SendCippConsoleLog -Message $Message -Level 'Warning'
100+
}
97101

98102
# Call original function
99103
Microsoft.PowerShell.Utility\Write-Warning @PSBoundParameters
@@ -122,7 +126,10 @@ function Enable-CippConsoleLogging {
122126
elseif ($Exception) { $Exception.Message }
123127
elseif ($ErrorRecord) { $ErrorRecord.Exception.Message }
124128
else { 'Unknown error' }
125-
& $global:SendCippConsoleLog -Message $errorMessage -Level 'Error'
129+
130+
if ($errorMessage -and -not [string]::IsNullOrWhiteSpace($errorMessage)) {
131+
& $global:SendCippConsoleLog -Message $errorMessage -Level 'Error'
132+
}
126133

127134
# Call original function
128135
Microsoft.PowerShell.Utility\Write-Error @PSBoundParameters
@@ -137,7 +144,9 @@ function Enable-CippConsoleLogging {
137144
)
138145

139146
# Send to telemetry
140-
& $global:SendCippConsoleLog -Message $Message -Level 'Verbose'
147+
if ($Message -and -not [string]::IsNullOrWhiteSpace($Message)) {
148+
& $global:SendCippConsoleLog -Message $Message -Level 'Verbose'
149+
}
141150

142151
# Call original function
143152
Microsoft.PowerShell.Utility\Write-Verbose @PSBoundParameters
@@ -152,7 +161,9 @@ function Enable-CippConsoleLogging {
152161
)
153162

154163
# Send to telemetry
155-
& $global:SendCippConsoleLog -Message $Message -Level 'Debug'
164+
if ($Message -and -not [string]::IsNullOrWhiteSpace($Message)) {
165+
& $global:SendCippConsoleLog -Message $Message -Level 'Debug'
166+
}
156167

157168
# Call original function
158169
Microsoft.PowerShell.Utility\Write-Debug @PSBoundParameters
@@ -172,7 +183,9 @@ function Enable-CippConsoleLogging {
172183

173184
# Send to telemetry
174185
$message = if ($Object) { ($Object | Out-String).Trim() } else { '' }
175-
& $global:SendCippConsoleLog -Message $message -Level 'Information'
186+
if ($message -and -not [string]::IsNullOrWhiteSpace($message)) {
187+
& $global:SendCippConsoleLog -Message $message -Level 'Information'
188+
}
176189

177190
# Call original function
178191
Microsoft.PowerShell.Utility\Write-Host @PSBoundParameters

0 commit comments

Comments
 (0)