Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| powershell_examples [2021/10/26 17:38] – mgupton | powershell_examples [2023/01/30 13:48] (current) – mgupton | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Powershell Examples ====== | ====== Powershell Examples ====== | ||
| + | * code snippets | ||
| - | === Basic script with parameters === | + | * [[https:// |
| + | * [[https:// | ||
| + | |||
| + | |||
| + | ====== Common Script Structure ====== | ||
| + | ==== Basic script with parameters | ||
| <code powershell> | <code powershell> | ||
| param( | param( | ||
| - | [Parameter(Mandatory=$true)] | + | [Parameter(Mandatory=$true)] [String]$src = " |
| - | [Parameter(Mandatory=$true)] | + | [Parameter(Mandatory=$true)] [String]$dest = " |
| + | [Parameter(Mandatory=$false)] [switch]$recurse | ||
| ) | ) | ||
| function main { | function main { | ||
| - | Write-Host | + | Write-Host |
| - | Write-Host $Param1 | + | Write-Host $src |
| - | Write-Host $Param2 | + | Write-Host $dest |
| } | } | ||
| main | main | ||
| </ | </ | ||
| + | |||
| + | === Alt Format === | ||
| + | |||
| + | <code powershell> | ||
| + | param( | ||
| + | [Parameter(Mandatory=$true)] | ||
| + | [string] | ||
| + | $src = " | ||
| + | [Parameter(Mandatory=$true)] | ||
| + | [string] | ||
| + | $dest = " | ||
| + | [Parameter(Mandatory=$false)] | ||
| + | [switch] | ||
| + | $recurse | ||
| + | ) | ||
| + | |||
| + | function main { | ||
| + | Write-Host $recurse | ||
| + | Write-Host $src | ||
| + | Write-Host $dest | ||
| + | } | ||
| + | |||
| + | main | ||
| + | </ | ||
| + | |||
| + | ====== Loop Over Files ====== | ||
| + | === Example of looping over files and doing something with them === | ||
| + | <code powershell> | ||
| + | param( | ||
| + | [Parameter(Mandatory=$true)] [String]$dir | ||
| + | ) | ||
| + | |||
| + | foreach ($file in Get-ChildItem -File $dir) { | ||
| + | jq -s -f .\filter.jq $dir\$file | ||
| + | } | ||
| + | </ | ||
| + | |||