From 63f69fe22a6aad1d3e9dac2be484767321ce3574 Mon Sep 17 00:00:00 2001 From: secretspecter Date: Sat, 24 Jun 2023 13:03:29 -0600 Subject: [PATCH] updates and improvements --- CrashCourse.md | 35 +++++++++++++++++++++-------------- Scripts/Download-Video.ps1 | 8 ++++---- Scripts/Scale-Pixel.ps1 | 6 +++--- Scripts/Stream.ps1 | 22 ++++++++++++++++++++++ Scripts/Update-And-Clean.ps1 | 34 ++++++++++++++++++++++------------ profile.ps1 | 2 +- 6 files changed, 73 insertions(+), 34 deletions(-) create mode 100644 Scripts/Stream.ps1 diff --git a/CrashCourse.md b/CrashCourse.md index b067178..b7f555b 100644 --- a/CrashCourse.md +++ b/CrashCourse.md @@ -11,6 +11,17 @@ notepad "$(Split-Path -Parent $PROFILE.CurrentUserAllHosts)\my-script.ps1" codium "$(Split-Path -Parent $PROFILE.CurrentUserAllHosts)\my-script.ps1" ``` +# Get-Help + +You can learn about pretty much anything in PowerShell with the `Get-Help` cmdlet. + +```powershell +Update-Help +Get-Help Get-Help +Get-Help New-Item +Get-Help Get-ChildItem +``` + # variables [PowerShell is as case-insensitive as possible.](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_case-sensitivity?view=powershell-7.3) This means `$MyInvocation` works just the same as `$mYinVoCAtiOn` among other things. Coming from a Unix background and preferring some consistency I like to pretend it is case-sensitive so in this repo I'll strive for that but it's worth keeping in mind. Especially since **the Microsoft docs use case a lot** in variable and cmdlet names. @@ -38,6 +49,11 @@ else { Write-Host "value is not a y" } +if (-not $some_switch) { + # a bang ! also works... +} + +$num = 0 while ($num -lt 50) { $num = $num + 1 } @@ -59,10 +75,11 @@ There are [a lot of different powers](https://learn.microsoft.com/en-us/powershe ```powershell # Name-And-Age.ps1 -if ($silent) { exit } -Write-Host "Hello $name" -if ($age) { - Write-Host "according to the -age you provided you are: $age" +if (-not $silent) { + Write-Host "Hello $name" + if ($age) { + Write-Host "according to the -age you provided you are: $age" + } } ``` @@ -74,16 +91,6 @@ Name-And-Age.ps1 -name "Rose" -age "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 use `param` too (instead of parenthesis like other languages). The last value is the `return` result, (which you can also use to `return` early... or just regularly if you want) diff --git a/Scripts/Download-Video.ps1 b/Scripts/Download-Video.ps1 index c38355e..4dcf86e 100644 --- a/Scripts/Download-Video.ps1 +++ b/Scripts/Download-Video.ps1 @@ -5,8 +5,8 @@ param( [int]$wait = 60 * 10 ) -if (!(Get-Command "yt-dlp.exe" -ErrorAction SilentlyContinue)) { - Write-Host "There's no yt-dlp.exe to work with!" +if (-not (Get-Command "yt-dlp" -ErrorAction SilentlyContinue)) { + Write-Host "There's no yt-dlp to work with!" exit } @@ -14,7 +14,7 @@ if ($out -notmatch '\\$') { $out += "\" } -if (!(Test-Path $out -PathType container)) { +if (-not (Test-Path $out -PathType container)) { New-Item $out -ItemType container } @@ -28,7 +28,7 @@ if ($url) { else { while ($true) { $url = Read-host “URL” - if (!$url) { break } + if (-not $url) { break } Start-Process yt-dlp -ArgumentList "$ytdlp_options $url" } } diff --git a/Scripts/Scale-Pixel.ps1 b/Scripts/Scale-Pixel.ps1 index b7b38ee..8030d75 100644 --- a/Scripts/Scale-Pixel.ps1 +++ b/Scripts/Scale-Pixel.ps1 @@ -5,14 +5,14 @@ param( $scale = 128 ) -if (!(Get-Command "magick.exe" -ErrorAction SilentlyContinue)) { - Write-Host "There's no magick.exe to work with!" +if (-not (Get-Command "magick" -ErrorAction SilentlyContinue)) { + Write-Host "There's no magick to work with!" exit } if (Test-Path $in -PathType container) { $out_dir = "$out\x$scale\" - if (!(Test-Path $out_dir)) { + if (-not (Test-Path $out_dir)) { New-Item "$out_dir" -ItemType directory } foreach ($image in Get-ChildItem "$in*" -Include *.png, *.jpg, *.gif) { diff --git a/Scripts/Stream.ps1 b/Scripts/Stream.ps1 new file mode 100644 index 0000000..2321cc8 --- /dev/null +++ b/Scripts/Stream.ps1 @@ -0,0 +1,22 @@ +param( + [string]$url = "", + [string]$record = "~/Videos/Streamed/{title}.ts", + [int]$retryinterval = 30, + [string]$quality = "best", + [switch]$norecord +) + +if ($url) { + if (-not (Get-Command "streamlink" -ErrorAction SilentlyContinue)) { + Write-Host "There's no streamlink to work with!" + exit + } + + $streamlink_opts = "--default-stream $quality" + $streamlink_opts = " --retry-streams $retryinterval" + if ($out -and -not $norecord) { + $streamlink_opts = " --record $out" + } + + Start-Process streamlink -ArgumentList "$streamlink_opts $url" +} diff --git a/Scripts/Update-And-Clean.ps1 b/Scripts/Update-And-Clean.ps1 index bf88943..b3e7755 100644 --- a/Scripts/Update-And-Clean.ps1 +++ b/Scripts/Update-And-Clean.ps1 @@ -1,13 +1,25 @@ -param([switch]$elevated) +param( + [switch]$elevated, + [switch]$CleanOnly +) + +if ((Get-Command "scoop" -ErrorAction SilentlyContinue) -and -not $elevated) { + if (-not $CleanOnly) { + scoop update + scoop update --all + } + scoop cache rm * +} + # If not in an Administrator environment -If (!(New-Object Security.Principal.WindowsPrincipal $( +If (-not (New-Object Security.Principal.WindowsPrincipal $( [Security.Principal.WindowsIdentity]::GetCurrent() )).IsInRole( [Security.Principal.WindowsBuiltinRole]::Administrator ) ) { # and elevating hasn't already been tried - if (!$elevated) { + if (-not $elevated) { # attempt to become Administrator by re-invoking this script Start-Process PowerShell -Verb RunAs -ArgumentList ( '-file "{0}" -elevated' -f ($MyInvocation.MyCommand.Definition) @@ -17,17 +29,15 @@ If (!(New-Object Security.Principal.WindowsPrincipal $( exit } -if (!(Get-Module PSWindowsUpdate)) { - Install-Module PSWindowsUpdate + +if (-not $CleanOnly) { + if (-not (Get-Command "Get-WindowsUpdate")) { + Install-Module PSWindowsUpdate + } + Get-WindowsUpdate -AcceptAll -Install } -Get-WindowsUpdate -AcceptAll -Install -if (Get-Command "scoop.exe" -ErrorAction SilentlyContinue) { - scoop update - scoop update --all - scoop cache rm * -} -if (Get-Command "bleachbit.exe" -ErrorAction SilentlyContinue) { +if (Get-Command "bleachbit" -ErrorAction SilentlyContinue) { # Clean everything selected in the UI last time it was run. # So... run The UI to set things up initially. bleachbit --preset --clean diff --git a/profile.ps1 b/profile.ps1 index 380817f..84426e6 100644 --- a/profile.ps1 +++ b/profile.ps1 @@ -23,7 +23,7 @@ function Install-Scoop { } function Install-BleachBit { - if (!(Get-Command "git.exe" -ErrorAction SilentlyContinue)) { + if (-not (Get-Command "git" -ErrorAction SilentlyContinue)) { scoop install git } scoop bucket add extras