updates and improvements

This commit is contained in:
secretspecter 2023-06-24 13:03:29 -06:00
parent d347d67671
commit 63f69fe22a
6 changed files with 73 additions and 34 deletions

View file

@ -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)

View file

@ -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"
}
}

View file

@ -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) {

22
Scripts/Stream.ps1 Normal file
View file

@ -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"
}

View file

@ -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

View file

@ -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