diff --git a/Commands.ps1 b/Commands.ps1 index 9dd9622..375554c 100644 --- a/Commands.ps1 +++ b/Commands.ps1 @@ -111,7 +111,15 @@ function Set-PolicyFileEntry } $dirty = $false - $policyFile = OpenPolicyFile -Path $Path -ErrorAction Stop + + try + { + $policyFile = OpenPolicyFile -Path $Path -ErrorAction Stop + } + catch + { + $PSCmdlet.ThrowTerminatingError($_) + } } process @@ -139,7 +147,8 @@ function Set-PolicyFileEntry $bytes = $Data -as [byte[]] if ($null -eq $bytes) { - throw 'When -Type is set to Binary, -Data must be passed a Byte[] array.' + $errorRecord = InvalidDataTypeCombinationErrorRecord -Message 'When -Type is set to Binary, -Data must be passed a Byte[] array.' + $PSCmdlet.ThrowTerminatingError($errorRecord) } else { @@ -155,7 +164,8 @@ function Set-PolicyFileEntry if ($array.Count -ne 1) { - throw 'When -Type is set to String, -Data must be passed a scalar value or single-element array.' + $errorRecord = InvalidDataTypeCombinationErrorRecord -Message 'When -Type is set to String, -Data must be passed a scalar value or single-element array.' + $PSCmdlet.ThrowTerminatingError($errorRecord) } else { @@ -171,7 +181,8 @@ function Set-PolicyFileEntry if ($array.Count -ne 1) { - throw 'When -Type is set to ExpandString, -Data must be passed a scalar value or single-element array.' + $errorRecord = InvalidDataTypeCombinationErrorRecord -Message 'When -Type is set to ExpandString, -Data must be passed a scalar value or single-element array.' + $PSCmdlet.ThrowTerminatingError($errorRecord) } else { @@ -187,7 +198,8 @@ function Set-PolicyFileEntry $dword = ($array | Select-Object -First 1) -as [UInt32] if ($null -eq $dword -or $array.Count -ne 1) { - throw 'When -Type is set to DWord, -Data must be passed a valid UInt32 value.' + $errorRecord = InvalidDataTypeCombinationErrorRecord -Message 'When -Type is set to DWord, -Data must be passed a valid UInt32 value.' + $PSCmdlet.ThrowTerminatingError($errorRecord) } else { @@ -203,7 +215,8 @@ function Set-PolicyFileEntry $qword = ($array | Select-Object -First 1) -as [UInt64] if ($null -eq $qword -or $array.Count -ne 1) { - throw 'When -Type is set to QWord, -Data must be passed a valid UInt64 value.' + $errorRecord = InvalidDataTypeCombinationErrorRecord -Message 'When -Type is set to QWord, -Data must be passed a valid UInt64 value.' + $PSCmdlet.ThrowTerminatingError($errorRecord) } else { @@ -243,9 +256,16 @@ function Set-PolicyFileEntry { $doUpdateGptIni = -not $NoGptIniUpdate - # SavePolicyFile contains the calls to $PSCmdlet.ShouldProcess, and will inherit our - # WhatIfPreference / ConfirmPreference values from here. - SavePolicyFile -PolicyFile $policyFile -UpdateGptIni:$doUpdateGptIni -ErrorAction Stop + try + { + # SavePolicyFile contains the calls to $PSCmdlet.ShouldProcess, and will inherit our + # WhatIfPreference / ConfirmPreference values from here. + SavePolicyFile -PolicyFile $policyFile -UpdateGptIni:$doUpdateGptIni -ErrorAction Stop + } + catch + { + $PSCmdlet.ThrowTerminatingError($_) + } } } } @@ -313,7 +333,14 @@ function Get-PolicyFileEntry Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState } - $policyFile = OpenPolicyFile -Path $Path -ErrorAction Stop + try + { + $policyFile = OpenPolicyFile -Path $Path -ErrorAction Stop + } + catch + { + $PSCmdlet.ThrowTerminatingError($_) + } if ($PSCmdlet.ParameterSetName -eq 'ByKeyAndValue') { @@ -406,7 +433,15 @@ function Remove-PolicyFileEntry } $dirty = $false - $policyFile = OpenPolicyFile -Path $Path -ErrorAction Stop + + try + { + $policyFile = OpenPolicyFile -Path $Path -ErrorAction Stop + } + catch + { + $PSCmdlet.ThrowTerminatingError($_) + } } process @@ -430,9 +465,16 @@ function Remove-PolicyFileEntry { $doUpdateGptIni = -not $NoGptIniUpdate - # SavePolicyFile contains the calls to $PSCmdlet.ShouldProcess, and will inherit our - # WhatIfPreference / ConfirmPreference values from here. - SavePolicyFile -PolicyFile $policyFile -UpdateGptIni:$doUpdateGptIni -ErrorAction Stop + try + { + # SavePolicyFile contains the calls to $PSCmdlet.ShouldProcess, and will inherit our + # WhatIfPreference / ConfirmPreference values from here. + SavePolicyFile -PolicyFile $policyFile -UpdateGptIni:$doUpdateGptIni -ErrorAction Stop + } + catch + { + $PSCmdlet.ThrowTerminatingError($_) + } } } } @@ -502,5 +544,12 @@ function Update-GptIniVersion Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState } - IncrementGptIniVersion @PSBoundParameters + try + { + IncrementGptIniVersion @PSBoundParameters + } + catch + { + $PSCmdlet.ThrowTerminatingError($_) + } } diff --git a/Common.ps1 b/Common.ps1 index c0ae340..3e9932d 100644 --- a/Common.ps1 +++ b/Common.ps1 @@ -17,12 +17,28 @@ function OpenPolicyFile { $policyFile.LoadFile() } + catch [TJX.PolFileEditor.FileFormatException] + { + $message = "File '$Path' is not a valid POL file." + $exception = New-Object System.Exception($message) + + $errorRecord = New-Object System.Management.Automation.ErrorRecord( + $exception, 'InvalidPolFileContents', [System.Management.Automation.ErrorCategory]::InvalidData, $Path + ) + + throw $errorRecord + } catch { $errorRecord = $_ $message = "Error loading policy file at path '$Path': $($errorRecord.Exception.Message)" $exception = New-Object System.Exception($message, $errorRecord.Exception) - throw $exception + + $newErrorRecord = New-Object System.Management.Automation.ErrorRecord( + $exception, 'FailedToOpenPolicyFile', [System.Management.Automation.ErrorCategory]::OperationStopped, $Path + ) + + throw $newErrorRecord } } @@ -112,7 +128,12 @@ function SavePolicyFile $errorRecord = $_ $message = "Error creating parent folder of path '$Path': $($errorRecord.Exception.Message)" $exception = New-Object System.Exception($message, $errorRecord.Exception) - throw $exception + + $newErrorRecord = New-Object System.Management.Automation.ErrorRecord( + $exception, 'CreateParentFolderError', $errorRecord.CategoryInfo.Category, $Path + ) + + throw $newErrorRecord } } @@ -125,7 +146,12 @@ function SavePolicyFile $errorRecord = $_ $message = "Error saving policy file to path '$($PolicyFile.FileName)': $($errorRecord.Exception.Message)" $exception = New-Object System.Exception($message, $errorRecord.Exception) - throw $exception + + $newErrorRecord = New-Object System.Management.Automation.ErrorRecord( + $exception, 'FailedToSavePolicyFile', [System.Management.Automation.ErrorCategory]::OperationStopped, $PolicyFile + ) + + throw $newErrorRecord } } @@ -479,7 +505,7 @@ function GetSidForAccount($Account) $Acc ) - $PSCmdlet.ThrowTerminatingError($errorRecord) + throw $errorRecord } } @@ -654,7 +680,15 @@ function Assert-ValidDataAndType $Type -ne [Microsoft.Win32.RegistryValueKind]::Binary -and $Data.Count -gt 1) { - throw 'Do not pass arrays with multiple values to the -Data parameter when -Type is not set to either Binary or MultiString.' + $errorRecord = InvalidDataTypeCombinationErrorRecord -Message 'Do not pass arrays with multiple values to the -Data parameter when -Type is not set to either Binary or MultiString.' + throw $errorRecord } +} +function InvalidDataTypeCombinationErrorRecord($Message) +{ + $exception = New-Object System.Exception($Message) + return New-Object System.Management.Automation.ErrorRecord( + $exception, 'InvalidDataTypeCombination', [System.Management.Automation.ErrorCategory]::InvalidArgument, $null + ) } diff --git a/PolicyFileEditor.psd1 b/PolicyFileEditor.psd1 index 37702e8..464e3d9 100644 --- a/PolicyFileEditor.psd1 +++ b/PolicyFileEditor.psd1 @@ -1,6 +1,6 @@ @{ ModuleToProcess = 'PolicyFileEditor.psm1' - ModuleVersion = '1.4.2' + ModuleVersion = '2.0' GUID = '110a2398-3053-4ffc-89d1-1b6a38a2dc86' Author = 'Dave Wyatt' CompanyName = 'Home' @@ -36,7 +36,7 @@ LicenseUri = 'https://www.apache.org/licenses/LICENSE-2.0.html' ProjectUri = 'https://github.com/dlwyatt/PolicyFileEditor' # IconUri = '' - ReleaseNotes = 'Build script update' + ReleaseNotes = 'Error reporting changes' } } }