You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: improve error handling in PowerShell guidelines (#280)
Update error handling examples to use $PSCmdlet.WriteError() and
$PSCmdlet.ThrowTerminatingError() instead of Write-Error and throw for better PowerShell cmdlet integration.
Copy file name to clipboardExpand all lines: instructions/powershell.instructions.md
+64-41Lines changed: 64 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,12 @@
1
1
---
2
2
applyTo: '**/*.ps1,**/*.psm1'
3
3
description: 'PowerShell cmdlet and scripting best practices based on Microsoft guidelines'
4
-
---
4
+
---
5
5
6
6
# PowerShell Cmdlet Development Guidelines
7
7
8
-
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.
8
+
This guide provides PowerShell-specific instructions to help GitHub Copilot generate idiomatic,
9
+
safe, and maintainable scripts. It aligns with Microsoft’s PowerShell cmdlet development guidelines.
9
10
10
11
## Naming Conventions
11
12
@@ -87,19 +88,19 @@ function Set-ResourceConfiguration {
87
88
param(
88
89
[Parameter(Mandatory)]
89
90
[string]$Name,
90
-
91
+
91
92
[Parameter()]
92
93
[ValidateSet('Dev', 'Test', 'Prod')]
93
94
[string]$Environment = 'Dev',
94
-
95
+
95
96
[Parameter()]
96
97
[switch]$Force,
97
-
98
+
98
99
[Parameter()]
99
100
[ValidateNotNullOrEmpty()]
100
101
[string[]]$Tags
101
102
)
102
-
103
+
103
104
process {
104
105
# Logic here
105
106
}
@@ -150,32 +151,32 @@ function Update-ResourceStatus {
150
151
)
151
152
152
153
begin {
153
-
Write-Verbose "Starting resource status update process"
154
+
Write-Verbose 'Starting resource status update process'
154
155
$timestamp = Get-Date
155
156
}
156
157
157
158
process {
158
159
# Process each resource individually
159
160
Write-Verbose "Processing resource: $Name"
160
-
161
+
161
162
$resource = [PSCustomObject]@{
162
-
Name = $Name
163
-
Status = $Status
163
+
Name = $Name
164
+
Status = $Status
164
165
LastUpdated = $timestamp
165
-
UpdatedBy = $env:USERNAME
166
+
UpdatedBy = $env:USERNAME
166
167
}
167
168
168
169
# Only output if PassThru is specified
169
-
if ($PassThru) {
170
+
if ($PassThru.IsPresent) {
170
171
Write-Output $resource
171
172
}
172
173
}
173
174
174
175
end {
175
-
Write-Verbose "Resource status update process completed"
176
+
Write-Verbose 'Resource status update process completed'
176
177
}
177
178
}
178
-
```
179
+
```
179
180
180
181
## Error Handling and Safety
181
182
@@ -198,6 +199,9 @@ function Update-ResourceStatus {
198
199
- Return meaningful error messages
199
200
- Use ErrorVariable when needed
200
201
- Include proper terminating vs non-terminating error handling
202
+
- In advanced functions with `[CmdletBinding()]`, prefer `$PSCmdlet.WriteError()` over `Write-Error`
203
+
- In advanced functions with `[CmdletBinding()]`, prefer `$PSCmdlet.ThrowTerminatingError()` over `throw`
204
+
- Construct proper ErrorRecord objects with category, target, and exception details
201
205
202
206
-**Non-Interactive Design:**
203
207
- Accept input via parameters
@@ -220,40 +224,54 @@ function Remove-UserAccount {
220
224
)
221
225
222
226
begin {
223
-
Write-Verbose "Starting user account removal process"
227
+
Write-Verbose 'Starting user account removal process'
0 commit comments