Skip to content

Instantly share code, notes, and snippets.

View SelfDestructingScheduledJobExample.ps1
# This will register a new job that removes itself from Get-ScheduledJob when finished
$Trigger = New-JobTrigger -Once -At (Get-Date).AddSeconds(3)
$TaskName = "SelfDestructingJob"
Register-ScheduledJob -Trigger $Trigger -Name $TaskName `
-ScriptBlock ([scriptblock]::Create(@"
("Started job at " + (Get-Date) + ", waiting...") | Out-File -FilePath 'C:\ProofThatItWorks.txt'
Start-Sleep 3
("Finished at " + (Get-Date)) | Out-File -Append -FilePath 'C:\ProofThatItWorks.txt'
Unregister-ScheduledJob -Name '$TaskName' -Force
@karlgluck
karlgluck / WaitForScheduledTaskExample.ps1
Last active May 15, 2022 21:18
Example of how to wait for a scheduled task to finish running
View WaitForScheduledTaskExample.ps1
$Trigger = New-JobTrigger -Once -At (get-date).AddSeconds(5)
$JobOptions = New-ScheduledJobOption -StartIfOnBattery -RunElevated
$Job = Register-ScheduledJob -Name "TaskName" -Trigger $Trigger -ScheduledJobOption $JobOptions `
-ScriptBlock {
Sleep 5
}
# From https://docs.microsoft.com/en-us/windows/win32/taskschd/task-scheduler-error-and-success-constants?redirectedfrom=MSDN
Set-Variable -Name "SCHED_S_TASK_HAS_NOT_RUN" -Value 0x00041303 -Option Constant
@karlgluck
karlgluck / RunAtLoginExample.ps1
Created May 15, 2022 19:02
How to register a script block to run with elevated privileges when the current user logs in
View RunAtLoginExample.ps1
$JobName = "RunAtLoginExample"
$Enabled = $True # set to $False and run to remove the job
# https://stackoverflow.com/questions/40569045/register-scheduledjob-as-the-system-account-without-having-to-pass-in-credentia
# Output: %LOCALAPPDATA%\Microsoft\Windows\PowerShell\ScheduledJobs\$JobName
$accountId = ((Get-WMIObject -class Win32_ComputerSystem | Select-Object -ExpandProperty username))
$Trigger = New-JobTrigger -AtLogOn
$JobOptions = New-ScheduledJobOption -StartIfOnBattery -RunElevated
@karlgluck
karlgluck / CreatePathIfDoesntExistExample.ps1
Created May 14, 2022 19:57
One-liner to create a path if it doesn't exist
View CreatePathIfDoesntExistExample.ps1
if (-not (Test-Path $Path)) { New-Item $Path -ItemType Directory | Out-Null }
@karlgluck
karlgluck / ExtractFileFromZipExample.ps1
Created May 14, 2022 17:46
How to extract a single file from a ZIP archive using Powershell
View ExtractFileFromZipExample.ps1
$ZipFilePath = ""
$FileNameInZip = ""
Add-Type -AssemblyName System.IO.Compression.FileSystem
$Zip = [System.IO.Compression.ZipFile]::OpenRead($ZipFilePath)
$Zip.Entries |
Where-Object { $_.Name -eq $FileNameInZip } |
ForEach-Object {
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, $_.Name, $true)
@karlgluck
karlgluck / DownloadFromPrivateGitHubRepoExample.ps1
Created May 14, 2022 17:32
One-liner for downloading from a private GitHub repository
View DownloadFromPrivateGitHubRepoExample.ps1
# Fill these in with your own values
$Username = "your_username"
$Branch = "main"
$PathToFile = "README.md"
# Go to [https://github.com/settings/tokens] and add a Personal Access Token with two scopes: "repo" and "read:org"
$GitHubAuthToken = "ghp_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
Invoke-WebRequest -Method Get -Uri "https://raw.githubusercontent.com/$Username/$Repository/$Branch/$PathToFile" -Headers @{Authorization="token $GitHubAuthToken"; 'Cache-Control'='no-cache'} | ForEach-Object { $_.Content } | Write-Host
@karlgluck
karlgluck / RoombaVictory.ps1
Created September 18, 2019 00:55
Play the Roomba's victory jingle using the Powershell beep. Because why not?
View RoombaVictory.ps1
[console]::beep(260,200)
[console]::beep(320,200)
[console]::beep(380,150)
[console]::beep(520,200)
Start-Sleep -Milliseconds 200
[console]::beep(520,150)
[console]::beep(700,800)
@karlgluck
karlgluck / PerfectPixelCamera.cs
Created September 13, 2018 22:35
Main file from the Perfect Pixel Camera asset on the Unity asset store, licensed with MIT https://assetstore.unity.com/packages/tools/camera/perfect-pixel-camera-by-gg-ez-100000
View PerfectPixelCamera.cs
// Copyright 2018 Karl Gluck
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
View Viewport to World Point Unity.cs
private static Vector3 ViewportToWorldPointUnity (Vector3 point, float fieldOfView, float aspect, Vector3 from, Vector3 to, Vector3 up)
{
// The zNear / zFar magic numbers are what Unity's viewport always uses regardless of camera settings
var matP = Matrix4x4.Perspective (fieldOfView, aspect, 0.6f /* zNear */, 1000f /* zFar */);
// For some reason we have to reverse these coordinates
from = -from;
to = -to;
@karlgluck
karlgluck / VectorMagnitude.md
Created October 13, 2017 05:11
Approximate magnitude of 2d and 3d vectors
View VectorMagnitude.md

Vec3 Magnitude

These C# implementations of approximate magnitude with no square roots are actually slower than calling a native implementation in Unity, but if this were implemented intelligently in C there's a good chance they would be faster. In any case, they're here because it was a pain to get the magic numbers.

Accurate to within 4.5%.


public static float MagnitudeFast (this Vector2 self)
    {