Random PowerShell Code Snippets, PS Functions, & Links Library

The below list a continued collection of random PowerShell Code Snippets & Functions that I use for various things. If there is anything that you wish to have added, let me know in the comments below!
Get-CsHostedMigrationURL – PowerShell Function
If you are wanting to move users to Microsoft Teams from Skype for Business and are not using a .onmicrosoft.com admin account, you need to manually specify the hosted migration URL. Unfortunately, the newest Teams PowerShell (2.4.x) breaks Get-CsTenant and is no longer able to provide this. The below method is by far one of the best ive been able to come up with as it doesnt even need the Teams PowerShell Module!
How to Run:
Get-CsHostedMigrationURL -Domain microsoft.com
Function Code:
function Get-CsHostedMigrationURL
{
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[String]$Domain
)
$HMSID = try {
(((Invoke-WebRequest -Uri "https://webdir.online.lync.com/Autodiscover/AutodiscoverService.svc/root?originalDomain=$($Domain)").content -split "webdir")[3] -split ".online")[0]
}
catch
{
Write-Error "Failed to get the Hosted Migration URL. The exception caught was $_" -ErrorAction Stop
}
$MigrationURL = "https://admin" + "$($HMSID)" + ".online.lync.com/HostedMigration/hostedmigrationService.svc"
$MigrationURL
}
Get-TenantID – PowerShell Function
This provides the GUID of a Microsoft 365 Tenant ID. Simply input any tenant domain name. Thanks @Richard Brynteson
How to Run:
Get-TenantID -Domain Microsoft.com
Function Code:
function Get-TenantID
{
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[String]$Domain
)
try {
(Invoke-WebRequest https://login.windows.net/$($Domain)/.well-known/openid-configuration|ConvertFrom-Json).token_endpoint.Split(‘/’)[3]
}
catch
{
Write-Error "Failed to get the Tenant ID. The exception caught was $_" -ErrorAction Stop
}
}
Generate-NVKey – PowerShell Function
This PowerShell Function generates a random 32-bit Hex string that can be used for various tasks such as security. This is based on the output of New-Guid however since the 13th character is always a 4, we have to modify this to give us a completely random yet unique response.
function Generate-NVKey
{
function New-12Bit
{
((new-guid).guid).replace('-','').SubString(0,12)
}
(((New-12Bit) + (New-12Bit) + (New-12Bit)).ToUpper()).SubString(0,32)
}
Generate-NVKey
If you would like an output that is of any value 0-36 characters in length, change the 32 in the above function to the desired number.
Example Output:

Leave a Reply