Skip to content

Instantly share code, notes, and snippets.

View JohnLBevan's full-sized avatar
🏠
Working from home

John Bevan JohnLBevan

🏠
Working from home
View GitHub Profile
@JohnLBevan
JohnLBevan / Get-SiteCert.ps1
Created February 4, 2025 22:31
Get Site's Certificates
# Given a URI fetches the certificates hosted on that site, then parses the PEM to extract user friendly info (currently just as strings; I've not added logic to convert the output to PSCustomObjects, though may add that soon if I find the need).
$uri = 'example.org'
$output = (echo "" | openssl s_client -connect "$($uri):443" -servername $uri -showcerts 2>$null) -join "`n"
$output | select-string '(?smi)-----BEGIN CERTIFICATE-----(.*?)-----END CERTIFICATE-----' -AllMatches | %{$_.Matches} | %{$_.Value | openssl x509 -issuer -fingerprint -sha256 -enddate -subject -noout;'---'}
<# # Example Output #
issuer=C = US, O = DigiCert Inc, CN = DigiCert Global G3 TLS ECC SHA384 2020 CA1
SHA256 Fingerprint=85:A1:DF:2F:31:49:6B:1F:D7:04:A8:43:6D:30:9C:DC:5C:1B:4B:B3:95:6F:B3:1A:73:2A:C1:82:4D:AF:CA:C2
notAfter=Jan 15 23:59:59 2026 GMT
subject=C = US, ST = California, L = Los Angeles, O = Internet Corporation for Assigned Names and Numbers, CN = *.example.org
@JohnLBevan
JohnLBevan / Convert-EntraSidToObjectId.ps1
Created October 28, 2024 11:20
Convert an Azure Active Directory / MS Entra / AAD SID to Object Id. Entra doesn't store the SID natively (the OnPremiseSecurityIdentifier is the AD SID in hybrid environments, but has no comptable link to the Entra SID), so if given a SID we need to convert it to an ObjectId before we can use that to find the correspnding Entra object (could be…
# Thanks to https://github.com/okieselbach/Intune/blob/master/Convert-AzureAdSidToObjectId.ps1
Function Convert-EntraSidToObjectId {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline)]
[String]$Sid
)
Process {
$text = $sid.Replace('S-1-12-1-', '')
$array = [UInt32[]]$text.Split('-')
@JohnLBevan
JohnLBevan / Export-AzKeyVaultCertificateToPfx.ps1
Created July 19, 2024 18:02
Fetches a certificate from Azure Key Vault and exports its full chain (client>intermediate>root) and private key to a PFX with a private key export password.
Function Export-AzKeyVaultCertificateToPfx {
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[string]$Subscription
,
[Parameter(Mandatory)]
[string]$VaultName
,
[Parameter(Mandatory)]
@JohnLBevan
JohnLBevan / ALineChart.ps1
Last active July 18, 2024 10:36
Plot a line chart in PowerShell
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Windows.Forms.DataVisualization
Function Out-LineChart {
[CmdletBinding()]
Param (
[Parameter(Mandatory, ValueFromPipeline)]
[Hashtable]$Data
,
[Parameter()]
@JohnLBevan
JohnLBevan / ConvertCidrIpToInt.xlsx.md
Last active May 31, 2024 13:07
Convert an IP or CIDR to an integer in Excel

Row 1 has headings:

  • CIDR
  • IP
  • First IP Int
  • Last IP Int
  • Test IP Int
  • IP In Range

Column A contains CIDRs (e.g. 3.2.1.0/30)

@JohnLBevan
JohnLBevan / Convert-StringToValidateSetParameterCase.ps1
Last active April 22, 2024 08:05
Ensures values match the case given in validateset by correcting to match instead of throwing exceptions. Trick thanks to MKlement: https://stackoverflow.com/a/42699260/361842
Function Convert-StringToValidateSetParameterCase {
Param (
[Parameter(Mandatory)]
[System.Management.Automation.InvocationInfo]$InvocationInfo
,
[Parameter(Mandatory)]
[string]$ParameterName
,
[Parameter(Mandatory)]
[AllowEmptyString()]
@JohnLBevan
JohnLBevan / Get-AllCombos.ps1
Created April 6, 2024 07:41
Script to produce all combos (including empty) of a set of items. A bit clunky since PS tries to be helpful, but that doesn't play well when returning lists of lists.
function Get-AllCombos {
[CmdletBinding()]
[OutputType("System.Collections.Generic.List[System.Object]")]
Param (
[Parameter(Mandatory)]
[AllowEmptyCollection()]
#[System.Collections.Generic.List[System.Object]]$arr
[System.Collections.Generic.List[System.Object]]$arr
)
$ErrorActionPreference = [System.Management.Automation.ActionPreference]::Stop
@JohnLBevan
JohnLBevan / ListIPGroupsContainingCIDR.kql
Created April 3, 2024 13:09
Azure: Kusto (KQL): Network Related Queries
// Specify a value for `testCidr` (must be a valid CIDR; so if just looking for a specific IPv4 IP, append /32 on the end).
// Run this and you'll see all IP Groups which contain CIDRs or IPs which overlap in any way with the given value.
resourcecontainers | where type == "microsoft.resources/subscriptions" | limit 1 // this is a hack to give us a single row
| project testCidr = "123.123.123.123/32" // update this value to the CIDR you're interested in
| extend testCidrSplit = array_concat(split(split(testCidr, '/')[0],'.'), split(split(testCidr, '/')[1],'x'))
| extend testCidrFirstIp = toint(testCidrSplit[0]) * 16777216 + toint(testCidrSplit[1]) * 65536 + toint(testCidrSplit[2]) * 256 + toint(testCidrSplit[3])
| extend testCidrLastIp = testCidrFirstIp + pow(2,32-testCidrSplit[4])-1
| extend joinhack = 1
| join kind = inner
@JohnLBevan
JohnLBevan / ConvertAwsR53RecordSetChange.ps1
Created February 7, 2024 12:56
AWS Route53 Zone Migration
<#
.SYNOPSIS
Used to help migrate R53 zones by converting the JSON obtained by
extracting all record sets from a zone to the JSON required to upload
these recordsets to another zone.
.DESCRIPTION
Covers those tasks described in step 4 of [Migrating an AWS Zone](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/hosted-zones-migrating.html#hosted-zones-migrating-create-file)
i.e. to convert the output of `aws route53 list-resource-record-sets --hosted-zone-id <hosted-zone-id>`
... to the input of `aws route53 change-resource-record-sets --hosted-zone-id id-of-new-hosted-zone --change-batch file://path-to-file-that-contains-records`
@JohnLBevan
JohnLBevan / Copy-FtpItem.ps1
Created October 27, 2023 16:22
FTP Upload using PowerShell
Function Copy-FtpItem {
[CmdletBinding()]
Param (
[Parameter(Mandatory, ValueFromPipeline)]
[string[]]$Path
,
[Parameter(Mandatory)]
[string]$FtpHost