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 / Copy-FtpItem.ps1
Created October 27, 2023 16:22
FTP Upload using PowerShell
View Copy-FtpItem.ps1
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)
View demo.tf
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..
View ConvertTo-RootDomain.ps1
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
View PowershellParallelForeach.ps1
# 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.
View Repair-AzureDevOpsConnection.ps1
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.
View Get-AzVmPageFileInfo.ps1
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)
View Compare-ArrayItems.ps1
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 February 10, 2023 12:19
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)
View Get-DomainRegisrar.ps1
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
View Archive-Files.ps1
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…
View DBTest.java
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;