idiomatic-ish updates

This commit is contained in:
secretspecter 2023-06-22 09:37:38 -06:00
parent 1cb1769948
commit cbb41f876f
5 changed files with 26 additions and 7 deletions

View file

@ -58,6 +58,7 @@ param(
There are [a lot of different powers](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.3) behind the square bracket syntax, [including custom validation](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.3#validatescript-validation-attribute), but these are the basics. Use `[parameter(mandatory)]` to require one otherwise they are optional. The above param block could be used in a script like this: There are [a lot of different powers](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.3) behind the square bracket syntax, [including custom validation](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.3#validatescript-validation-attribute), but these are the basics. Use `[parameter(mandatory)]` to require one otherwise they are optional. The above param block could be used in a script like this:
```powershell ```powershell
# Name-And-Age.ps1
if ($silent) { exit } if ($silent) { exit }
Write-Host "Hello $name" Write-Host "Hello $name"
if ($age) { if ($age) {
@ -68,9 +69,19 @@ if ($age) {
And run like this: And run like this:
```powershell ```powershell
name_and_age.ps1 -name "Rose" -age "27" Name-And-Age.ps1 -name "Rose" -age "27"
# or, arguments are also positional! # or, arguments are also positional!
name_and_age.ps1 "Rose" "27" Name-And-Age.ps1 "Rose" "27"
```
# Get-Help
You can learn about the available `param` list for pretty much anything in PowerShell with the `Get-Help` cmdlet.
```powershell
Get-Help Get-Help
Get-Help New-Item
Get-Help Name-And-Age.ps1
``` ```
# functions # functions
@ -116,7 +127,7 @@ foreach ($image Get-ChildItem .\images\* -Include *.png) {
} }
``` ```
There are also a handful of useful functions for dealing with path strings by the names of [[Verb]-Path](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/?view=powershell-7.3). Here are some examples: There are also a handful of useful functions for dealing with path strings by the names of [[Verb]-Path](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/?view=powershell-7.3). Note that in PowerShell folders are `container`s and files are `leaf`s. Here are some examples:
```powershell ```powershell
# Get parent folder # Get parent folder

View file

@ -1,11 +1,19 @@
param( param(
[string]$url = "", [string]$url = "",
[validatescript({ Test-Path $_ -PathType Container })]
[system.io.fileinfo]$out = "~/Videos/Downloads/", [system.io.fileinfo]$out = "~/Videos/Downloads/",
[string]$namepattern = "%(title)s", [string]$namepattern = "%(title)s",
[int]$wait = 60 * 10 [int]$wait = 60 * 10
) )
if (!(Get-Command "yt-dlp.exe" -ErrorAction SilentlyContinue)) {
Write-Host "There's no yt-dlp.exe to work with!"
exit
}
if (!(Test-Path $out -PathType container)) {
New-Item $out -ItemType container
}
$ytdlp_options = "--live-from-start --sponsorblock-remove all" $ytdlp_options = "--live-from-start --sponsorblock-remove all"
$ytdlp_options += " --wait-for-video $wait" $ytdlp_options += " --wait-for-video $wait"
$ytdlp_options += " --output $out$namepattern.%(ext)s" $ytdlp_options += " --output $out$namepattern.%(ext)s"

View file

@ -63,9 +63,9 @@ $env:PATH += ";$(Split-Path -Parent $MyInvocation.MyCommand.Path)"
[`Split-Path`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/split-path?view=powershell-7.3) is a pretty useful cmdlet for working with paths. Here we're saying "give me the parent folder for the current command path" which is the `$PROFILE` folder in this context. `$MyInvocation` is from the [automatic variables](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.3#myinvocation) which are provided by PowerShell under-the-hood. [`Split-Path`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/split-path?view=powershell-7.3) is a pretty useful cmdlet for working with paths. Here we're saying "give me the parent folder for the current command path" which is the `$PROFILE` folder in this context. `$MyInvocation` is from the [automatic variables](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.3#myinvocation) which are provided by PowerShell under-the-hood.
## update_and_clean.ps1 ## Update-And-Clean.ps1
With this I've also added a [`update_and_clean.ps1`](./update_and_clean.ps1) script (in the `$PROFILE` folder) that will update Windows as well as `scoop` if it's installed and then run `bleachbit --preset --clean` (also only if installed) **which depends on whatever you've last checked in the BleachBit UI... so run that and set it up first!** The script will also try to re-invoke itself as Administrator if you're not in an elevated session already... which is usually. If you're familiar with Arch Linux you might say `yay`! With this I've also added a [`Update-And-Clean.ps1`](./Update-and-Clean.ps1) script (in the `$PROFILE` folder) that will update Windows as well as `scoop` if it's installed and then run `bleachbit --preset --clean` (also only if installed) **which depends on whatever you've last checked in the BleachBit UI... so run that and set it up first!** The script will also try to re-invoke itself as Administrator if you're not in an elevated session already... which is usually. If you're familiar with Arch Linux you might say `yay`!
Checkout the [Crash Course](./CrashCourse.md) for a terse guide on writing your own PowerShell scripts. Checkout the [Crash Course](./CrashCourse.md) for a terse guide on writing your own PowerShell scripts.

View file

@ -10,7 +10,7 @@ If (!(New-Object Security.Principal.WindowsPrincipal $(
if (!$elevated) { if (!$elevated) {
# attempt to become Administrator by re-invoking this script # attempt to become Administrator by re-invoking this script
Start-Process PowerShell -Verb RunAs -ArgumentList ( Start-Process PowerShell -Verb RunAs -ArgumentList (
'-noprofile -noexit -file "{0}" -elevated' -f ($MyInvocation.MyCommand.Definition) '-file "{0}" -elevated' -f ($MyInvocation.MyCommand.Definition)
) )
} }
# otherwise just exit. # otherwise just exit.