Skip to content

Instantly share code, notes, and snippets.

Avatar

Ryan Leap ryan-leap

View GitHub Profile
@ryan-leap
ryan-leap / Get-WmiObjectTimeout.ps1
Created Nov 2, 2019
Get-WmiObject with a timeout
View Get-WmiObjectTimeout.ps1
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
@ryan-leap
ryan-leap / Get-ExceptionErrorType.ps1
Last active Feb 2, 2020
Get the error type of an exception (so you can write code to catch that particular exception)
View Get-ExceptionErrorType.ps1
# 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!" }
View AutoCompleteExample.ps1
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-ProcessAncestry.ps1
Created Oct 24, 2020
Shows info about the current process, and it's parent, and it's parent...
View Get-ProcessAncestry.ps1
$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)