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 / Remove-AzTagTrailingSpace.ps1
Created July 2, 2021 12:03
Remove trailing spaces from Azure tag names
#
# Rough and ready script to clean up trailing spaces from tag names
# Loops through all resources and resource groups under all subscriptions to which you have access.
# If a resource isn't already tagged with the clean (same name with no trailing space) then the clean tag name is given the diry tag's value, and the dirty tag is removed.
# If a resource has both clean and dirty tags, this shows a warning (even if the values are the same... I was lazy / didn't add a check for that) and leaves to the caller to fix manually.
# If a resource doesn't have any dirty tags, it's not affected.
#
Clear-Host
Login-AzAccount # (interactive / opens web browser)
@JohnLBevan
JohnLBevan / Test-SqlConnection.ps1
Created May 18, 2021 16:17
A script for quickly testing a SQL Server Db Connection; useful when debugging potential connectivity issues (beyond just Test-NetConnection)
Function Test-SqlConnection {
[CmdletBinding(DefaultParameterSetName = 'ByConnectionString')]
Param (
[Parameter(ParameterSetName = 'ByConnectionString', Mandatory)]
[string]$ConnectionString
,
[Parameter(ParameterSetName = 'AuthSqlUser', Mandatory)]
[Parameter(ParameterSetName = 'AuthAdCredentials', Mandatory)]
[Alias('DbInstance')]
[string]$ComputerName
@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