Skip to content

Commit 2365c7c

Browse files
committed
Added new functions to throw exceptions
1 parent 1eca3fb commit 2365c7c

1 file changed

Lines changed: 90 additions & 2 deletions

File tree

DSCResources/CommonResourceHelper.psm1

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ function Test-DscParameterState
211211

212212
if ($desiredType.IsArray)
213213
{
214-
Write-Verbose "Comparing values in property '$key'"
214+
Write-Verbose -Message "Comparing values in property '$key'"
215215
if (-not $CurrentValues.ContainsKey($key) -or -not $CurrentValues.$key)
216216
{
217217
Write-Verbose -Message "NOTMATCH: Value (type $($desiredType.Name)) for property '$key' does not match. Current state is '$($CurrentValues.$key)' and desired state is '$($desiredValuesClean.$key)'"
@@ -290,7 +290,7 @@ function Test-DscParameterState
290290
}
291291
}
292292

293-
Write-Verbose "Result is '$returnValue'"
293+
Write-Verbose -Message "Result is '$returnValue'"
294294
return $returnValue
295295
}
296296

@@ -325,9 +325,97 @@ function Test-DSCObjectHasProperty
325325
return $false
326326
}
327327

328+
<#
329+
.SYNOPSIS
330+
Creates and throws an invalid argument exception
331+
332+
.PARAMETER Message
333+
The message explaining why this error is being thrown
334+
335+
.PARAMETER ArgumentName
336+
The name of the invalid argument that is causing this error to be thrown
337+
#>
338+
function New-InvalidArgumentException
339+
{
340+
[CmdletBinding()]
341+
param
342+
(
343+
[Parameter(Mandatory = $true)]
344+
[ValidateNotNullOrEmpty()]
345+
[String]
346+
$Message,
347+
348+
[Parameter(Mandatory = $true)]
349+
[ValidateNotNullOrEmpty()]
350+
[String]
351+
$ArgumentName
352+
)
353+
354+
$argumentException = New-Object -TypeName 'ArgumentException' `
355+
-ArgumentList @($Message, $ArgumentName)
356+
$newObjectParams = @{
357+
TypeName = 'System.Management.Automation.ErrorRecord'
358+
ArgumentList = @($argumentException, $ArgumentName, 'InvalidArgument', $null)
359+
}
360+
$errorRecord = New-Object @newObjectParams
361+
362+
throw $errorRecord
363+
}
364+
365+
<#
366+
.SYNOPSIS
367+
Creates and throws an invalid operation exception
368+
369+
.PARAMETER Message
370+
The message explaining why this error is being thrown
371+
372+
.PARAMETER ErrorRecord
373+
The error record containing the exception that is causing this terminating error
374+
#>
375+
function New-InvalidOperationException
376+
{
377+
[CmdletBinding()]
378+
param
379+
(
380+
[ValidateNotNullOrEmpty()]
381+
[String]
382+
$Message,
383+
384+
[ValidateNotNull()]
385+
[System.Management.Automation.ErrorRecord]
386+
$ErrorRecord
387+
)
388+
389+
if ($null -eq $Message)
390+
{
391+
$invalidOperationException = New-Object -TypeName 'InvalidOperationException'
392+
}
393+
elseif ($null -eq $ErrorRecord)
394+
{
395+
$invalidOperationException = New-Object -TypeName 'InvalidOperationException' `
396+
-ArgumentList @($Message)
397+
}
398+
else
399+
{
400+
$invalidOperationException = New-Object -TypeName 'InvalidOperationException' `
401+
-ArgumentList @($Message, $ErrorRecord.Exception)
402+
}
403+
404+
$newObjectParams = @{
405+
TypeName = 'System.Management.Automation.ErrorRecord'
406+
ArgumentList = @( $invalidOperationException.ToString(), 'MachineStateIncorrect',
407+
'InvalidOperation', $null )
408+
}
409+
410+
$errorRecordToThrow = New-Object @newObjectParams
411+
throw $errorRecordToThrow
412+
}
413+
328414
Export-ModuleMember -Function @(
329415
'Get-LocalizedData'
330416
'Remove-CommonParameter'
331417
'Test-DscParameterState'
332418
'Test-DSCObjectHasProperty'
419+
'New-InvalidOperationException'
420+
'New-InvalidArgumentException'
333421
)

0 commit comments

Comments
 (0)