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 / New-SelfSignedCertPfx.ps1
Created May 13, 2021 13:29
A wrapper for creating and exporting self-signed certs as PFX files. Useful if setting up sites which require HTTPS config ahead of getting a proper cert (e.g. to configure HTTPS for app gateway before configuring LetsEncrypt per https://intelequia.com/blog/post/1012/automating-azure-application-gateway-ssl-certificate-renewals-with-let-s-encryp…
Function New-SelfSignedCertPfx {
[CmdletBinding(DefaultParameterSetName = 'PasswordAsSecureString')]
Param (
[Parameter()]
[string[]]$SanList = @('localhost', '127.0.0.1')
,
[Parameter(ParameterSetName = 'PasswordAsSecureString')]
[SecureString]$ExportPassword = [System.Security.SecureString]::new()
,
# note: using the secure string option is recommended... but tbh most real world cases where you'd use this script you're just looking for something quick and easy
@JohnLBevan
JohnLBevan / Import-CommentedListFile.ps1
Last active April 27, 2021 11:19
A helpful cmdlet for converting a file containing a bunch of things of a certain type to that type, whilst handling comments and whitespace in the file easily.
Function Import-CommentedListFile {
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[ValidateScript({(Test-Path -Path $_) -or (&{throw "Path does not exist: '$_'"})})]
[string]$Path
,
[Parameter()]
[string]$Encoding = 'UTF8'
,
function Get-AtlassianScimUsers {
[CmdletBinding()]
Param (
[Parameter(Mandatory)]
[string]$DirectoryId
,
[Parameter(Mandatory)]
[string]$Token
)
$startIndex = 1
@JohnLBevan
JohnLBevan / MigratePostgres.sql
Last active December 1, 2020 16:20
A few scripts to aid in migrating db schemas in postgres. This is a hacky approach, but good for sorting out issues where environments have data with schema changes not tracked in `dotnet ef` migrations (for example).
-- note: each SQL should be run on its own, then its outputs run, before moving on.
-- this is a hacky solution, so just for those quick fix scenarios
-- rename existing tables to include an underscore on the end
SELECT format ('ALTER TABLE %I.%I RENAME TO %I;', table_schema, table_name, table_name || '_')
table_schema, table_name)
FROM information_schema.tables
where table_schema = 'cam'
-- remove any constraints on our table
@JohnLBevan
JohnLBevan / Convert-ImageSize.ps1
Last active November 12, 2020 15:01
Google Backup and Sync - Resize Photos For Upload
# hackaround for https://support.google.com/drive/thread/35629075?hl=en
# Warnings:
# I've not put much sanity wraping around this solution as I just needed a quick fix... there are some risks / caveats / room for improvement
# The save method will overwrite existing files with the same name / directory without prompting. Adding a check before saving is trivial / you could add Force, ShouldProcess, NoClobber, etc options as needed.
# You can't use the `NewNameMask = '{0}{3}{1}{2}'` (i.e. overwrite the source file), as we keep the file handle of the source open until after the resized image is changed. If that's a need, it's fairly simple to correct; I've just not needed the extra effort
Function Convert-ImageSize {
[CmdletBinding()]
Param (
@JohnLBevan
JohnLBevan / Test-IsExecutable
Last active October 31, 2020 17:06
Test if a file in a git repo is executable (i.e. has `chmod+x` set). Works on Windows as well as *nix systems.
# Thanks to Torek for the git command
# https://stackoverflow.com/questions/64607795/query-git-index-for-chmod-values-on-windows/64617017#64617017
# https://git-scm.com/docs/git-ls-files
function Test-IsExecutable {
[CmdletBinding()]
Param (
# Side note: Not only should Path point to a file/files within a git repo
# but also you should call this command from within that same repo
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
{
"recommendations": [
"redhat.vscode-yaml"
]
}
@JohnLBevan
JohnLBevan / ParseIIS.ps1
Created October 16, 2020 11:12
Regex to parse IIS Logs
pushd C:\inetpub\logs\LogFiles\W3SVC1
[Regex]$regex = '^(?<date>[\d-]+)\s(?<time>[\d\:]+)\s(?<ServerIP>[\d\.]+)\s(?<method>\S+)\s(?<path>\S+)\s(?<querystring>\S+)\s(?<port>\d+)\s(?<username>\S+)\s(?<clientIP>[\d\.]+)\s(?<browser>\S+)\s(?<fulluri>\S+)\s(?<HttpStatus>\d+)\s(?<a>\d+)\s(?<b>\d+)\s(?<c>\d+)$'
cat 'u_ex201015.log' | ?{$_ -like '2020-10-15 07*'} | %{
if ($_ -match $regex) {
([PSCustomObject]$Matches)
} else {
throw "Unexpected line format: '$_'"
}
} | ft time, ClientIP, username, httpstatus, port, path, querystring -AutoSize
popd
@JohnLBevan
JohnLBevan / TerraformNotes.md
Last active July 23, 2020 10:19
Useful articles and hints around Terraform / Terragrunt
@JohnLBevan
JohnLBevan / RateLimitExcel.ps1
Created July 10, 2020 06:26
Dirty hack to prevent 1 user's Excel from hogging CPU and impacting all other users on the same RDP session. Proper solution would be to implement auto-scaling (that's in the pipeline, but this gets us around the immediate issue)
[CmdletBinding()]
Param()
$noProcessors = (Get-WmiObject -class 'Win32_Processor' -Namespace 'root\cimv2').NumberOfLogicalProcessors
if ($noProcessors -gt 1) {
$maxAffinity = [Math]::Pow(2, $noProcessors) - 1 # i.e. this signfies that all processors are assigned to a process
$procsPerUser = [Math]::Ceiling($noProcessors / 3) # i.e. allow users to use 1/3rd the available processors (rounded up)
while ($true) {