Skip to content

Instantly share code, notes, and snippets.

View ryan-leap's full-sized avatar

Ryan Leap ryan-leap

View GitHub Profile
@ryan-leap
ryan-leap / Get-ProcessAncestry.ps1
Created October 24, 2020 13:24
Shows info about the current process, and it's parent, and it's parent...
$processId = $PID
do {
$thisProcess = Get-CimInstance -ClassName Win32_Process -Filter "ProcessId = $processId"
$thisProcess | Select-Object ProcessId,ParentProcessId,Name,CreationClassName,CreationDate,CommandLine
$processId = $thisProcess.ParentProcessId
} while ($null -ne $processId)
function Show-AutoComplete {
[CmdletBinding()]
param (
[ValidateSet('Apple','Orange','Banana','Kiwi','Strawberry')]
[string] $Fruit = 'Apple'
)
Write-Host "The chosen value is [$Fruit]"
}
@ryan-leap
ryan-leap / Get-ExceptionErrorType.ps1
Last active February 2, 2020 16:20
Get the error type of an exception (so you can write code to catch that particular exception)
# Quick bit of code to get the error type of an exception so that
# you can write code to catch that particular exception
Try { 1 / 0 } Catch { "Exception Error Type is: [$($_.Exception.GetType().FullName)]"}
# Running the above:
# Exception Error Type is: [System.Management.Automation.RuntimeException]
# Now use the output to catch that specific error type
Try { 1 / 0 } Catch [System.Management.Automation.RuntimeException] { "Caught it!" }
@ryan-leap
ryan-leap / Get-WmiObjectTimeout.ps1
Created November 2, 2019 16:30
Get-WmiObject with a timeout
Function Get-WmiObjectTimeout {
<#
.SYNOPSIS
Get-WmiObject call with a timeout
.PARAMETER ComputerName
Specifies the target computer for the management operation
.PARAMETER Namespace
Specifies the WMI repository namespace where the specified WMI class is located when used with the Class
parameter
.PARAMETER Class