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 / 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 / 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 / Get-Dfo365EncryptedSettings.ps1
Created March 7, 2019 15:20
Code to pull values from Dynamics 365 for Finance and Operations (aka DFO365 / unified operations) Web.Config file and decrypt values where required, to find relevant credential information used by the OneBox install.
[string[]]$Assemblies = @(
'C:\AOSService\webroot\bin\Microsoft.Dynamics.AX.Framework.EncryptionEngine.dll'
)
[string]$CSSource = @"
using System;
using Microsoft.Dynamics.Ax.Xpp.Security;
namespace n201903071243 //use an odd namespace so I don't have to reload powershell each time I want to tweak this code; just tweak the NS
{
public class Dfo365CertificateThumbprintProvider: Microsoft.Dynamics.Ax.Xpp.Security.ICertificateThumbprintProvider
@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 / WebDavClient.linq
Last active March 9, 2023 14:28
A basic c# client for testing webdav endpoints
void Main()
{
var usr = @"myDomain\myUser";
var pwd = Util.GetPassword(usr); //linqpad util library
var uri = "https://webdavendpoint.example.com/somefolder/";
var fn = @"c:\temp\filetouploadTestData.txt";
var rn = "remoteFilenameTestData.txt";
var wd = new BasicWebDavClient(usr, pwd, uri);
wd.Put(fn, rn);
@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)
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 / Invoke-DFO365AdminUserProvisioningTool.ps1
Created May 1, 2018 08:50
Find and run the DFO365 Admin User Provisioning Tool
$serviceVolume = Get-PSDrive -PSProvider 'Microsoft.PowerShell.Core\FileSystem' | ?{$_.Description -eq 'Service Volume'}
$adminProvTool = Join-Path -Path $serviceVolume.Root -ChildPath 'AosService\PackagesLocalDirectory\bin\AdminUserProvisioning.exe'
if (Test-Path -Path $adminProvTool) {
"Admin User Provisioning Tool Found: $adminProvTool"
& $adminProvTool
} else {
Write-Error "Could not find admin user provisitioning tool at: $adminProvTool"
}