PowerShell Snippets

These are a few common snippets I use somewhat regularly and always find myself looking for.

$initialPW = $null
$initialPW += -join ('abcdefghkmnrstuvwxyzABCDEFGHKLMNPRSTUVWXYZ23456789$%&*#'.ToCharArray() | Get-Random -Count 45)
Start-Transcript -Path ($PSScriptRoot + "\" + ([io.fileinfo]$MyInvocation.MyCommand.Definition).BaseName + "_" + (get-date -f yyyy-MM-dd) + ".log")
        $mailSettings = @{
            Subject    = $Subject
            From       = $From
            SmtpServer = $smtpRelay
            Encoding   = [text.encoding]::UTF8
            To         = $EmailAddress
            Cc         = $CopyAddress
            Bcc        = $BlindCopyAddress
            Body       = $Body
            Priority   = "High"
        }

        $mailSettings.Body = $mailSettings.Body.replace("%displayName%",$ADObject.displayName)

        try {
            Send-MailMessage @mailSettings -BodyAsHtml -ErrorAction Stop
        } catch {
            Write-Host "[ERROR] "+$_.Exception.Message
        }
            $DN = $ADObject.distinguishedName
            $Domain = $DN -Split "," | ? {$_ -like "DC=*"}
            $Domain = $Domain -join "." -replace ("DC=", "")

Calulating the Immutable ID

The Immutable ID is used to link an on-premise user object to an entra ID user object.

Sidenote, I should really move these snippets to Github

$UserObject = Get-ADUser "CN=User1,OU=Corp.Users,DC=itsrob,DC=local" -server dc1.itsrob.local
$UserObjectGuid = $UserObject.ObjectGUID
$UserObjectGuidBase64 = [System.Convert]::ToBase64String($UserObjectGuid.ToByteArray())
write-host
write-host $UserObjectGuidBase64

NoHello

For a while now, Microsoft has been making memorable URLs by shortening them via the “aka.ms” prefix, which I truly appreciate.

One of my favourites addresses a pet peeve of mine: people who ping me with a mere “hello”.

See more here: https://aka.ms/nohello

SAML Claim Padding Transform

Recently I needed to include a 10-digit padded employee number in a SAML claim.

It seems that padding is a missing function within SAML Claim Transforms.

I tried a few things, like joining 000000000 to the employee ID (say 12345) then returning the rightmost 10 characters (0000012345), RegEx expressions, etc, but nothing seemed to work as expected. I even opened a Microsoft ticket on this, and there isn’t a solution for padding in the transforms.

Please visit Add claim manipulation for padding · Community (azure.com) to upvote this.