Skip to content

Instantly share code, notes, and snippets.

View SP3269's full-sized avatar

Svyatoslav Pidgorny SP3269

View GitHub Profile
@SP3269
SP3269 / env.tf
Last active July 18, 2024 03:43
Output Linux environment from Terraform
# Output Linux environment from Terraform
provider "local" {}
data "local_file" "env" {
filename = "/proc/1/environ"
}
output "env" {
value = data.local_file.env.content
# Using curl to measure TLS negotiation time, as a proxy of PKCS #11 performance
# https://blog.cloudflare.com/a-question-of-timing/
function Get-ConnectionTimes ([string] $Uri) {
$curl = curl -w "%{time_namelookup},%{time_connect},%{time_appconnect}\n" -s -o /dev/null $Uri
$namelookup,$connect,$appconnect = $curl -split ","
$res = [PSCustomObject]@{
namelookup = [float]$namelookup
connect = [float]$connect
appconnect = [float]$appconnect
@SP3269
SP3269 / flatten.md
Last active February 21, 2023 06:25
Flattening structures in PowerShell

Quickly coding a function that allows flattening a structure. The goal is to use in comparing complex structures, such as created by the ConvertFrom-JSON or ConvertFrom-Yaml.

Outputs a hash table.

Example:

$res = Flatten-Object @{SH = @("bin", "bash"); A = 1; B = "ZZ"; C = @{CC = "CC"}; Logic = $true}
$res.Keys | Sort-Object | % { Write-Output "$_ = $($res[$_])"}
@SP3269
SP3269 / promex.ps1
Created May 9, 2021 23:56
Dockerable Prometheus "anything" exporter in PowerShell
# pwsh "anything" Prometheus exporter sketch
# Using: PrometheusExporter (https://github.com/jobec/powershell-prom-client), including class defintitions
# Using: Polaris (https://github.com/PowerShell/Polaris)
# Motivation: being able to package PowerShell-based collector/exporter in a Docker container
# PrometheusExporter's 0.1.0 New-PrometheusExporter functionfails when run in a container - [console]::KeyAvailable error
# NOTE: Polaris can process one request at a time. Refactor/implement LB for real use
Using module PrometheusExporter
Import-Module Polaris
# Simple backoff in PowerShell
# Executes a scriptblock - the Code parameter
# In case of a blocking error, retries - the number of retries and seconds to wait before each is the Intervals parameter (array)
# Returns the result of the code invocation of throws the last error if run out of the backoffs
function Invoke-WithBackoff {
[CmdletBinding()]
param (
[Parameter()] [scriptblock]$Code,
[Parameter()] [float[]]$Intervals
// A Web server with two endpoints - /stable (returning 200) and /unstable (going into failing state with defined probability every tick and returning 500 while in it)
// Based on Googles slo-burn code https://github.com/google/prometheus-slo-burn-example/tree/master/server
package main
import (
"fmt"
"math/rand"
"net/http"
"os"
@SP3269
SP3269 / DiceRolls.ps1
Created March 6, 2020 23:37
Simple statistical look at the dice rolls in game of Catan using PowerShell 7
$game20200305 = @'
44
61
65
63
41
45
22
63
23
@SP3269
SP3269 / HIBP.ps1
Last active November 16, 2022 22:02
SImple Have I Been Pwned API client in PowerShell. Check whether your passwords have been compromised.
# Runs in PowerShell 5.1, PowerShell Core 6 on Windows and Linux, and PowerShell 7 preview
# Calculating SHA1 hash and returning it as a hexadecimal string
function Compute-SHA1Hash ([string] $string) {
$sha1 = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
$encoder = New-Object System.Text.UTF8Encoding
$bytes = $encoder.GetBytes($string)
$hash = ($sha1.ComputeHash($bytes) | % { $_.ToString("X2") }) -join ''
return $hash
@SP3269
SP3269 / ConvertJsonToPfx.ps1
Created May 3, 2019 23:19
PowerShell script to convert Google Cloud Platform service account JSON credentials to PFX credentials (for using with New-Jwt from my JWT module)
#! /usr/bin/pwsh -nop
$j = Get-Content "./Gsuite.json" | ConvertFrom-JSON
$priv = $j.private_key
$pub = (Invoke-RestMethod $j.client_x509_cert_url).($j.private_key_id)
$rnd = Get-Random 1000001
$priv | Out-File ".\priv$rnd.key"
$pub | Out-File ".\pub$rnd.cer"
openssl pkcs12 -export -in "pub$rnd.cer" -inkey "priv$rnd.key" -out "pfx$rnd.p12" -password pass:notasecret
@SP3269
SP3269 / RunGBigQuery.ps1
Created May 3, 2019 05:39
This PowerShell script is using Google BigQuery REST API to run a query and creates a result set using returned schema for field names and types
$project = "your-gcp-project"
$accesstoken = gcloud auth print-access-token # Lazy way. Can also get access token using service account and 2LO - ref. https://gist.github.com/SP3269/da43b00692de5b3f591b3068d76df577
# Using Google BigQuery REST API per https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query
# Example query using public dataset available to anyone in BQ
$query = 'SELECT * FROM `bigquery-public-data.san_francisco_trees.street_trees` WHERE plant_date >= "1969-01-01 00:00:00 UTC" AND plant_date < "1970-01-01 00:00:00 UTC"'
$body = @{
useLegacySql = "false"