Skip to content

Instantly share code, notes, and snippets.

@nicolonsky
nicolonsky / DisableSelfServicing.ps1
Created March 11, 2020 09:31
Disable self-service purchases
# Disable Office 365 Self-Servicing Purchases
# src: https://docs.microsoft.com/en-us/microsoft-365/commerce/subscriptions/allowselfservicepurchase-powershell?view=o365-worldwide
# Install abd connect MSCommerce services with PowerShell
Install-Module -Name MSCommerce -Scope CurrentUser
Connect-MSCommerce
Get-MSCommerceProductPolicies -PolicyId AllowSelfServicePurchase
# Disable Self-Servicing for all products
@nicolonsky
nicolonsky / LicenseAssignmentErrors.ps1
Created December 11, 2019 17:41
Get all Office 365, Azure users with license assignment errors [requires msonline module]
$licenseAssignmentErrors = Get-MsolGroup -HasLicenseErrorsOnly $true | % {
$groupId = $_.ObjectId
Get-MsolGroupMember -All -GroupObjectId $groupId |
Get-MsolUser -ObjectId {$_.ObjectId} |
Where {$_.IndirectLicenseErrors -and $_.IndirectLicenseErrors.ReferencedObjectId -eq $groupId} |
@nicolonsky
nicolonsky / ConnectMsGraph-AzSCP.ps1
Created December 10, 2019 10:17
Connect to Microsoft Graph with an Azure Service Principal certificate
servicePrincipalConnection = Get-AutomationConnection -Name "AzureRunAsConnection" -ErrorAction Stop
Update-MSGraphEnvironment -AuthUrl "https://login.microsoftonline.com/$($servicePrincipalConnection.TenantId)" -AppId $servicePrincipalConnection.ApplicationId
Connect-MSGraph -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint -Quiet
Get-DeviceManagement_ManagedDevices
@nicolonsky
nicolonsky / Change-AutopilotOrderID.ps1
Last active February 5, 2024 13:32
Bulk Update Windows Autopilot entities
Connect-MSGraph
Update-MSGraphEnvironment -SchemaVersion "Beta" -Quiet
Connect-MSGraph -Quiet
# Get all autopilot devices (even if more than 1000)
$autopilotDevices = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceManagement/windowsAutopilotDeviceIdentities" | Get-MSGraphAllPages
# Display gridview to show devices
$selectedAutopilotDevices = $autopilotDevices | Out-GridView -OutputMode Multiple -Title "Select Windows Autopilot entities to update"
@nicolonsky
nicolonsky / Rollover-KerberosDecryptionKey.ps1
Last active August 19, 2019 12:36
Roll Over Kerberos Decryption Key
Import-Module "$env:ProgramFiles\Microsoft Azure Active Directory Connect\AzureADSSO.psd1"
New-AzureADSSOAuthenticationContext
$creds = Get-Credential
Update-AzureADSSOForest -OnPremCredentials $creds
Connect-AzureAD
$runAsAccountServicePrincipal = Get-AzureADServicePrincipal -ObjectId "497421d0-5b78-4cf1-bab1-6ad2e3d7f319"
$accountAdminDirectoryRole= Get-AzureADDirectoryRole | where-object {$_.DisplayName -eq "User Account Administrator"}
Add-AzureADDirectoryRoleMember -ObjectId $accountAdminDirectoryRole.ObjectId `
-RefObjectId $runAsAccountServicePrincipal.ObjectId
Get-AzureADDirectoryRoleMember -ObjectId $accountAdminDirectoryRole.ObjectId
function New-Example{
[CmdletBinding()]
Param(
[Parameter(
Mandatory=$true,
Position=0)]
[ValidateScript({
$requiredProperties=@("Property1","Property2","Property3", "Property4")
[ValidateScript({
$requiredProperties=@("Property1","Property2","Property3", "Property4")
$members=Get-Member -InputObject $_ -MemberType NoteProperty
$missingProperties=Compare-Object -ReferenceObject $requiredProperties -DifferenceObject $members.Name -PassThru -ErrorAction SilentlyContinue
if (-not($missingProperties)){
#Requieres -Version 5.0
<#
.SYNOPSIS
You can use this script for troubleshooting or engineering purposes to verify if TCP ports are opened.
.DESCRIPTION
With this Script you are able to specify server names and port numbers to check in a CSV File.
The Script generates an CSV output file as a report.
#requires -Modules AzureAD
$UsageLocation = Get-AutomationVariable -Name "UsageLocation" -ErrorAction Stop
$servicePrincipalConnection = Get-AutomationConnection -Name "AzureRunAsConnection" -ErrorAction Stop
Connect-AzureAD -TenantId $servicePrincipalConnection.TenantId -ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
Get-AzureADUser | Where-Object{$_.UsageLocation -ne $UsageLocation} | ForEach-Object {