From cbb41f876fa4a4a54924cef31596a8e2fb65449d Mon Sep 17 00:00:00 2001 From: secretspecter Date: Thu, 22 Jun 2023 09:37:38 -0600 Subject: [PATCH] idiomatic-ish updates --- CrashCourse.md | 17 ++++++++++++++--- dl.ps1 => Download-Video.ps1 | 10 +++++++++- pixup.ps1 => Pixel-Scale.ps1 | 0 README.md | 4 ++-- update_and_clean.ps1 => Update-And-Clean.ps1 | 2 +- 5 files changed, 26 insertions(+), 7 deletions(-) rename dl.ps1 => Download-Video.ps1 (71%) rename pixup.ps1 => Pixel-Scale.ps1 (100%) rename update_and_clean.ps1 => Update-And-Clean.ps1 (90%) diff --git a/CrashCourse.md b/CrashCourse.md index c40f25a..b067178 100644 --- a/CrashCourse.md +++ b/CrashCourse.md @@ -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: ```powershell +# Name-And-Age.ps1 if ($silent) { exit } Write-Host "Hello $name" if ($age) { @@ -68,9 +69,19 @@ if ($age) { And run like this: ```powershell -name_and_age.ps1 -name "Rose" -age "27" +Name-And-Age.ps1 -name "Rose" -age "27" # 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 @@ -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 # Get parent folder diff --git a/dl.ps1 b/Download-Video.ps1 similarity index 71% rename from dl.ps1 rename to Download-Video.ps1 index fa56583..77d8ece 100644 --- a/dl.ps1 +++ b/Download-Video.ps1 @@ -1,11 +1,19 @@ param( [string]$url = "", - [validatescript({ Test-Path $_ -PathType Container })] [system.io.fileinfo]$out = "~/Videos/Downloads/", [string]$namepattern = "%(title)s", [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 += " --wait-for-video $wait" $ytdlp_options += " --output $out$namepattern.%(ext)s" diff --git a/pixup.ps1 b/Pixel-Scale.ps1 similarity index 100% rename from pixup.ps1 rename to Pixel-Scale.ps1 diff --git a/README.md b/README.md index 490bc83..8601202 100644 --- a/README.md +++ b/README.md @@ -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. -## 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. diff --git a/update_and_clean.ps1 b/Update-And-Clean.ps1 similarity index 90% rename from update_and_clean.ps1 rename to Update-And-Clean.ps1 index f1b25ab..fa94127 100644 --- a/update_and_clean.ps1 +++ b/Update-And-Clean.ps1 @@ -10,7 +10,7 @@ If (!(New-Object Security.Principal.WindowsPrincipal $( if (!$elevated) { # attempt to become Administrator by re-invoking this script 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.