fix: improve error handling in PowerShell guidelines
Update error handling examples to use $PSCmdlet.WriteError() and $PSCmdlet.ThrowTerminatingError() instead of Write-Error and throw for better PowerShell cmdlet integration.
This commit is contained in:
parent
591d2fdc08
commit
8c13baa7df
@ -5,7 +5,8 @@ description: 'PowerShell cmdlet and scripting best practices based on Microsoft
|
|||||||
|
|
||||||
# PowerShell Cmdlet Development Guidelines
|
# PowerShell Cmdlet Development Guidelines
|
||||||
|
|
||||||
This guide provides PowerShell-specific instructions to help GitHub Copilot generate idiomatic, safe, and maintainable scripts. It aligns with Microsoft’s PowerShell cmdlet development guidelines.
|
This guide provides PowerShell-specific instructions to help GitHub Copilot generate idiomatic,
|
||||||
|
safe, and maintainable scripts. It aligns with Microsoft’s PowerShell cmdlet development guidelines.
|
||||||
|
|
||||||
## Naming Conventions
|
## Naming Conventions
|
||||||
|
|
||||||
@ -150,7 +151,7 @@ function Update-ResourceStatus {
|
|||||||
)
|
)
|
||||||
|
|
||||||
begin {
|
begin {
|
||||||
Write-Verbose "Starting resource status update process"
|
Write-Verbose 'Starting resource status update process'
|
||||||
$timestamp = Get-Date
|
$timestamp = Get-Date
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -166,13 +167,13 @@ function Update-ResourceStatus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Only output if PassThru is specified
|
# Only output if PassThru is specified
|
||||||
if ($PassThru) {
|
if ($PassThru.IsPresent) {
|
||||||
Write-Output $resource
|
Write-Output $resource
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
end {
|
end {
|
||||||
Write-Verbose "Resource status update process completed"
|
Write-Verbose 'Resource status update process completed'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -198,6 +199,9 @@ function Update-ResourceStatus {
|
|||||||
- Return meaningful error messages
|
- Return meaningful error messages
|
||||||
- Use ErrorVariable when needed
|
- Use ErrorVariable when needed
|
||||||
- Include proper terminating vs non-terminating error handling
|
- Include proper terminating vs non-terminating error handling
|
||||||
|
- In advanced functions with `[CmdletBinding()]`, prefer `$PSCmdlet.WriteError()` over `Write-Error`
|
||||||
|
- In advanced functions with `[CmdletBinding()]`, prefer `$PSCmdlet.ThrowTerminatingError()` over `throw`
|
||||||
|
- Construct proper ErrorRecord objects with category, target, and exception details
|
||||||
|
|
||||||
- **Non-Interactive Design:**
|
- **Non-Interactive Design:**
|
||||||
- Accept input via parameters
|
- Accept input via parameters
|
||||||
@ -220,7 +224,7 @@ function Remove-UserAccount {
|
|||||||
)
|
)
|
||||||
|
|
||||||
begin {
|
begin {
|
||||||
Write-Verbose "Starting user account removal process"
|
Write-Verbose 'Starting user account removal process'
|
||||||
$ErrorActionPreference = 'Stop'
|
$ErrorActionPreference = 'Stop'
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,7 +232,13 @@ function Remove-UserAccount {
|
|||||||
try {
|
try {
|
||||||
# Validation
|
# Validation
|
||||||
if (-not (Test-UserExists -Username $Username)) {
|
if (-not (Test-UserExists -Username $Username)) {
|
||||||
Write-Error "User account '$Username' not found"
|
$errorRecord = [System.Management.Automation.ErrorRecord]::new(
|
||||||
|
[System.Exception]::new("User account '$Username' not found"),
|
||||||
|
'UserNotFound',
|
||||||
|
[System.Management.Automation.ErrorCategory]::ObjectNotFound,
|
||||||
|
$Username
|
||||||
|
)
|
||||||
|
$PSCmdlet.WriteError($errorRecord)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -241,19 +251,27 @@ function Remove-UserAccount {
|
|||||||
Remove-ADUser -Identity $Username -ErrorAction Stop
|
Remove-ADUser -Identity $Username -ErrorAction Stop
|
||||||
Write-Warning "User account '$Username' has been removed"
|
Write-Warning "User account '$Username' has been removed"
|
||||||
}
|
}
|
||||||
}
|
} catch [Microsoft.ActiveDirectory.Management.ADException] {
|
||||||
catch [Microsoft.ActiveDirectory.Management.ADException] {
|
$errorRecord = [System.Management.Automation.ErrorRecord]::new(
|
||||||
Write-Error "Active Directory error: $_"
|
$_.Exception,
|
||||||
throw
|
'ActiveDirectoryError',
|
||||||
}
|
[System.Management.Automation.ErrorCategory]::NotSpecified,
|
||||||
catch {
|
$Username
|
||||||
Write-Error "Unexpected error removing user account: $_"
|
)
|
||||||
throw
|
$PSCmdlet.ThrowTerminatingError($errorRecord)
|
||||||
|
} catch {
|
||||||
|
$errorRecord = [System.Management.Automation.ErrorRecord]::new(
|
||||||
|
$_.Exception,
|
||||||
|
'UnexpectedError',
|
||||||
|
[System.Management.Automation.ErrorCategory]::NotSpecified,
|
||||||
|
$Username
|
||||||
|
)
|
||||||
|
$PSCmdlet.ThrowTerminatingError($errorRecord)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
end {
|
end {
|
||||||
Write-Verbose "User account removal process completed"
|
Write-Verbose 'User account removal process completed'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -307,12 +325,12 @@ function New-Resource {
|
|||||||
)
|
)
|
||||||
|
|
||||||
begin {
|
begin {
|
||||||
Write-Verbose "Starting resource creation process"
|
Write-Verbose 'Starting resource creation process'
|
||||||
}
|
}
|
||||||
|
|
||||||
process {
|
process {
|
||||||
try {
|
try {
|
||||||
if ($PSCmdlet.ShouldProcess($Name, "Create new resource")) {
|
if ($PSCmdlet.ShouldProcess($Name, 'Create new resource')) {
|
||||||
# Resource creation logic here
|
# Resource creation logic here
|
||||||
Write-Output ([PSCustomObject]@{
|
Write-Output ([PSCustomObject]@{
|
||||||
Name = $Name
|
Name = $Name
|
||||||
@ -320,14 +338,19 @@ function New-Resource {
|
|||||||
Created = Get-Date
|
Created = Get-Date
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
} catch {
|
||||||
catch {
|
$errorRecord = [System.Management.Automation.ErrorRecord]::new(
|
||||||
Write-Error "Failed to create resource: $_"
|
$_.Exception,
|
||||||
|
'ResourceCreationFailed',
|
||||||
|
[System.Management.Automation.ErrorCategory]::NotSpecified,
|
||||||
|
$Name
|
||||||
|
)
|
||||||
|
$PSCmdlet.ThrowTerminatingError($errorRecord)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
end {
|
end {
|
||||||
Write-Verbose "Completed resource creation process"
|
Write-Verbose 'Completed resource creation process'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user