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 / 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 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 / 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 {
@JohnLBevan
JohnLBevan / Validate local user's credentials
Last active February 8, 2024 18:16
If you want to check the credentials you have for a local user account are correct, you can use this script (from: https://blog.idera.com/database-tools/test-local-user-account-credentials )
$username = 'username'
$password = 'password'
$computer = $env:COMPUTERNAME
Add-Type -AssemblyName 'System.DirectoryServices.AccountManagement'
$obj = [System.DirectoryServices.AccountManagement.PrincipalContext]::new('machine',$computer)
$obj.ValidateCredentials("$computer\$username", $password)
@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 / PrtgSmtpsCertificateScan.ps1
Last active November 29, 2022 14:31
A first pass at a script for monitoriong SMTPS certificicate lifetimes in PRTG. Notes on usage here: https://www.paessler.com/manuals/prtg/exe_script_advanced_sensor. Note; doesn't currently cover other TCP TLS certs (e.g. FTPS), but likely could with some additional tweaks...
Param(
[Parameter(Mandatory = $true)]
[string]$ComputerName
,
[Parameter()]
[int]$Port = 25
,
[Parameter()]
[System.Security.Authentication.SslProtocols]$SslProtocols = [System.Security.Authentication.SslProtocols]::GetValues([System.Security.Authentication.SslProtocols])
,
@JohnLBevan
JohnLBevan / GetOwernship.bat
Last active October 23, 2022 17:39
Get Ownership of / Access To Files
:: Recursively take ownership of all files under a folder, and amend their access to grant full ownership to admins.
takeown /R /A /D Y /F c:\temp\path
:: /R = Recursive
:: /A = Ownership to Admins
:: /D Y = answer Yes to any questions (/D N would answer No)
:: /F c:\temp\path = which path to run the command against
icacls c:\temp\path /grant Administrators:F /T /C
:: c:\temp\path The path to run the command against
@JohnLBevan
JohnLBevan / Export-X509CertToPemFiles.ps1
Created July 18, 2022 13:14
The start of a function for working with PEM certs in PS. Note: functionality for handling encrypted private keys was too complex wtihout third party libraries (e.g. BouncyCastle), so this doesn't full work yet / is a work in progress. There are some potential solutions for .net 6 out there (PemEncoding.Write), but that wasn't an option for the …
Function Export-X509CertToPemFiles {
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]
[System.Security.Cryptography.X509Certificates.X509Certificate2]$Certificate
,
[Parameter(Mandatory = $true)]
[System.IO.FileInfo]$FullChainPath
,
[Parameter(Mandatory = $true)]