Skip to content

Instantly share code, notes, and snippets.

@mattmcnabb
mattmcnabb / TransformTest1.psm1
Last active Nov 14, 2019
Can't quite get these transforms working...
View TransformTest1.psm1
# this works, but only if I import with using
class StringTransformAttribute : System.Management.Automation.ArgumentTransformationAttribute
{
[object] Transform([System.Management.Automation.EngineIntrinsics]$EngineIntrinsics, [object]$InputData)
{
return "a different string"
}
}
function Test-StringTransform
@mattmcnabb
mattmcnabb / Get-JsonWhois.ps1
Created Jul 8, 2019
Get IP Address WhoIs Info
View Get-JsonWhois.ps1
function Get-JsonWhois
{
[CmdletBinding(DefaultParameterSetName = "ip")]
param
(
[Parameter(Mandatory, ParameterSetName = "ip")]
[string]
$IPAddress,
[Parameter(Mandatory, ParameterSetName = "domain")]
@mattmcnabb
mattmcnabb / NamedParameters.ps1
Last active Jul 5, 2019
New up an object with default constructor
View NamedParameters.ps1
# default constructor - has no parameters
# method 1 - new and then set properties
$Configuration = [Foo.ConfigurationObject]::new()
$Configuration.Domain = $DomainName
$Configuration.Token = $Token
# method 2 - add properties via New-Object
$Configuration = New-Object Foo.ConfigurationObject -Property @{
Domain = $DomainName
@mattmcnabb
mattmcnabb / Start-ControlPanelApplet.ps1
Created Dec 14, 2018
Launch windows control panel applets from PowerShell
View Start-ControlPanelApplet.ps1
$ControlPanelApplets = [ordered]@{
"Add a Device wizard" = "$env:windir\System32\DevicePairingWizard.exe"
"Add Hardware wizard" = "$env:windir\System32\hdwwiz.exe"
"Add a Printer wizard" = "rundll32.exe shell32.dll,SHHelpShortcuts_RunDLL AddPrinter"
"Administrative Tools" = "control.exe /name Microsoft.AdministrativeTools"
"AutoPlay" = "control.exe /name Microsoft.AutoPlay"
"Backup and Restore" = "control.exe /name Microsoft.BackupAndRestoreCenter"
"BitLocker Drive Encryption" = "control.exe /name Microsoft.BitLockerDriveEncryption"
"Color and Appearance" = "explorer shell:::{ED834ED6-4B5A-4bfe-8F11-A626DCB6A921} -Microsoft.Personalization\pageColorization"
"Color Management" = "control.exe /name Microsoft.ColorManagement"
@mattmcnabb
mattmcnabb / TermColors.ps1
Created Oct 30, 2018
PS Terminal Theme Colors
View TermColors.ps1
# desktop
"terminal.background": "#012456",
"terminal.foreground": "#F5F5F5"
# core
"terminal.background": "#141A25",
"terminal.foreground": "#F5F5F5"
@mattmcnabb
mattmcnabb / TruncateString.ps1
Last active Oct 23, 2018
Add a Truncate method to system.string
View TruncateString.ps1
$Script = {
param([int]$MaxLength)
switch ($this)
{
{[string]::IsNullOrEmpty($_)} {$this; break}
{$_.Length -le $MaxLength} {$this; break}
default {$_.SubString(0, $MaxLength)}
}
}
@mattmcnabb
mattmcnabb / Json.Demo.ps1
Created Sep 20, 2018
Wrap Json with C# class in PowerShell
View Json.Demo.ps1
add-type -Path .\newtonsoft.json.dll
$CSharp = @"
namespace json
{
public class demo
{
public string prop1 {get; set;}
public int prop2 {get; set;}
public string[] prop3 {get; set;}
@mattmcnabb
mattmcnabb / Get-cPath.ps1
Last active Feb 3, 2018
Get canonical path in PowerShell
View Get-cPath.ps1
function Get-cPath
{
param
(
$Path
)
if (!([System.Io.File]::Exists($Path) -or [System.Io.Directory]::Exists($Path)))
{
return $Path
@mattmcnabb
mattmcnabb / ArbitrarySwitch.ps1
Created Jan 13, 2018
Switch evaluates arbitrary scriptblocks
View ArbitrarySwitch.ps1
$TestValue = "nuffin"
switch ($TestValue)
{
"sumpin"
{
"this does not run"
}
"nuffin"
View ConvertFrom-RGB.ps1
function ConvertFrom-RGB
{
param
(
[Parameter(Mandatory)]
[ValidateCount(3,3)]
[Byte[]]
$RGB
)