Skip to content

Instantly share code, notes, and snippets.

View markwragg's full-sized avatar

Mark Wragg markwragg

View GitHub Profile
@markwragg
markwragg / redirect-out-file.tests.ps1
Last active October 14, 2024 15:13
Redirect Out-File to write to TestDrive: during Pester execution
BeforeAll {
$OutFile = Get-Command -Name 'Out-File'
Mock Out-File -ParameterFilter { $FilePath -match 'file.txt' } -MockWith {
$InputObject = $PesterBoundParameters['InputObject']
& $OutFile -InputObject $InputObject -FilePath (Join-Path $TestDrive 'file.txt')
}
}
It 'Should update the file with the expected values' {
@markwragg
markwragg / Format-Markdown.ps1
Created September 7, 2024 08:54
A PowerShell cmdlet to make markdown files more readable in the console.
function Format-MarkDown {
[CmdletBinding()]
param (
# Parameter help description
[Parameter(ValueFromPipeline)]
[string[]]
$InputObject
)
begin {
$Color = 'gray'
@markwragg
markwragg / Find-ModuleByAttribute.ps1
Last active August 10, 2024 22:47
Find PowerShell modules for a specified author and return download count, description, URI and published date
function Find-ModuleByAttribute {
[cmdletbinding()]
param(
$Attribute = 'companyname',
$Value = 'markwragg'
)
Write-Verbose "Finding all modules where $Attribute = $Value.."
$Modules = Find-Module | Where-Object { $_.$Attribute -eq $Value }
@markwragg
markwragg / Format-HashTable.ps1
Last active August 2, 2023 11:16
Function to convert an object into HashTable PowerShell code, with name values optionally anonymized. This was written to quickly create the contents of Pester Mocks, by taking real output and converting it to Hashtables or PsCustomObject output for declaring within the Mock in Pester. The function can anonymize the name strings in the objects a…
function Format-HashTable {
[CmdletBinding()]
param(
[parameter(, ValueFromPipeline)]
$InputObject,
[switch]
$AnonymizeNames,
[switch]
@markwragg
markwragg / Get-AppInsightsUsage.ps1
Created April 25, 2020 12:42
A PowerShell function for querying one or more AppInsights accounts (or all AppInsights accounts under a subscription once logged in) to get the daily cap setting and previous 24 hours of usage
Function Get-AppInsightsUsage {
Param(
# Optional: One or more AppInsights resource names
[string[]]
$Name
)
$AppInsights = Get-AzApplicationInsights
If ($Name) { $AppInsights = $AppInsights | Where-Object { $_.Name -in $Name } }
@markwragg
markwragg / profile.ps1
Created March 5, 2020 09:30
PowerShell Profile snippet to save/return to last location on exit
$LastLocationFile = Join-Path $env:USERPROFILE 'lastlocation.txt'
If (Test-Path $LastLocationFile) {
Import-Clixml $LastLocationFile | Set-Location
}
Else {
Set-Location $env:USERPROFILE
}
Register-EngineEvent PowerShell.Exiting -Action {

How to get @DevBlackOps Terminal-Icons module working in PowerShell on Windows

Note: since version 0.1.1 of the module this now works in Windows PowerShell or PowerShell Core.

  1. Download and install this version of Literation Mono Nerd Font which has been specifically fixed to be recognised as monospace on Windows:

https://github.com/haasosaurus/nerd-fonts/blob/regen-mono-font-fix/patched-fonts/LiberationMono/complete/Literation%20Mono%20Nerd%20Font%20Complete%20Mono%20Windows%20Compatible.ttf

(see this issue for more info: ryanoasis/nerd-fonts#269)

@markwragg
markwragg / Write-Human.ps1
Last active May 18, 2024 21:16
A PowerShell function to output text to screen as if typed by a human. I don't know why I wrote this.
Function Write-Human {
<#
.SYNOPSIS
Use to output text as if typed by a human.
.SYNOPSIS
This script takes one or more strings and prints them to the screen with a slightly randomised delay between each character to emulate a human typing.
.EXAMPLE
Get-Content .\mytestfile.txt | Write-Human
#>
[cmdletbinding()]
@markwragg
markwragg / Get-Subnet.ps1
Last active April 5, 2024 14:32
PowerShell cmdlet to return the IP details and range of a network and subnet mask
function Get-Subnet {
param (
[parameter(ValueFromPipeline)]
[String]
$IP,
[ValidateRange(0, 32)]
[int]
$MaskBits,
@markwragg
markwragg / get-awsami.ps1
Last active October 29, 2018 08:30
PowerShell / AWS CLI command to retrieve list of Amazon AMIs matching a partial name string
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
}