Skip to content

Instantly share code, notes, and snippets.

@mattmcnabb
mattmcnabb / TransformTest1.psm1
Last active November 14, 2019 20:56
Can't quite get these transforms working...
# 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 July 8, 2019 13:42
Get IP Address WhoIs Info
function Get-JsonWhois
{
[CmdletBinding(DefaultParameterSetName = "ip")]
param
(
[Parameter(Mandatory, ParameterSetName = "ip")]
[string]
$IPAddress,
[Parameter(Mandatory, ParameterSetName = "domain")]
@mattmcnabb
mattmcnabb / NamedParameters.ps1
Last active July 5, 2019 15:52
New up an object with default constructor
# 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 December 14, 2018 18:50
Launch windows control panel applets from PowerShell
$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 October 30, 2018 19:25
PS Terminal Theme Colors
# desktop
"terminal.background": "#012456",
"terminal.foreground": "#F5F5F5"
# core
"terminal.background": "#141A25",
"terminal.foreground": "#F5F5F5"
@mattmcnabb
mattmcnabb / TruncateString.ps1
Last active October 23, 2018 13:51
Add a Truncate method to system.string
$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 September 20, 2018 17:57
Wrap Json with C# class in PowerShell
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 February 3, 2018 13:08
Get canonical path in PowerShell
function Get-cPath
{
param
(
$Path
)
if (!([System.Io.File]::Exists($Path) -or [System.Io.Directory]::Exists($Path)))
{
return $Path
@mattmcnabb
mattmcnabb / ArbitrarySwitch.ps1
Created January 13, 2018 19:17
Switch evaluates arbitrary scriptblocks
$TestValue = "nuffin"
switch ($TestValue)
{
"sumpin"
{
"this does not run"
}
"nuffin"
function ConvertFrom-RGB
{
param
(
[Parameter(Mandatory)]
[ValidateCount(3,3)]
[Byte[]]
$RGB
)