Skip to content

Instantly share code, notes, and snippets.

View SP3269's full-sized avatar

Svyatoslav Pidgorny SP3269

View GitHub Profile
@SP3269
SP3269 / dodeca.m
Created September 16, 2017 23:31
Visualising rhombic dodecahedron in Wolfram Mathematica
(* ::Package:: *)
(* ::Input:: *)
(*a={0,0,0}*)
(*b={1,0,0}*)
(*c={1,1,0}*)
(*d={0,1,0}*)
(*e={0,0,1}*)
(*f={1,0,1}*)
(*g={1,1,1}*)
@SP3269
SP3269 / Read-XKCD.ps1
Created December 10, 2017 07:43
This Powershell code fetches XKCD comics and submits to Microsoft Cognitive Services Vision API for handwritten text recognition, creating XKCD text dataset
function Get-XKCDComic { param ($n,$file); $h="https:"; iwr "$h$(((iwr "$h//xkcd.com/$n").images)[1].src)" -out $file } # Saves XKCD #n to a file
function Get-XKCDCurrentIssueNo { (iwr "https://xkcd.com").RawContent -match "Permanent link to this comic: https://xkcd.com/(?<N>\d+)" | Out-Null; return [int]$matches["N"] }
# Running Project Oxford APIs
# Inspired by https://jamessdixon.wordpress.com/2016/12/25/age-and-sex-analysis-of-microsoft-usa-mvps/
# In Powershell
# Get Subscription key at https://www.microsoft.com/cognitive-services/en-US/subscriptions
#Modifying for XKCD recognition
@SP3269
SP3269 / bf.jl
Last active September 30, 2018 04:02
Brainfuck implementation in Julia
# Runs in Julia 0.6.2. For Julia 1.0 code, refer to my Julia-Playground repo
# Also, implementation of the bracket matching code isawful. What was I thinking about?
bfcode = ", [ > + < - ] > ."
cells = [0 for i=1:30000]
bfcode = replace(bfcode, r"[^\+\-\<\>\.\,\[\]]", "") # Eliminating all extranious characters in the code
# Initial processing: identifying pairs of [ and ], and populating two arrays with indexes (positions of the brackets in the bf code)
@SP3269
SP3269 / VerifyJWTSignature.ps1
Created January 5, 2018 05:53
JWT verification in Powershell - prototype
# JWT signature verification
# $jwt should contain the JWT as a string
$parts = $jwt.Split('.')
$SHA256 = New-Object Security.Cryptography.SHA256Managed
$computed = $SHA256.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($parts[0]+"."+$parts[1]))
# Method A - just using X509Certificate2. TODO: initialise from file, no private key
# Reference: https://blogs.msdn.microsoft.com/alejacma/2008/06/25/how-to-sign-and-verify-the-signature-with-net-and-a-certificate-c/
@SP3269
SP3269 / ADGroupChanges.ps1
Last active April 11, 2018 04:17
Detecting Active Directory group changes based on AD metadata. In addition, the account making the change is found from Windows event on originating DC (assumptions apply)
Function Get-GroupChanges {
Param(
$group = "Domain Admins",
$server = (Get-ADDomainController).HostName,
$hours = 4
)
$adgroupobject = Get-ADGroup $group
$memberchanges = Get-ADReplicationAttributeMetadata -Object $adgroupobject.DistinguishedName -Server $server -ShowAllLinkedValues | ? AttributeName -eq "Member" | ? LastOriginatingChangeTime -gt (Get-Date).AddHours(-1 * $Hours)
@SP3269
SP3269 / GoogleVisionAPI.ps1
Last active March 25, 2018 23:47
This is an example of using JSON template for Google API calls in Powershell. The JSON template is direct copy/paste from Google documentation. The script reads image file, modifies the JSON and submits the request to the API.
$jsontemplate = @'
{
"requests":[
{
"image":{
"content":"/9j/7QBEUGhvdG9...image contents...eYxxxzj/Coa6Bax//Z"
},
"features":[
{
"type":"LABEL_DETECTION",
@SP3269
SP3269 / sparkbar.ps1
Last active May 19, 2021 22:20
Sparkbar in Powershell
function New-Sparkbar {
param (
$numbers
)
$bars = "▁","▂","▃","▄","▅","▆","▇","█"
$max = ($numbers | Measure-Object -Maximum).Maximum
$min = ($numbers | Measure-Object -Minimum).Minimum
$span = $max - $min
$out = ""
foreach ($n in $numbers) {
@SP3269
SP3269 / Prepare-TerraformImport.ps1
Created September 12, 2018 01:28
This is PowerShell code that is using Google Cloud Platform SDK's gcloud to read the organization's folders, iterates through the folder hierarchy, and generates Terraform HCL code and shell script for import to Terraform state.
# This code relies on gcloud present in the path and authenticated with permissions to read folders
# The output is Terraform HCL code in the .tf file and shell code to import the discovered resources in the .sh file
# Similar approach can be used to survey other type of resources
gcloud organizations list --format='json' | ConvertFrom-JSON -OutVariable org
if (!$?) { throw "Error invoking gcloud" } # Rudimentary error handling - throw terminating error if gcloud errors out in any way
$seed = $org.name.split("/")[1]
$folders = @() # Starting with empty array, to be populated with org-level folders
@SP3269
SP3269 / Get-GAuthorizationKey.ps1
Last active August 3, 2022 20:28
This is a PowerShell implementation of two-legged OAuth 2.0 scenario for server-to-server interactions with Google Identity Platform
#! /usr/bin/pwsh -nop
# This is PowerShell implementation of 2LO per https://developers.google.com/identity/protocols/OAuth2ServiceAccount
# Inputs: GCP service account with credentials, user to impersonate, and permissions to request
# Output: the access token for subsequent requests
# Using New-JWT function from the JWT module - "Install-Module JWT" if you don't have it
Import-Module JWT
@SP3269
SP3269 / AccessAzureADGraph.ps1
Last active September 18, 2021 01:01
This is simple Azure AD graph call given client ID and secret generated by the AAD administrator. Lists the users. Some error handling.
# Setting default parameters for irm for better error tolerance in case of transient connectivity issues. Can specify Proxy and ProxyCredential here:
$PSDefaultParameterValues = @{
"Invoke-RestMethod:MaximumRetryCount" = 3
"Invoke-RestMethod:RetryIntervalSec" = 1
}
# This is simple token request per http://codematters.tech/getting-access-token-for-microsoft-graph-using-oauth-rest-api/
# Credentials JSON per ADAL example at https://github.com/AzureAD/azure-activedirectory-library-for-python/blob/dev/sample/client_credentials_sample.py