Skip to content

Instantly share code, notes, and snippets.

@gsscoder
gsscoder / Stage_Profile.ps1
Created July 21, 2020 07:57
PowerShell profile code to stage git changes with one command
function Set-GitChanges {
gaa
gcam Staging
ggp
}
Import-Module posh-git
Import-Module git-aliases -DisableNameChecking
Set-Alias -Name stage -Value Set-GitChanges
@gsscoder
gsscoder / Get-RandomString.ps1
Last active January 20, 2021 05:24
PowerShell function to generate alphanumeric string
# PS> 20 | Get-RandomString -Alphanumeric $false -NoQuotes $true
# PS> p({BFhlH*}KG#S{i{Ih5
function Get-RandomString {
[OutputType([string])]
param(
[Parameter(Mandatory, ValueFromPipeline)] [int] $Length,
[bool] $Alphanumeric = $true,
[bool] $NoQuotes = $false
)
@gsscoder
gsscoder / Require-AzCliVersion.ps1
Last active April 23, 2021 08:20
PowerShell function to check Azure CLI version
function Require-AzCliVersion {
[OutputType([void])]
param(
[Parameter(Mandatory, ValueFromPipeline)] [ValidatePattern("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,9}$")] [string] $Version,
[switch] $Fail
)
$actual = ('az version' | Invoke-Expression | ConvertFrom-Json).'azure-cli'
if ($actual -eq $Version) {
"Azure CLI present in pinned version $Version" | Write-Verbose
} else {
@gsscoder
gsscoder / bearer_token.ps1
Created December 30, 2020 10:55
PowerShell code to get the bearer token using an app registration
$tenantId = 'your_tenant_id'
$clientId = 'your_client_id'
$clientSecret = 'your_client_secret'
$tokenResponse = (Invoke-WebRequest -Uri "https://login.microsoftonline.com/$tenantId/oauth2/token" `
-Method Post -UseBasicParsing `
-Body "grant_type=client_credentials&client_id=$clientId&client_secret=$clientSecret").Content
"$(($tokenResponse | ConvertFrom-Json).access_token)`n" | Write-Host
@gsscoder
gsscoder / verbose.ps1
Created January 12, 2021 07:08
PowerShell code to test if -Verbose switch is enabled
[CmdletBinding()]
param ()
Set-StrictMode -Version Latest
$verbose = if ($PSBoundParameters.ContainsKey('Verbose')) {
$PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent } else { $false }
"Verbose enabled: $verbose" | Write-Host
@gsscoder
gsscoder / chaos.fsx
Last active February 27, 2021 13:39
Simple F# function that may inject chaos
// inspired by https://www.youtube.com/watch?v=RWyZkNzvC-c&t=553s
// $ dotnet fsi chaos.fsx
// latency test
// 0.919409
open System
open System.Threading
let chaos (name:string) (shouldChaos:unit -> bool) (chaos:unit -> unit) (service:unit -> 't) =
if shouldChaos () then
printfn "%s" name
@gsscoder
gsscoder / chaos2.fsx
Last active February 27, 2021 13:39
Simple F# function that returns a function that may inject chaos
// inspired by https://www.youtube.com/watch?v=RWyZkNzvC-c&t=553s
// $ dotnet fsi chaos2.fsx
// latency test
// 0.969491
open System
open System.Threading
let chaos (name:string) (shouldChaos:unit -> bool) (chaos:unit -> unit) =
fun (service:unit -> 't) ->
if shouldChaos () then
@gsscoder
gsscoder / chaos3.fsx
Last active February 27, 2021 14:06
Simple F# function that returns an asynchronous function that may inject chaos
// inspired by https://www.youtube.com/watch?v=RWyZkNzvC-c&t=553s
// $ dotnet fsi chaos3.fsx
// latency test
// 0.490776
open System
open System.Threading
let chaosAsync (name:string) (shouldChaosAsync:unit -> Async<bool>) (chaosAsync:unit -> Async<unit>) =
fun (serviceAsync:unit -> Async<'t>) ->
async {
@gsscoder
gsscoder / query_appinsights_rest.ps1
Last active March 12, 2021 13:24
PowerShell code to query Azure App Insights using REST API
$aiAppId = '00000000-0000-0000-0000-000000000000'
$aiApiKey = 'ehzgdrES4kBJNCuRUzcBfkuka5CGWkAr4hyhJ6y3'
$result = Invoke-RestMethod -Uri "https://api.applicationinsights.io/v1/apps/$aiAppId/query" `
-Method Post -UseBasicParsing `
-Headers @{
'x-api-key' = $aiApiKey
'content-type' = 'application/json'
} `
-Body '{"query": "traces | where timestamp <= ago(1m) | limit 10"}'
@gsscoder
gsscoder / checksum.bat
Last active March 13, 2021 06:59
Batch script to compare a file SHA256 checksum to a given one
@echo off
setlocal enabledelayedexpansion
::Batch script to compare a file SHA256 checksum to a given one.
::Usage: checksum [FILE] [VALUE]
set filepath=%1
set checksum=%2
set idx=0