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 / 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]+')) {
@JohnLBevan
JohnLBevan / PowershellParallelForeach.ps1
Created September 30, 2023 06:12
Powershell parallel foreach reference demo code
# some function we want available in our parallel runspace
function Get-DummyItem {
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[string]$Name
)
$myIp = Invoke-WebRequest -Method Get -Uri 'https://api.my-ip.io/ip'
"[$Name] was processed by [$myIp]"
@JohnLBevan
JohnLBevan / Repair-AzureDevOpsConnection.ps1
Created June 22, 2023 08:08
Renew ARM service connection secrets for Azure DevOps / convert them to manual. Note: once they're amended to be manual, you can manage secrets via the UI going forwards. This script is based on the initial script and info from https://rlevchenko.com/2022/03/04/azure-devops-update-service-connection-expired-secret/; thanks rlevchenko for this.
Function Repair-AzureDevOpsConnection {
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[string]$AzureDevOpsPAT # bad practice to pass secrests as strings, but this is a quick and dirty
,
[Parameter(Mandatory)]
[string]$Org
,
[Parameter(Mandatory)]
@JohnLBevan
JohnLBevan / Get-AzVmPageFileInfo.ps1
Created March 24, 2023 10:54
After doing a lift & shift of VMs into Azue, their page file may still be on the C drive (or whatever its original location), rather than on the temporary storage available to the new AZ VM (giving better perfromance). This script scans all VMs to help flag such issues.
Login-AzAccount # opens browser for interactive login
$subscriptions = Get-AzSubscription |
Where-Object {$_.State -eq 'Enabled'} |
Select-Object -ExpandProperty 'Id'
$script = @'
$driveInfo = Get-PSDrive -PSProvider FileSystem | Select-Object Root, Description
$pageFileDrive = Get-WmiObject Win32_Pagefile | Select-Object -ExpandProperty Drive
([PSCustomObject]@{
DriveInfo = ($driveInfo | %{"$($_.Root) = $($_.Description)"}) -join '; '
@JohnLBevan
JohnLBevan / Compare-ArrayItems.ps1
Last active March 15, 2023 09:51
Gives you a way to compare 2 objects side by side to see what properties differ. The example shows how we could compare 2 user accounts from AD (arbitrarily picks the first 2 people called John from the directory for comparison)
function Compare-ArrayItems {
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[PSObject[]]$ReferenceArray
,
[Parameter(Mandatory)]
[PSObject[]]$DifferenceArray
)
# note: this treats the arrays as sets; i.e. doesn't care what position items are in within an array, or if the same item occurs multiple times
@JohnLBevan
JohnLBevan / Get-DomainRegisrar.ps1
Last active December 13, 2024 13:30
Script to run whois queries to fetch the registrar for a given domain. Note: TCP code stolen from my SMTPS script (https://gist.github.com/JohnLBevan/c7974c2839e1486345d63ab6bd76523c) - hence some redundant code (not yet cleaned up)
function Receive-TcpServerResponse {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]
[System.IO.StreamReader]$StreamReader
)
# useful notes on SMTP https://www.rfc-editor.org/rfc/rfc5321.html / http://www.tcpipguide.com/free/t_SMTPRepliesandReplyCodes-3.htm / FTP on https://www.w3.org/Protocols/rfc959/4_FileTransfer.html
$return = [PSCustomObject]@{PSTypeName='TcpResponse';Code=$null;Text=''} # don't need code, but no harm in leaving it (it was here from: )
$hasMoreData = $true
#Write-Verbose ': Awaiting Server Response'
@JohnLBevan
JohnLBevan / Archive-Files.ps1
Last active February 2, 2023 17:30
A powershell script to help in many scenarios where you need to get older files elsewhere; e.g. archive scenarios (though doesn't include compression) / cases where you have a fileshare with too many historic files to be workable
function Get-FilesInDirectory {
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[string]$Path
,
[Parameter(Mandatory)]
[string]$SearchPattern
,
@JohnLBevan
JohnLBevan / DBTest.java
Last active January 12, 2023 17:41
We have a java based system which uses the SqlServerXADataTable class from sqljdbc4.jar to connect to SQL Server. Whilst migrating this app to a new server I got the error `Connection test for 'MyDatabaseName' FAILED: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "SQL Serv…
import com.microsoft.sqlserver.jdbc.*;
import java.sql.*;
public class DBTest {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Please provide a connection string as an argument when calling this method; exactly 1 argument expected; received: " + args.length);
return;
@JohnLBevan
JohnLBevan / Get-MostRecentIISLogDate.ps1
Created December 8, 2022 07:34
Code for finding whether any file transfers have occured on any IIS FTP sites in the last year (has parameters to allow this to be tweaked more, though I've not passed those all the way down as they were beyond my requirement).
Function Import-IISLog {
[CmdletBinding()]
Param (
[Parameter(Mandatory, ValueFromPipeline)]
[System.IO.FileInfo]$Path
,
[Parameter()]
[Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding]$Encoding = [Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding]::Ascii
)
Begin {