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 I’ve been able to come up with as it doesn’t even need the Teams PowerShell Module! This was updated on 3/23/2023 to add support for GCC, GCC High, DOD, & DOD High customers. Updated again on 8/2/2023 to support the automatic attempt of each Microsoft cloud before failing.
How to Run:
Get-CsHostedMigrationURL -Domain microsoft.com
Get-CsHostedMigrationURL microsoft.com
Function Code:
function Get-CsHostedMigrationURL
{
param(
[Parameter(Mandatory=$true)][string]$Domain
)
try {
$HMSID = (((Invoke-WebRequest -Uri https://webdir.online.lync.com/Autodiscover/AutodiscoverService.svc/root?originalDomain=$($Domain) -ErrorAction Stop).content -split "webdir")[3] -split ".online")[0]
$MigrationURL = "https://admin" + "$($HMSID)" + ".online.lync.com/HostedMigration/hostedmigrationService.svc"
}
catch {
#Domain is not associated with a Commercial Cloud or GCC Tenant. Attempt other Cloud Environments.
try {
$HMSID = (((Invoke-WebRequest -Uri https://webdir.online.gov.skypeforbusiness.us/Autodiscover/AutodiscoverService.svc/root?originalDomain=$($Domain) -ErrorAction Stop).content -split "webdir")[3] -split ".online")[0]
$MigrationURL = "https://admin" + "$($HMSID)" + ".gov.skypeforbusiness.us/HostedMigration/hostedmigrationService.svc"
}
catch {
#Domain is not associated with a GCC High Tenant. Attempt other Cloud Environments.
try {
$HMSID = (((Invoke-WebRequest -Uri https://webdir.online.dod.skypeforbusiness.us/Autodiscover/AutodiscoverService.svc/root?originalDomain=$($Domain) -ErrorAction Stop).content -split "webdir")[3] -split ".online")[0]
$MigrationURL = "https://admin" + "$($HMSID)" + ".dod.skypeforbusiness.us/HostedMigration/hostedmigrationService.svc"
}
catch {
#Domain is not associated with a DOD or DOD High Tenant. Attempt other Cloud Environments.
try {
$HMSID = (((Invoke-WebRequest -Uri https://webdir.online.partner.lync.cn/Autodiscover/AutodiscoverService.svc/root?originalDomain=$($Domain) -ErrorAction Stop).content -split "webdir")[3] -split ".online")[0]
$MigrationURL = "https://admin" + "$($HMSID)" + ".online.partner.lync.cn/HostedMigration/hostedmigrationService.svc"
}
catch {
#Domain is not associated with a CN 21Vianet Tenant. No other clouds left to try.
Write-Error "Failed to get the Hosted Migration URL. Either no internet connection was available, or the domain is not associated with a tenant. The exception caught was $_" -ErrorAction Stop
}
}
}
}
$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