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 / Test-PSCredentials.ps1
Created July 14, 2015 13:05
Test-PSCredentials: Check if an AD account's password is valid
#http://serverfault.com/questions/276098/check-if-user-password-input-is-valid-in-powershell-script
$cred = Get-Credential #Read credentials
$username = $cred.username
$password = $cred.GetNetworkCredential().password
# Get current domain using logged-on user's credentials
$CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
$domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,$UserName,$Password)
if ($domain.name -eq $null)
@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 / Covert-CidrToIpV4List.ps1
Last active March 8, 2024 15:34
Converts a CIDR notation IP Range to an array of those IPs
<#
.SYNOPSIS
Converts a CIDR notation IP Range to an array of those IPs, or an object containing the first and last values in the range.
All representations of IPs are returned as type: [System.Net.IPAddress]
.DESCRIPTION
Converts a CIDR notation IP Range to an array of those IPs, or an object containing the first and last values in the range.
All representations of IPs are returned as type: [System.Net.IPAddress]
.PARAMETER IpRangeCidr
@JohnLBevan
JohnLBevan / Validate local user's credentials
Last active February 8, 2024 18:16
If you want to check the credentials you have for a local user account are correct, you can use this script (from: https://blog.idera.com/database-tools/test-local-user-account-credentials )
$username = 'username'
$password = 'password'
$computer = $env:COMPUTERNAME
Add-Type -AssemblyName 'System.DirectoryServices.AccountManagement'
$obj = [System.DirectoryServices.AccountManagement.PrincipalContext]::new('machine',$computer)
$obj.ValidateCredentials("$computer\$username", $password)
@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
@JohnLBevan
JohnLBevan / demo.tf
Created October 27, 2023 14:16
Terraform Validate CIDR. Checks that the format is correct, and that the prefix matches the first IP in the range (thus it's a valid prefix)
variable "demoIpv4Cidr" {
type = string
default = "10.0.0.1/16" # try "10.0.0.0/16" for a valid value or "10.0.0.x/16" for an invalidly formatted cidr
validation {
condition = (
can(cidrhost(var.demoIpv4Cidr, 0)) &&
try(cidrhost(var.demoIpv4Cidr, 0), null) == split("/", var.demoIpv4Cidr)[0]
)
# the above could be simplified to:
@JohnLBevan
JohnLBevan / ConvertTo-RootDomain.ps1
Last active October 16, 2023 13:59
Get the root domain from a given subdomain; i.e. the first level under a public suffix / "top level domain" (TLD). So given the string "test.example.co.uk" this would return "example.co.uk", whilst "example.com" would return itself unchanged..
function ConvertTo-RootDomain {
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[string]$Domain
)
if ($null -eq $Global:CachedPublicDomainSuffixList) {
$publicDomainSuffixList = Invoke-WebRequest -Method GET -Uri 'https://publicsuffix.org/list/public_suffix_list.dat' -ContentType 'text/plain' -ErrorAction Stop
$tempSet = [System.Collections.Generic.HashSet[string]]::new()
foreach ($suffix in @($publicDomainSuffixList.Content -split '\s*[\r\n]+')) {