Instantly share code, notes, and snippets.

View get-awsami.ps1
Function Get-AWSAMI {
[cmdletbinding()]
Param(
$Name = 'Windows_Server-2016-English'
)
$Images = aws ec2 describe-images --filters Name="name",Values="$Name" --region us-west-2 --owners amazon
($Images | ConvertFrom-Json).Images
}
View settings.json
{
"files.defaultLanguage": "powershell",
"powershell.powerShellExePath": "/usr/local/bin/pwsh",
"terminal.integrated.shell.osx": "/usr/local/bin/pwsh",
"terminal.integrated.shellArgs.osx": [],
}
View find-cmdlets.ps1
$AST = [System.Management.Automation.Language.Parser]::ParseFile(
(Resolve-Path './Some-File.ps1'),
[ref]$null,
[ref]$Null
)
$CommandElements = ($AST.FindAll( {$args[0] -is [System.Management.Automation.Language.CommandAst]}, $true)).CommandElements
($CommandElements | Where-Object {$_.StringConstantType -eq 'BareWord'}).Value | Sort-Object -Unique
View github.ps1
Function github {
$RemoteURL = git config remote.origin.url
if ($RemoteURL) {
if ($RemoteURL -match '\.git$') { $RemoteURL = $RemoteURL.trimend('.git') }
if ($IsMacOS) {
open $RemoteURL
}
else {
start $RemoteURL
}
View Check-Modules.ps1
$Modules = Get-Module -ListAvailable |
Where-Object { $_.RepositorySourceLocation -like '*powershellgallery*'} |
Group-Object Name |
ForEach-Object { $_.Group | Sort-Object Version -Descending | Select-Object -First 1 }
$ModuleCheck = ForEach ($Module in $Modules) {
Find-Module -Name $Module | Select-Object Name,Version,@{N='InstalledVersion';E={$Module.Version}}
}
$OutOfDateModules = $ModuleCheck | Where-Object {[version]$_.Version -gt [version]$_.InstalledVersion}
View AddOne.tests.ps1
function AddOne ([int16]$Num) {
$Num = $Num + 1
Return $Num
}
Describe 'Add One Tests' {
32000..32766 | ForEach-Object {
It "Should Add 1 to $_" {
AddOne $_ | Should -Be ($_ + 1)
}
}
View mactweaks.md

Changes to my Mac

Turn off "press and hold" for foreign letters, allowing you to then press and hold for repeat letters. Open Terminal and run:

defaults write NSGlobalDomain ApplePressAndHoldEnabled -bool false

Then you need to close/reopen any app before it takes effect.

Make the dock appear faster when using auto hide:

View Invoke-Puppet.ps1
Function Invoke-Puppet {
Param(
$Environment
)
if ($Environment) { $Environment = "--environment='$Environment'" }
while (Test-Path C:/ProgramData/PuppetLabs/puppet/cache/state/agent_catalog_run.lock) {
Write-Warning 'Puppet is already running. Waiting for it to finish..'
Start-Sleep 5
}
View scrape-rt.ps1
Function Get-Top100Movies {
[cmdletbinding()]
Param(
[Int[]]$Year = ((Get-Date).Year -1)
)
foreach ($Yr in $Year) {
$Result = (Invoke-WebRequest "https://www.rottentomatoes.com/top/bestofrt/?year=$Yr") -split '</tr>'
$Result | Select -Skip 1 | ForEach-Object {
$_ -match '(?smi)<td>\s*?<a href="/m/.*?>\s*(.*?)</a>.*?</td>' | Out-Null
View Sort-AzureSubnets.ps1
$VNET = Get-AzureRmVirtualNetwork -name VNET_NAME -ResourceGroupName RG_NAME
(Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork $VNET).AddressPrefix | ForEach-Object {
$Net = $_ -split '/'
[pscustomobject]@{
Address = [version]$Net[0]
Mask = $Net[1]
}
} | Sort Address