@mytechtoday/augment-extensions 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +614 -39
- package/augment-extensions/coding-standards/bash/README.md +196 -0
- package/augment-extensions/coding-standards/bash/module.json +163 -0
- package/augment-extensions/coding-standards/bash/rules/naming-conventions.md +336 -0
- package/augment-extensions/coding-standards/bash/rules/universal-standards.md +289 -0
- package/augment-extensions/coding-standards/css/README.md +40 -0
- package/augment-extensions/coding-standards/css/examples/css-examples.css +550 -0
- package/augment-extensions/coding-standards/css/module.json +44 -0
- package/augment-extensions/coding-standards/css/rules/css-modern-features.md +448 -0
- package/augment-extensions/coding-standards/css/rules/css-standards.md +492 -0
- package/augment-extensions/coding-standards/html/README.md +40 -0
- package/augment-extensions/coding-standards/html/examples/html-examples.html +267 -0
- package/augment-extensions/coding-standards/html/examples/responsive-layout.html +505 -0
- package/augment-extensions/coding-standards/html/module.json +44 -0
- package/augment-extensions/coding-standards/html/rules/html-standards.md +349 -0
- package/augment-extensions/coding-standards/html-css-js/README.md +194 -0
- package/augment-extensions/coding-standards/html-css-js/examples/async-examples.js +487 -0
- package/augment-extensions/coding-standards/html-css-js/examples/css-examples.css +550 -0
- package/augment-extensions/coding-standards/html-css-js/examples/dom-examples.js +667 -0
- package/augment-extensions/coding-standards/html-css-js/examples/html-examples.html +267 -0
- package/augment-extensions/coding-standards/html-css-js/examples/javascript-examples.js +612 -0
- package/augment-extensions/coding-standards/html-css-js/examples/responsive-layout.html +505 -0
- package/augment-extensions/coding-standards/html-css-js/module.json +48 -0
- package/augment-extensions/coding-standards/html-css-js/rules/async-patterns.md +515 -0
- package/augment-extensions/coding-standards/html-css-js/rules/css-modern-features.md +448 -0
- package/augment-extensions/coding-standards/html-css-js/rules/css-standards.md +492 -0
- package/augment-extensions/coding-standards/html-css-js/rules/dom-manipulation.md +439 -0
- package/augment-extensions/coding-standards/html-css-js/rules/html-standards.md +349 -0
- package/augment-extensions/coding-standards/html-css-js/rules/javascript-standards.md +486 -0
- package/augment-extensions/coding-standards/html-css-js/rules/performance.md +463 -0
- package/augment-extensions/coding-standards/html-css-js/rules/tooling.md +543 -0
- package/augment-extensions/coding-standards/js/README.md +46 -0
- package/augment-extensions/coding-standards/js/examples/async-examples.js +487 -0
- package/augment-extensions/coding-standards/js/examples/dom-examples.js +667 -0
- package/augment-extensions/coding-standards/js/examples/javascript-examples.js +612 -0
- package/augment-extensions/coding-standards/js/module.json +49 -0
- package/augment-extensions/coding-standards/js/rules/async-patterns.md +515 -0
- package/augment-extensions/coding-standards/js/rules/dom-manipulation.md +439 -0
- package/augment-extensions/coding-standards/js/rules/javascript-standards.md +486 -0
- package/augment-extensions/coding-standards/js/rules/performance.md +463 -0
- package/augment-extensions/coding-standards/js/rules/tooling.md +543 -0
- package/augment-extensions/coding-standards/php/README.md +248 -0
- package/augment-extensions/coding-standards/php/examples/api-endpoint-example.php +204 -0
- package/augment-extensions/coding-standards/php/examples/cli-command-example.php +206 -0
- package/augment-extensions/coding-standards/php/examples/legacy-refactoring-example.php +234 -0
- package/augment-extensions/coding-standards/php/examples/web-application-example.php +211 -0
- package/augment-extensions/coding-standards/php/examples/woocommerce-extension-example.php +215 -0
- package/augment-extensions/coding-standards/php/examples/wordpress-plugin-example.php +189 -0
- package/augment-extensions/coding-standards/php/module.json +166 -0
- package/augment-extensions/coding-standards/php/rules/api-development.md +480 -0
- package/augment-extensions/coding-standards/php/rules/category-configuration.md +332 -0
- package/augment-extensions/coding-standards/php/rules/cli-tools.md +472 -0
- package/augment-extensions/coding-standards/php/rules/cms-integration.md +561 -0
- package/augment-extensions/coding-standards/php/rules/code-quality.md +402 -0
- package/augment-extensions/coding-standards/php/rules/documentation.md +425 -0
- package/augment-extensions/coding-standards/php/rules/ecommerce.md +627 -0
- package/augment-extensions/coding-standards/php/rules/error-handling.md +336 -0
- package/augment-extensions/coding-standards/php/rules/legacy-migration.md +677 -0
- package/augment-extensions/coding-standards/php/rules/naming-conventions.md +279 -0
- package/augment-extensions/coding-standards/php/rules/performance.md +392 -0
- package/augment-extensions/coding-standards/php/rules/psr-standards.md +186 -0
- package/augment-extensions/coding-standards/php/rules/security.md +358 -0
- package/augment-extensions/coding-standards/php/rules/testing.md +403 -0
- package/augment-extensions/coding-standards/php/rules/type-declarations.md +331 -0
- package/augment-extensions/coding-standards/php/rules/web-applications.md +426 -0
- package/augment-extensions/coding-standards/powershell/README.md +154 -0
- package/augment-extensions/coding-standards/powershell/examples/admin-example.ps1 +272 -0
- package/augment-extensions/coding-standards/powershell/examples/automation-example.ps1 +173 -0
- package/augment-extensions/coding-standards/powershell/examples/cloud-example.ps1 +243 -0
- package/augment-extensions/coding-standards/powershell/examples/cross-platform-example.ps1 +297 -0
- package/augment-extensions/coding-standards/powershell/examples/dsc-example.ps1 +224 -0
- package/augment-extensions/coding-standards/powershell/examples/legacy-migration-example.ps1 +340 -0
- package/augment-extensions/coding-standards/powershell/examples/module-example.psm1 +255 -0
- package/augment-extensions/coding-standards/powershell/module.json +165 -0
- package/augment-extensions/coding-standards/powershell/rules/administrative-tools.md +439 -0
- package/augment-extensions/coding-standards/powershell/rules/automation-scripts.md +240 -0
- package/augment-extensions/coding-standards/powershell/rules/cloud-orchestration.md +384 -0
- package/augment-extensions/coding-standards/powershell/rules/configuration-schema.md +383 -0
- package/augment-extensions/coding-standards/powershell/rules/cross-platform-scripts.md +482 -0
- package/augment-extensions/coding-standards/powershell/rules/dsc-configurations.md +296 -0
- package/augment-extensions/coding-standards/powershell/rules/error-handling.md +314 -0
- package/augment-extensions/coding-standards/powershell/rules/legacy-migrations.md +466 -0
- package/augment-extensions/coding-standards/powershell/rules/modules-functions.md +244 -0
- package/augment-extensions/coding-standards/powershell/rules/naming-conventions.md +266 -0
- package/augment-extensions/coding-standards/powershell/rules/performance-optimization.md +209 -0
- package/augment-extensions/coding-standards/powershell/rules/security-practices.md +314 -0
- package/augment-extensions/coding-standards/powershell/rules/testing-guidelines.md +268 -0
- package/augment-extensions/coding-standards/powershell/rules/universal-standards.md +197 -0
- package/augment-extensions/coding-standards/python/README.md +12 -8
- package/augment-extensions/coding-standards/python/examples/best-practices.py +373 -0
- package/augment-extensions/coding-standards/python/module.json +8 -4
- package/augment-extensions/coding-standards/python/rules/async-patterns.md +884 -0
- package/augment-extensions/coding-standards/python/rules/documentation.md +831 -0
- package/augment-extensions/coding-standards/python/rules/error-handling.md +855 -68
- package/augment-extensions/coding-standards/python/rules/testing.md +409 -0
- package/augment-extensions/coding-standards/python/rules/tooling.md +446 -0
- package/augment-extensions/coding-standards/python/rules/type-hints.md +115 -50
- package/augment-extensions/collections/html-css-js/README.md +82 -0
- package/augment-extensions/collections/html-css-js/collection.json +41 -0
- package/augment-extensions/domain-rules/database/README.md +161 -0
- package/augment-extensions/domain-rules/database/examples/flat-database-example.md +793 -0
- package/augment-extensions/domain-rules/database/examples/hybrid-database-example.md +1132 -0
- package/augment-extensions/domain-rules/database/examples/nosql-document-example.md +868 -0
- package/augment-extensions/domain-rules/database/examples/nosql-graph-example.md +805 -0
- package/augment-extensions/domain-rules/database/examples/relational-schema-example.md +621 -0
- package/augment-extensions/domain-rules/database/examples/vector-database-example.md +965 -0
- package/augment-extensions/domain-rules/database/module.json +28 -0
- package/augment-extensions/domain-rules/database/rules/flat-databases.md +624 -0
- package/augment-extensions/domain-rules/database/rules/nosql-databases.md +588 -0
- package/augment-extensions/domain-rules/database/rules/nosql-document-stores.md +856 -0
- package/augment-extensions/domain-rules/database/rules/nosql-graph-databases.md +778 -0
- package/augment-extensions/domain-rules/database/rules/nosql-key-value-stores.md +963 -0
- package/augment-extensions/domain-rules/database/rules/performance-optimization.md +1076 -0
- package/augment-extensions/domain-rules/database/rules/relational-databases.md +697 -0
- package/augment-extensions/domain-rules/database/rules/relational-indexing.md +671 -0
- package/augment-extensions/domain-rules/database/rules/relational-query-optimization.md +607 -0
- package/augment-extensions/domain-rules/database/rules/relational-schema-design.md +907 -0
- package/augment-extensions/domain-rules/database/rules/relational-transactions.md +783 -0
- package/augment-extensions/domain-rules/database/rules/security-standards.md +980 -0
- package/augment-extensions/domain-rules/database/rules/universal-best-practices.md +485 -0
- package/augment-extensions/domain-rules/database/rules/vector-databases.md +521 -0
- package/augment-extensions/domain-rules/database/rules/vector-embeddings.md +858 -0
- package/augment-extensions/domain-rules/database/rules/vector-indexing.md +934 -0
- package/augment-extensions/domain-rules/mcp/README.md +150 -0
- package/augment-extensions/domain-rules/mcp/examples/compressed-example.md +522 -0
- package/augment-extensions/domain-rules/mcp/examples/graph-augmented-example.md +520 -0
- package/augment-extensions/domain-rules/mcp/examples/hybrid-example.md +570 -0
- package/augment-extensions/domain-rules/mcp/examples/state-based-example.md +427 -0
- package/augment-extensions/domain-rules/mcp/examples/token-based-example.md +435 -0
- package/augment-extensions/domain-rules/mcp/examples/vector-based-example.md +502 -0
- package/augment-extensions/domain-rules/mcp/module.json +49 -0
- package/augment-extensions/domain-rules/mcp/rules/compressed-mcp.md +595 -0
- package/augment-extensions/domain-rules/mcp/rules/configuration.md +345 -0
- package/augment-extensions/domain-rules/mcp/rules/graph-augmented-mcp.md +687 -0
- package/augment-extensions/domain-rules/mcp/rules/hybrid-mcp.md +636 -0
- package/augment-extensions/domain-rules/mcp/rules/state-based-mcp.md +484 -0
- package/augment-extensions/domain-rules/mcp/rules/testing-validation.md +360 -0
- package/augment-extensions/domain-rules/mcp/rules/token-based-mcp.md +393 -0
- package/augment-extensions/domain-rules/mcp/rules/universal-rules.md +194 -0
- package/augment-extensions/domain-rules/mcp/rules/vector-based-mcp.md +625 -0
- package/augment-extensions/workflows/beads/module.json +4 -3
- package/augment-extensions/workflows/database/README.md +195 -0
- package/augment-extensions/workflows/database/ai-prompt-testing.md +295 -0
- package/augment-extensions/workflows/database/examples/migration-example.md +498 -0
- package/augment-extensions/workflows/database/examples/optimization-example.md +496 -0
- package/augment-extensions/workflows/database/examples/schema-design-example.md +444 -0
- package/augment-extensions/workflows/database/module.json +42 -0
- package/augment-extensions/workflows/database/rules/data-migration.md +249 -0
- package/augment-extensions/workflows/database/rules/documentation-standards.md +339 -0
- package/augment-extensions/workflows/database/rules/migration-workflow.md +352 -0
- package/augment-extensions/workflows/database/rules/optimization-workflow.md +435 -0
- package/augment-extensions/workflows/database/rules/schema-design-workflow.md +535 -0
- package/augment-extensions/workflows/database/rules/testing-patterns.md +305 -0
- package/augment-extensions/workflows/database/rules/workflow.md +458 -0
- package/augment-extensions/workflows/openspec/module.json +4 -3
- package/augment-extensions/writing-standards/screenplay/README.md +171 -0
- package/augment-extensions/writing-standards/screenplay/examples/aaa-hollywood-scene.fountain +164 -0
- package/augment-extensions/writing-standards/screenplay/module.json +124 -0
- package/augment-extensions/writing-standards/screenplay/rules/universal-formatting.md +339 -0
- package/cli/MODULES.md +302 -0
- package/cli/dist/cli.js +142 -9
- package/cli/dist/cli.js.map +1 -1
- package/cli/dist/commands/catalog.d.ts +13 -0
- package/cli/dist/commands/catalog.d.ts.map +1 -0
- package/cli/dist/commands/catalog.js +104 -0
- package/cli/dist/commands/catalog.js.map +1 -0
- package/cli/dist/commands/gui.d.ts +6 -0
- package/cli/dist/commands/gui.d.ts.map +1 -0
- package/cli/dist/commands/gui.js +211 -0
- package/cli/dist/commands/gui.js.map +1 -0
- package/cli/dist/commands/init.d.ts.map +1 -1
- package/cli/dist/commands/init.js +12 -0
- package/cli/dist/commands/init.js.map +1 -1
- package/cli/dist/commands/install-rules.d.ts +14 -0
- package/cli/dist/commands/install-rules.d.ts.map +1 -0
- package/cli/dist/commands/install-rules.js +127 -0
- package/cli/dist/commands/install-rules.js.map +1 -0
- package/cli/dist/commands/link.d.ts.map +1 -1
- package/cli/dist/commands/link.js +9 -11
- package/cli/dist/commands/link.js.map +1 -1
- package/cli/dist/commands/list.d.ts.map +1 -1
- package/cli/dist/commands/list.js +11 -28
- package/cli/dist/commands/list.js.map +1 -1
- package/cli/dist/commands/mcp.d.ts +48 -0
- package/cli/dist/commands/mcp.d.ts.map +1 -0
- package/cli/dist/commands/mcp.js +229 -0
- package/cli/dist/commands/mcp.js.map +1 -0
- package/cli/dist/commands/self-remove.d.ts +7 -0
- package/cli/dist/commands/self-remove.d.ts.map +1 -0
- package/cli/dist/commands/self-remove.js +179 -0
- package/cli/dist/commands/self-remove.js.map +1 -0
- package/cli/dist/commands/show.d.ts.map +1 -1
- package/cli/dist/commands/show.js +29 -99
- package/cli/dist/commands/show.js.map +1 -1
- package/cli/dist/commands/skill.d.ts +67 -0
- package/cli/dist/commands/skill.d.ts.map +1 -0
- package/cli/dist/commands/skill.js +513 -0
- package/cli/dist/commands/skill.js.map +1 -0
- package/cli/dist/commands/unlink.d.ts +6 -0
- package/cli/dist/commands/unlink.d.ts.map +1 -0
- package/cli/dist/commands/unlink.js +115 -0
- package/cli/dist/commands/unlink.js.map +1 -0
- package/cli/dist/commands/validate.d.ts +6 -0
- package/cli/dist/commands/validate.d.ts.map +1 -0
- package/cli/dist/commands/validate.js +159 -0
- package/cli/dist/commands/validate.js.map +1 -0
- package/cli/dist/utils/catalog-sync.d.ts +22 -0
- package/cli/dist/utils/catalog-sync.d.ts.map +1 -0
- package/cli/dist/utils/catalog-sync.js +157 -0
- package/cli/dist/utils/catalog-sync.js.map +1 -0
- package/cli/dist/utils/character-count.d.ts +56 -0
- package/cli/dist/utils/character-count.d.ts.map +1 -0
- package/cli/dist/utils/character-count.js +190 -0
- package/cli/dist/utils/character-count.js.map +1 -0
- package/cli/dist/utils/documentation-validator.d.ts +18 -0
- package/cli/dist/utils/documentation-validator.d.ts.map +1 -0
- package/cli/dist/utils/documentation-validator.js +233 -0
- package/cli/dist/utils/documentation-validator.js.map +1 -0
- package/cli/dist/utils/install-rules.d.ts +32 -0
- package/cli/dist/utils/install-rules.d.ts.map +1 -0
- package/cli/dist/utils/install-rules.js +375 -0
- package/cli/dist/utils/install-rules.js.map +1 -0
- package/cli/dist/utils/mcp-integration.d.ts +70 -0
- package/cli/dist/utils/mcp-integration.d.ts.map +1 -0
- package/cli/dist/utils/mcp-integration.js +292 -0
- package/cli/dist/utils/mcp-integration.js.map +1 -0
- package/cli/dist/utils/module-system.d.ts +153 -0
- package/cli/dist/utils/module-system.d.ts.map +1 -0
- package/cli/dist/utils/module-system.js +528 -0
- package/cli/dist/utils/module-system.js.map +1 -0
- package/cli/dist/utils/modules-catalog.d.ts +33 -0
- package/cli/dist/utils/modules-catalog.d.ts.map +1 -0
- package/cli/dist/utils/modules-catalog.js +163 -0
- package/cli/dist/utils/modules-catalog.js.map +1 -0
- package/cli/dist/utils/rule-install-hooks.d.ts +19 -0
- package/cli/dist/utils/rule-install-hooks.d.ts.map +1 -0
- package/cli/dist/utils/rule-install-hooks.js +224 -0
- package/cli/dist/utils/rule-install-hooks.js.map +1 -0
- package/cli/dist/utils/skill-system.d.ts +95 -0
- package/cli/dist/utils/skill-system.d.ts.map +1 -0
- package/cli/dist/utils/skill-system.js +313 -0
- package/cli/dist/utils/skill-system.js.map +1 -0
- package/modules.md +518 -106
- package/package.json +12 -3
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# PowerShell Automation Scripts
|
|
2
|
+
|
|
3
|
+
Best practices for task automation, scheduled tasks, and output formatting.
|
|
4
|
+
|
|
5
|
+
## Script Structure
|
|
6
|
+
|
|
7
|
+
### Complete Automation Script Template
|
|
8
|
+
|
|
9
|
+
```powershell
|
|
10
|
+
#Requires -Version 7.4
|
|
11
|
+
<#
|
|
12
|
+
.SYNOPSIS
|
|
13
|
+
Brief description of what the script does
|
|
14
|
+
|
|
15
|
+
.DESCRIPTION
|
|
16
|
+
Detailed description of the script's purpose and functionality
|
|
17
|
+
|
|
18
|
+
.PARAMETER LogPath
|
|
19
|
+
Path to the log file
|
|
20
|
+
|
|
21
|
+
.PARAMETER EmailRecipient
|
|
22
|
+
Email address for notifications
|
|
23
|
+
|
|
24
|
+
.EXAMPLE
|
|
25
|
+
.\Backup-Database.ps1 -LogPath "C:\Logs\backup.log"
|
|
26
|
+
|
|
27
|
+
.NOTES
|
|
28
|
+
Author: Your Name
|
|
29
|
+
Date: 2024-01-30
|
|
30
|
+
Version: 1.0
|
|
31
|
+
#>
|
|
32
|
+
|
|
33
|
+
[CmdletBinding()]
|
|
34
|
+
param(
|
|
35
|
+
[Parameter(Mandatory)]
|
|
36
|
+
[ValidateScript({ Test-Path (Split-Path $_) })]
|
|
37
|
+
[string]$LogPath,
|
|
38
|
+
|
|
39
|
+
[Parameter()]
|
|
40
|
+
[ValidatePattern('^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$')]
|
|
41
|
+
[string]$EmailRecipient
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
Set-StrictMode -Version Latest
|
|
45
|
+
$ErrorActionPreference = 'Stop'
|
|
46
|
+
|
|
47
|
+
# Script logic here
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Logging
|
|
51
|
+
|
|
52
|
+
### Structured Logging Function
|
|
53
|
+
|
|
54
|
+
```powershell
|
|
55
|
+
function Write-AutomationLog
|
|
56
|
+
{
|
|
57
|
+
[CmdletBinding()]
|
|
58
|
+
param(
|
|
59
|
+
[Parameter(Mandatory)]
|
|
60
|
+
[string]$Message,
|
|
61
|
+
|
|
62
|
+
[ValidateSet('Info', 'Warning', 'Error', 'Success')]
|
|
63
|
+
[string]$Level = 'Info',
|
|
64
|
+
|
|
65
|
+
[string]$LogPath = "$PSScriptRoot\automation.log"
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
$timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
|
|
69
|
+
$logEntry = "[$timestamp] [$Level] $Message"
|
|
70
|
+
|
|
71
|
+
# Console output with color
|
|
72
|
+
switch ($Level)
|
|
73
|
+
{
|
|
74
|
+
'Info' { Write-Host $logEntry -ForegroundColor Cyan }
|
|
75
|
+
'Warning' { Write-Warning $logEntry }
|
|
76
|
+
'Error' { Write-Error $logEntry }
|
|
77
|
+
'Success' { Write-Host $logEntry -ForegroundColor Green }
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
# File output
|
|
81
|
+
Add-Content -Path $LogPath -Value $logEntry
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
# Usage
|
|
85
|
+
Write-AutomationLog -Message "Starting backup process" -Level Info
|
|
86
|
+
Write-AutomationLog -Message "Backup completed successfully" -Level Success
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Error Handling for Automation
|
|
90
|
+
|
|
91
|
+
### Robust Error Handling
|
|
92
|
+
|
|
93
|
+
```powershell
|
|
94
|
+
try
|
|
95
|
+
{
|
|
96
|
+
Write-AutomationLog "Starting database backup"
|
|
97
|
+
|
|
98
|
+
# Backup logic
|
|
99
|
+
Backup-SqlDatabase -ServerInstance "localhost" -Database "MyDB"
|
|
100
|
+
|
|
101
|
+
Write-AutomationLog "Backup completed successfully" -Level Success
|
|
102
|
+
}
|
|
103
|
+
catch
|
|
104
|
+
{
|
|
105
|
+
$errorMessage = "Backup failed: $($_.Exception.Message)"
|
|
106
|
+
Write-AutomationLog $errorMessage -Level Error
|
|
107
|
+
|
|
108
|
+
# Send notification
|
|
109
|
+
Send-MailMessage -To $EmailRecipient -Subject "Backup Failed" -Body $errorMessage
|
|
110
|
+
|
|
111
|
+
# Exit with error code
|
|
112
|
+
exit 1
|
|
113
|
+
}
|
|
114
|
+
finally
|
|
115
|
+
{
|
|
116
|
+
Write-AutomationLog "Backup process finished"
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Scheduled Task Integration
|
|
121
|
+
|
|
122
|
+
### Create Scheduled Task
|
|
123
|
+
|
|
124
|
+
```powershell
|
|
125
|
+
# Register scheduled task
|
|
126
|
+
$action = New-ScheduledTaskAction -Execute 'PowerShell.exe' `
|
|
127
|
+
-Argument '-NoProfile -ExecutionPolicy Bypass -File "C:\Scripts\Backup-Database.ps1"'
|
|
128
|
+
|
|
129
|
+
$trigger = New-ScheduledTaskTrigger -Daily -At 2am
|
|
130
|
+
|
|
131
|
+
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
|
|
132
|
+
|
|
133
|
+
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -RestartCount 3 -RestartInterval (New-TimeSpan -Minutes 5)
|
|
134
|
+
|
|
135
|
+
Register-ScheduledTask -TaskName "Daily Database Backup" `
|
|
136
|
+
-Action $action `
|
|
137
|
+
-Trigger $trigger `
|
|
138
|
+
-Principal $principal `
|
|
139
|
+
-Settings $settings `
|
|
140
|
+
-Description "Automated daily database backup"
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Script for Scheduled Execution
|
|
144
|
+
|
|
145
|
+
```powershell
|
|
146
|
+
#Requires -Version 7.4
|
|
147
|
+
#Requires -RunAsAdministrator
|
|
148
|
+
|
|
149
|
+
[CmdletBinding()]
|
|
150
|
+
param()
|
|
151
|
+
|
|
152
|
+
Set-StrictMode -Version Latest
|
|
153
|
+
$ErrorActionPreference = 'Stop'
|
|
154
|
+
|
|
155
|
+
# Set up transcript logging
|
|
156
|
+
$transcriptPath = "C:\Logs\Backup-$(Get-Date -Format 'yyyyMMdd-HHmmss').log"
|
|
157
|
+
Start-Transcript -Path $transcriptPath
|
|
158
|
+
|
|
159
|
+
try
|
|
160
|
+
{
|
|
161
|
+
# Your automation logic
|
|
162
|
+
Write-Output "Starting automated task at $(Get-Date)"
|
|
163
|
+
|
|
164
|
+
# Perform work
|
|
165
|
+
|
|
166
|
+
Write-Output "Task completed successfully at $(Get-Date)"
|
|
167
|
+
exit 0
|
|
168
|
+
}
|
|
169
|
+
catch
|
|
170
|
+
{
|
|
171
|
+
Write-Error "Task failed: $_"
|
|
172
|
+
exit 1
|
|
173
|
+
}
|
|
174
|
+
finally
|
|
175
|
+
{
|
|
176
|
+
Stop-Transcript
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Output Formatting
|
|
181
|
+
|
|
182
|
+
### Structured Output
|
|
183
|
+
|
|
184
|
+
```powershell
|
|
185
|
+
# Return objects, not strings
|
|
186
|
+
function Get-BackupStatus
|
|
187
|
+
{
|
|
188
|
+
[CmdletBinding()]
|
|
189
|
+
param()
|
|
190
|
+
|
|
191
|
+
[PSCustomObject]@{
|
|
192
|
+
Timestamp = Get-Date
|
|
193
|
+
Status = 'Success'
|
|
194
|
+
FilesBackedUp = 150
|
|
195
|
+
TotalSize = '2.5 GB'
|
|
196
|
+
Duration = '00:05:23'
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
# Usage
|
|
201
|
+
$status = Get-BackupStatus
|
|
202
|
+
$status | Format-Table
|
|
203
|
+
$status | Export-Csv -Path "backup-status.csv" -NoTypeInformation
|
|
204
|
+
$status | ConvertTo-Json | Out-File "backup-status.json"
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Progress Reporting
|
|
208
|
+
|
|
209
|
+
```powershell
|
|
210
|
+
$items = 1..100
|
|
211
|
+
$totalItems = $items.Count
|
|
212
|
+
|
|
213
|
+
foreach ($i in 0..($totalItems - 1))
|
|
214
|
+
{
|
|
215
|
+
$percentComplete = ($i / $totalItems) * 100
|
|
216
|
+
|
|
217
|
+
Write-Progress -Activity "Processing Items" `
|
|
218
|
+
-Status "Processing item $($i + 1) of $totalItems" `
|
|
219
|
+
-PercentComplete $percentComplete
|
|
220
|
+
|
|
221
|
+
# Process item
|
|
222
|
+
Start-Sleep -Milliseconds 100
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
Write-Progress -Activity "Processing Items" -Completed
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## Best Practices
|
|
229
|
+
|
|
230
|
+
1. **Use comment-based help** - Document all parameters and examples
|
|
231
|
+
2. **Enable strict mode** - Catch errors early
|
|
232
|
+
3. **Implement logging** - Track execution and errors
|
|
233
|
+
4. **Handle errors gracefully** - Use try/catch/finally
|
|
234
|
+
5. **Return objects** - Not formatted strings
|
|
235
|
+
6. **Use transcript logging** - For scheduled tasks
|
|
236
|
+
7. **Set exit codes** - 0 for success, non-zero for failure
|
|
237
|
+
8. **Validate parameters** - Use validation attributes
|
|
238
|
+
9. **Send notifications** - Email or webhook on failure
|
|
239
|
+
10. **Test thoroughly** - Before scheduling
|
|
240
|
+
|
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
# PowerShell Cloud Orchestration
|
|
2
|
+
|
|
3
|
+
Best practices for Azure/AWS PowerShell, ARM/Bicep integration, and authentication patterns.
|
|
4
|
+
|
|
5
|
+
## Azure PowerShell
|
|
6
|
+
|
|
7
|
+
### Authentication
|
|
8
|
+
|
|
9
|
+
```powershell
|
|
10
|
+
#Requires -Version 7.4
|
|
11
|
+
#Requires -Modules Az.Accounts, Az.Resources
|
|
12
|
+
|
|
13
|
+
# ✅ GOOD: Use managed identity in Azure
|
|
14
|
+
function Connect-AzureWithManagedIdentity
|
|
15
|
+
{
|
|
16
|
+
[CmdletBinding()]
|
|
17
|
+
param()
|
|
18
|
+
|
|
19
|
+
try
|
|
20
|
+
{
|
|
21
|
+
# Connect using managed identity
|
|
22
|
+
Connect-AzAccount -Identity -ErrorAction Stop
|
|
23
|
+
Write-Verbose "Connected to Azure using Managed Identity"
|
|
24
|
+
}
|
|
25
|
+
catch
|
|
26
|
+
{
|
|
27
|
+
Write-Error "Failed to connect with Managed Identity: $_"
|
|
28
|
+
throw
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
# ✅ GOOD: Use service principal for automation
|
|
33
|
+
function Connect-AzureWithServicePrincipal
|
|
34
|
+
{
|
|
35
|
+
[CmdletBinding()]
|
|
36
|
+
param(
|
|
37
|
+
[Parameter(Mandatory)]
|
|
38
|
+
[string]$TenantId,
|
|
39
|
+
|
|
40
|
+
[Parameter(Mandatory)]
|
|
41
|
+
[string]$ApplicationId,
|
|
42
|
+
|
|
43
|
+
[Parameter(Mandatory)]
|
|
44
|
+
[PSCredential]$Credential
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
$params = @{
|
|
48
|
+
ServicePrincipal = $true
|
|
49
|
+
TenantId = $TenantId
|
|
50
|
+
Credential = $Credential
|
|
51
|
+
ApplicationId = $ApplicationId
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
Connect-AzAccount @params -ErrorAction Stop
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
# ❌ BAD: Interactive login in automation
|
|
58
|
+
# Connect-AzAccount # Don't use in scripts
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Resource Management
|
|
62
|
+
|
|
63
|
+
```powershell
|
|
64
|
+
function New-AzureResourceGroup
|
|
65
|
+
{
|
|
66
|
+
[CmdletBinding(SupportsShouldProcess)]
|
|
67
|
+
param(
|
|
68
|
+
[Parameter(Mandatory)]
|
|
69
|
+
[ValidatePattern('^[a-zA-Z0-9-_]+$')]
|
|
70
|
+
[string]$ResourceGroupName,
|
|
71
|
+
|
|
72
|
+
[Parameter(Mandatory)]
|
|
73
|
+
[ValidateSet('eastus', 'westus', 'centralus', 'northeurope', 'westeurope')]
|
|
74
|
+
[string]$Location,
|
|
75
|
+
|
|
76
|
+
[Parameter()]
|
|
77
|
+
[hashtable]$Tags = @{}
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
if ($PSCmdlet.ShouldProcess($ResourceGroupName, 'Create Resource Group'))
|
|
81
|
+
{
|
|
82
|
+
try
|
|
83
|
+
{
|
|
84
|
+
$params = @{
|
|
85
|
+
Name = $ResourceGroupName
|
|
86
|
+
Location = $Location
|
|
87
|
+
Tag = $Tags
|
|
88
|
+
ErrorAction = 'Stop'
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
$rg = New-AzResourceGroup @params
|
|
92
|
+
Write-Verbose "Created resource group: $($rg.ResourceGroupName)"
|
|
93
|
+
return $rg
|
|
94
|
+
}
|
|
95
|
+
catch
|
|
96
|
+
{
|
|
97
|
+
Write-Error "Failed to create resource group: $_"
|
|
98
|
+
throw
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### ARM Template Deployment
|
|
105
|
+
|
|
106
|
+
```powershell
|
|
107
|
+
function Deploy-AzureARMTemplate
|
|
108
|
+
{
|
|
109
|
+
[CmdletBinding()]
|
|
110
|
+
param(
|
|
111
|
+
[Parameter(Mandatory)]
|
|
112
|
+
[string]$ResourceGroupName,
|
|
113
|
+
|
|
114
|
+
[Parameter(Mandatory)]
|
|
115
|
+
[ValidateScript({ Test-Path $_ -PathType Leaf })]
|
|
116
|
+
[string]$TemplateFile,
|
|
117
|
+
|
|
118
|
+
[Parameter()]
|
|
119
|
+
[ValidateScript({ Test-Path $_ -PathType Leaf })]
|
|
120
|
+
[string]$ParametersFile,
|
|
121
|
+
|
|
122
|
+
[Parameter()]
|
|
123
|
+
[string]$DeploymentName = "deployment-$(Get-Date -Format 'yyyyMMdd-HHmmss')"
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
try
|
|
127
|
+
{
|
|
128
|
+
$deployParams = @{
|
|
129
|
+
ResourceGroupName = $ResourceGroupName
|
|
130
|
+
TemplateFile = $TemplateFile
|
|
131
|
+
Name = $DeploymentName
|
|
132
|
+
Mode = 'Incremental'
|
|
133
|
+
ErrorAction = 'Stop'
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if ($ParametersFile)
|
|
137
|
+
{
|
|
138
|
+
$deployParams['TemplateParameterFile'] = $ParametersFile
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
# Validate template first
|
|
142
|
+
$validation = Test-AzResourceGroupDeployment @deployParams
|
|
143
|
+
if ($validation)
|
|
144
|
+
{
|
|
145
|
+
Write-Error "Template validation failed: $($validation.Message)"
|
|
146
|
+
return
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
# Deploy template
|
|
150
|
+
$deployment = New-AzResourceGroupDeployment @deployParams -Verbose
|
|
151
|
+
Write-Output "Deployment completed: $($deployment.ProvisioningState)"
|
|
152
|
+
return $deployment
|
|
153
|
+
}
|
|
154
|
+
catch
|
|
155
|
+
{
|
|
156
|
+
Write-Error "Deployment failed: $_"
|
|
157
|
+
throw
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Bicep Integration
|
|
163
|
+
|
|
164
|
+
```powershell
|
|
165
|
+
function Deploy-BicepTemplate
|
|
166
|
+
{
|
|
167
|
+
[CmdletBinding()]
|
|
168
|
+
param(
|
|
169
|
+
[Parameter(Mandatory)]
|
|
170
|
+
[string]$ResourceGroupName,
|
|
171
|
+
|
|
172
|
+
[Parameter(Mandatory)]
|
|
173
|
+
[ValidateScript({ Test-Path $_ -PathType Leaf })]
|
|
174
|
+
[string]$BicepFile,
|
|
175
|
+
|
|
176
|
+
[Parameter()]
|
|
177
|
+
[hashtable]$Parameters = @{}
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
try
|
|
181
|
+
{
|
|
182
|
+
# Build Bicep to ARM
|
|
183
|
+
$armTemplate = $BicepFile -replace '\.bicep$', '.json'
|
|
184
|
+
bicep build $BicepFile --outfile $armTemplate
|
|
185
|
+
|
|
186
|
+
if (-not (Test-Path $armTemplate))
|
|
187
|
+
{
|
|
188
|
+
throw "Bicep build failed"
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
# Deploy ARM template
|
|
192
|
+
$deployParams = @{
|
|
193
|
+
ResourceGroupName = $ResourceGroupName
|
|
194
|
+
TemplateFile = $armTemplate
|
|
195
|
+
TemplateParameterObject = $Parameters
|
|
196
|
+
ErrorAction = 'Stop'
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
New-AzResourceGroupDeployment @deployParams -Verbose
|
|
200
|
+
}
|
|
201
|
+
finally
|
|
202
|
+
{
|
|
203
|
+
# Clean up generated ARM template
|
|
204
|
+
if (Test-Path $armTemplate)
|
|
205
|
+
{
|
|
206
|
+
Remove-Item $armTemplate -Force
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## AWS PowerShell
|
|
213
|
+
|
|
214
|
+
### Authentication
|
|
215
|
+
|
|
216
|
+
```powershell
|
|
217
|
+
#Requires -Modules AWS.Tools.Common, AWS.Tools.EC2
|
|
218
|
+
|
|
219
|
+
# ✅ GOOD: Use IAM role in EC2
|
|
220
|
+
function Initialize-AWSCredentials
|
|
221
|
+
{
|
|
222
|
+
[CmdletBinding()]
|
|
223
|
+
param(
|
|
224
|
+
[Parameter()]
|
|
225
|
+
[string]$ProfileName,
|
|
226
|
+
|
|
227
|
+
[Parameter()]
|
|
228
|
+
[string]$Region = 'us-east-1'
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
try
|
|
232
|
+
{
|
|
233
|
+
if ($ProfileName)
|
|
234
|
+
{
|
|
235
|
+
# Use named profile
|
|
236
|
+
Set-AWSCredential -ProfileName $ProfileName
|
|
237
|
+
}
|
|
238
|
+
else
|
|
239
|
+
{
|
|
240
|
+
# Use instance profile (IAM role)
|
|
241
|
+
$instanceProfile = Invoke-RestMethod -Uri 'http://169.254.169.254/latest/meta-data/iam/security-credentials/' -TimeoutSec 2
|
|
242
|
+
Write-Verbose "Using instance profile: $instanceProfile"
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
Set-DefaultAWSRegion -Region $Region
|
|
246
|
+
Write-Verbose "AWS credentials initialized for region: $Region"
|
|
247
|
+
}
|
|
248
|
+
catch
|
|
249
|
+
{
|
|
250
|
+
Write-Error "Failed to initialize AWS credentials: $_"
|
|
251
|
+
throw
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### EC2 Instance Management
|
|
257
|
+
|
|
258
|
+
```powershell
|
|
259
|
+
function New-EC2InstanceWithTags
|
|
260
|
+
{
|
|
261
|
+
[CmdletBinding(SupportsShouldProcess)]
|
|
262
|
+
param(
|
|
263
|
+
[Parameter(Mandatory)]
|
|
264
|
+
[string]$ImageId,
|
|
265
|
+
|
|
266
|
+
[Parameter(Mandatory)]
|
|
267
|
+
[string]$InstanceType,
|
|
268
|
+
|
|
269
|
+
[Parameter(Mandatory)]
|
|
270
|
+
[string]$KeyName,
|
|
271
|
+
|
|
272
|
+
[Parameter()]
|
|
273
|
+
[hashtable]$Tags = @{}
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
if ($PSCmdlet.ShouldProcess("EC2 Instance", "Create"))
|
|
277
|
+
{
|
|
278
|
+
try
|
|
279
|
+
{
|
|
280
|
+
# Create instance
|
|
281
|
+
$instance = New-EC2Instance -ImageId $ImageId `
|
|
282
|
+
-InstanceType $InstanceType `
|
|
283
|
+
-KeyName $KeyName `
|
|
284
|
+
-MinCount 1 `
|
|
285
|
+
-MaxCount 1
|
|
286
|
+
|
|
287
|
+
$instanceId = $instance.Instances[0].InstanceId
|
|
288
|
+
Write-Verbose "Created instance: $instanceId"
|
|
289
|
+
|
|
290
|
+
# Wait for instance to exist
|
|
291
|
+
Start-Sleep -Seconds 5
|
|
292
|
+
|
|
293
|
+
# Apply tags
|
|
294
|
+
$tagList = $Tags.GetEnumerator() | ForEach-Object {
|
|
295
|
+
[Amazon.EC2.Model.Tag]@{
|
|
296
|
+
Key = $_.Key
|
|
297
|
+
Value = $_.Value
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
New-EC2Tag -Resource $instanceId -Tag $tagList
|
|
302
|
+
Write-Output "Instance $instanceId created and tagged"
|
|
303
|
+
|
|
304
|
+
return $instanceId
|
|
305
|
+
}
|
|
306
|
+
catch
|
|
307
|
+
{
|
|
308
|
+
Write-Error "Failed to create EC2 instance: $_"
|
|
309
|
+
throw
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
## Error Handling and Retry Logic
|
|
316
|
+
|
|
317
|
+
### Implement Retry for Transient Failures
|
|
318
|
+
|
|
319
|
+
```powershell
|
|
320
|
+
function Invoke-CloudCommandWithRetry
|
|
321
|
+
{
|
|
322
|
+
[CmdletBinding()]
|
|
323
|
+
param(
|
|
324
|
+
[Parameter(Mandatory)]
|
|
325
|
+
[scriptblock]$ScriptBlock,
|
|
326
|
+
|
|
327
|
+
[Parameter()]
|
|
328
|
+
[int]$MaxRetries = 3,
|
|
329
|
+
|
|
330
|
+
[Parameter()]
|
|
331
|
+
[int]$RetryDelaySeconds = 5
|
|
332
|
+
)
|
|
333
|
+
|
|
334
|
+
$attempt = 0
|
|
335
|
+
$success = $false
|
|
336
|
+
|
|
337
|
+
while (-not $success -and $attempt -lt $MaxRetries)
|
|
338
|
+
{
|
|
339
|
+
$attempt++
|
|
340
|
+
|
|
341
|
+
try
|
|
342
|
+
{
|
|
343
|
+
$result = & $ScriptBlock
|
|
344
|
+
$success = $true
|
|
345
|
+
return $result
|
|
346
|
+
}
|
|
347
|
+
catch
|
|
348
|
+
{
|
|
349
|
+
Write-Warning "Attempt $attempt failed: $_"
|
|
350
|
+
|
|
351
|
+
if ($attempt -lt $MaxRetries)
|
|
352
|
+
{
|
|
353
|
+
Write-Verbose "Retrying in $RetryDelaySeconds seconds..."
|
|
354
|
+
Start-Sleep -Seconds $RetryDelaySeconds
|
|
355
|
+
}
|
|
356
|
+
else
|
|
357
|
+
{
|
|
358
|
+
Write-Error "All $MaxRetries attempts failed"
|
|
359
|
+
throw
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
# Usage
|
|
366
|
+
$result = Invoke-CloudCommandWithRetry -ScriptBlock {
|
|
367
|
+
Get-AzVM -ResourceGroupName 'MyRG' -Name 'MyVM'
|
|
368
|
+
} -MaxRetries 3
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
## Best Practices
|
|
372
|
+
|
|
373
|
+
1. **Use managed identities** - Avoid storing credentials
|
|
374
|
+
2. **Implement retry logic** - Handle transient failures
|
|
375
|
+
3. **Validate before deployment** - Test templates first
|
|
376
|
+
4. **Use parameter splatting** - Improve readability
|
|
377
|
+
5. **Tag all resources** - For cost tracking and organization
|
|
378
|
+
6. **Use specific modules** - Import only what you need
|
|
379
|
+
7. **Handle regions properly** - Set default region
|
|
380
|
+
8. **Implement proper logging** - Track all operations
|
|
381
|
+
9. **Use ShouldProcess** - For destructive operations
|
|
382
|
+
10. **Clean up resources** - Remove temporary resources in finally blocks
|
|
383
|
+
|
|
384
|
+
|