Skip to content

Instantly share code, notes, and snippets.

View markwragg's full-sized avatar

Mark Wragg markwragg

View GitHub Profile
@markwragg
markwragg / find-cmdlets.ps1
Last active October 18, 2018 11:02
Find all cmdlets being used in a specified script by using Abstract Syntax Trees
$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
@markwragg
markwragg / Open-GitUrl.ps1
Last active February 7, 2023 13:26
A PowerShell function to open the remote URL in the default browser for the git repo folder you are currently in at the command-line.
function Open-GitUrl {
$RemoteURL = git config remote.origin.url
if ($RemoteURL) {
if ($RemoteURL -match '\.git$') { $RemoteURL = $RemoteURL -replace '\.git$','/' }
if ($IsMacOS) {
open $RemoteURL
@markwragg
markwragg / Check-Modules.ps1
Last active January 29, 2019 22:08
Check for out of date PowerShell modules
$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}
@markwragg
markwragg / AddOne.tests.ps1
Created August 17, 2018 10:27
Add one tests
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)
}
}
@markwragg
markwragg / mactweaks.md
Last active April 25, 2023 18:10
Mac tweaks

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:

@markwragg
markwragg / Invoke-Puppet.ps1
Last active June 5, 2018 09:09
A PowerShell function to invoke Puppet after it has completed its current run
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
}
@markwragg
markwragg / scrape-rt.ps1
Last active August 23, 2019 22:24
Top 100 scrape from Rotten Tomatoes with rating
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
@markwragg
markwragg / Sort-AzureSubnets.ps1
Last active April 5, 2018 09:11
Get Azure subnets for a VNET and sort them correctly by address by recasting them to [version]
$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
@markwragg
markwragg / Test-URL.ps1
Created February 7, 2018 16:29
Testing URLs with added personality
ForEach ($URL in $URLs) {
Write-Host $URL -NoNewline ": "
Try {
Invoke-WebRequest $URL -UseBasicParsing | Out-Null
Write-Host ('Yup','Looks Good','OK' | Get-Random) -ForegroundColor Green
}Catch{
Write-Host $_ -ForegroundColor Red
}
}
@markwragg
markwragg / Get-CoinFlip.ps1
Created November 28, 2017 08:49
PowerShell Function for deciding between two choices. E.g Get-CoinFlip -Heads Coffee -Tails Tea
Function Get-CoinFlip {
Param(
$Heads = 'Heads',
$Tails = 'Tails'
)
Get-Random $Heads,$Tails
}