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 / 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"
}
#heavily based on https://gallery.technet.microsoft.com/scriptcenter/Verify-the-Active-021eedea/view/Discussions#content
function Test-ADCredential {
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[System.Management.Automation.Credential()]
[System.Management.Automation.PSCredential]$Credential
)
begin {
@JohnLBevan
JohnLBevan / Thesaurus.cs
Last active April 18, 2018 13:24
Get a list of synonyms for a given term. This is heavily based on code from a question here: https://stackoverflow.com/q/49899308/361842
void Main()
{
using (var thesaurus = new Thesaurus())
{
foreach (var term in (new string[]{"dictionary","hot","actor","code"}))
{
Debug.WriteLine(term);
foreach (var value in thesaurus.GetSynonyms(term))
{
Debug.WriteLine($"- {value}");
@JohnLBevan
JohnLBevan / Get-MailRecipients
Created April 6, 2018 18:29
Find everyone a mail's addressed to by loading an MSG file from MS Outlook
function Get-MailRecipients {
[CmdletBinding(DefaultParameterSetName = 'ByFilename')]
Param (
[Parameter(Mandatory = $true, ParameterSetName = 'ByFilename', ValueFromPipeline = $true)]
[string]$Path
,
[Parameter(Mandatory = $false, ParameterSetName = 'ByFilename')]
[PSObject]$Outlook = $null
,
[Parameter(Mandatory = $true, ParameterSetName = 'ByMessage')]
@JohnLBevan
JohnLBevan / Remove-Service.ps1
Last active April 6, 2018 09:29
Delete a Windows service (works fine with service names containing spaces too; uses the service's short name; i.e. Service Name rather than Display Name ). See https://stackoverflow.com/a/76127/361842 for more info.
function Remove-Service {
[CmdletBinding()]
Param (
[Parameter()]
[string]$Name
)
Process {
#could be improved by implementing everything the full suite of parameters that Start/Stop-Service have; for now just fulfils the most basic requirement
Stop-Service -Name $Name -Force -ErrorAction SilentlyContinue
sc.exe delete $Name
function Compress-Guid {
[CmdletBinding()]
Param (
[Parameter(ValueFromPipeline = $true)]
[Guid]$Guid = [Guid]::NewGuid()
)
Process {
Write-Verbose "Received Guid $Guid"
[string]$GuidJustTheDigits = ($Guid.ToString() -replace '[{}-]','') #actually we don't need to replace {} since PS defaults to hyphens only formatting; but this does no harm / there's not much additional cost.
Write-Verbose "Just the digits length: $($GuidJustTheDigits.Length)" #32 chars
@JohnLBevan
JohnLBevan / ShrinkPath.ps1
Created March 17, 2018 16:27
Path environment variable too long; trim down some of the SQL entries by replacing with other environment variables...
cls
[Environment]::SetEnvironmentVariable("MSSQL64",'C:\Program Files\Microsoft SQL Server',"Machine")
[Environment]::SetEnvironmentVariable("MSSQL86",'C:\Program Files (x86)\Microsoft SQL Server',"Machine")
$path = [Environment]::GetEnvironmentVariable("PATH","User")
$path = ($path -replace 'C:\\Program Files\\Microsoft SQL Server\\','%MSSQL64%\') -replace 'C:\\Program Files \(x86\)\\Microsoft SQL Server\\','%MSSQL86%\'
[Environment]::SetEnvironmentVariable("PATH",$path,"User")
$path = [Environment]::GetEnvironmentVariable("PATH","Machine")
$path = ($path -replace 'C:\\Program Files\\Microsoft SQL Server\\','%MSSQL64%\') -replace 'C:\\Program Files \(x86\)\\Microsoft SQL Server\\','%MSSQL86%\'
@JohnLBevan
JohnLBevan / BidirectionalDictionary.cs
Created March 16, 2018 23:28
Bidirectional generic dictionary; used for simple 1:1 mappings. Just holds 2 dictionaries 1 key,value the other value,key. Probably a better solution exists, but this meets my needs.
using System;
using System.Collections;
using System.Collections.Generic;
namespace JohnLBevan.Collections.Generic
{
public class BidirectionalDictionary<T1, T2>: IDictionary<T1, T2>
{
IDictionary<T1, T2> ab;
@JohnLBevan
JohnLBevan / AdvancedEnum.cs
Last active March 7, 2018 12:33
Futureproof enum classes
/*
A while back I started using classes with static properties to represent enums
More on that here: https://stackoverflow.com/a/15713789/361842
This is the beginning of a reusable base class to allow me to easily create
these pseudo-enums without having to repeat myself each time.
Not yet complete; once done I'll add another file to this gist to represent a couple of use cases...
*/
public abstract class AdvancedEnum
@JohnLBevan
JohnLBevan / GuidFormats.cs
Created March 2, 2018 14:19
Quick Reference :: GUID Formatting in .Net
//case of the format makes no difference (e.g. D and d give the same output / have no impact on the output string's case as suspected)
Guid guid = Guid.NewGuid();
Console.WriteLine(guid); //same as D/d
//75dad478-98cf-4a5f-afc4-ad172747db0b
Console.WriteLine(guid.ToString("D")); //digits and hyphens
//75dad478-98cf-4a5f-afc4-ad172747db0b
Console.WriteLine(guid.ToString("d")); //digits and hyphens
//75dad478-98cf-4a5f-afc4-ad172747db0b
Console.WriteLine(guid.ToString("N")); //just digits
//75dad47898cf4a5fafc4ad172747db0b