Skip to content

Instantly share code, notes, and snippets.

#This only works in PowerShell 6 and 7 as lower versions don't support -AsHashtable
$Dracula = convertfrom-json -AsHashtable @"
{
"background" : "#282A36",
"black" : "#21222C",
"blue" : "#BD93F9",
"brightBlack" : "#6272A4",
"brightBlue" : "#D6ACFF",
"brightCyan" : "#A4FFFF",
"brightGreen" : "#69FF94",
@gpduck
gpduck / readme.md
Last active December 15, 2023 17:22
Ansible on Windows over SSH

Windows

  • Install OpenSSH
  • For domain user on 7.7+ (all lower case): AllowUsers domain\user
  • For local user (all lower case): AllowUsers user
  • run-as user to create profile (or log-in)
  • create .ssh and authorized_keys in the user's profile
  • run the fix user key script (program files\openssh)

Ansible

@gpduck
gpduck / TrimTCX.ps1
Created May 23, 2017 03:29
Trim a TCX file from Garmin
$InPath = "c:\activity.tcx"
$OutPath = "c:\activity_trimmed.tcx"
$x = [xml](get-content $InPath)
$x.TrainingCenterDatabase.activities.Activity | %{
$Activity = $_
$Activity.lap | %{
$Lap = $_
$lap.track | %{
$Track = $_
@gpduck
gpduck / default.ps1
Created April 12, 2017 23:06
PSake Build Script
Properties {
if(!$OutDir) {
$OutDir = "out"
}
if(!$ProjectDir) {
$ProjectDir = $PSake.build_script_dir
}
if(!$TargetDir) {
$TargetDir = Join-Path -Path $ProjectDir -ChildPath $OutDir
}
@gpduck
gpduck / gist:fd4c2a14b56c18dcbfc0fa13662742a7
Last active April 9, 2017 20:29
I made a wrapper for your .Net method calls so you can test...
[System.AppDomain]::CurrentDomain.GetAssemblies().ExportedTypes | %{
$Type = $_
$Type.GetMethods() | Select -Unique Name | %{
$Method = $_
$Name = "Invoke-{0}.{1}" -f $Type.Fullname, $Method.Name
@"
@gpduck
gpduck / Errors.ps1
Created March 7, 2017 18:48
PowerShell Errors
$Error.Clear()
Try {
Get-Process fsdks -ErrorAction Stop
} Catch {
Write-Error "Couldn't find the process"
}
#Prints out the error "Couldn't find the process"
#This contains 2 objects, the "Couldn't find the process" error, and the original error from Get-Process
$RemoteComputer = "remotecomputer"
Invoke-Command -Computer $RemoteComputer -ScriptBlock { import-module bitstransfer; get-module bitstransfer; }
$S = New-PSSession -Computer $RemoteComputer
Invoke-Command -Session $S -ScriptBlock { Import-Module bitstransfer}
Invoke-Command -Session $S -ScriptBlock { get-Module bitstransfer}
Remove-PSSession $S

The PowerShell pipeline unwraps collections. Let's write a function to test to see if something is a collection

function Test-Collection {
  param(
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
    $InputObject
  )
  process {
     $InputObject -is [System.Collections.ICollection]

The whole point of using ValueFromPipeline (and ValueFromPipelineByPropertyName) is to replace this syntax:

"c:\windows","c:\Program Files" | ForEach-Object {
  Get-DirectoryFileSize -Directory $_
}

With this synax:

function New-Node {
[OutputType("Whatsupduck.Powershell.GraphViz.Node")]
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String]$Name,
[Parameter(Mandatory=$false)]