Skip to content

Instantly share code, notes, and snippets.

@AlexanderHolmeset
Last active January 4, 2023 19:26
Show Gist options
  • Save AlexanderHolmeset/f7a1cfa6175602f78ea3537a9893bbb1 to your computer and use it in GitHub Desktop.
Save AlexanderHolmeset/f7a1cfa6175602f78ea3537a9893bbb1 to your computer and use it in GitHub Desktop.
### Teams Devices Configuration Profiles Uploader ###
### Version 1.0 ###
### Author: Alexander Holmeset ###
### Email: [email protected] ###
### Twitter: twitter.com/alexholmeset ###
### Blog: alexholmeset.blog ###
### Thanks to Jens Madsen for idea and extensive CSV ###
### with predefined configuration profiles. ###
### https://twitter.com/JensHMadsen ###
$PowerShellVersion = ((Get-Host).Version).Major
If($PowerShellVersion -eq 7){
Write-Host "PowerShell 7 does not support interatctive input longer than 1024 characters."
Write-Host "Open script in editor and paste token in token variable at line 25 instead of read host command."
Write-Host "Then remove line 11 to 20."
return
}
Write-Host "================ Teams Devices Configuration Profiles Uploader ================"
Write-Host ""
$Token = Read-Host "Enter Teams Admin Center token to authenticate"
#Checking if token is valid.
Try{
$URL = "https://admin.devicemgmt.teams.microsoft.com/api/v2/configProfiles"
$header = @{Authorization = "Bearer $token"}
$ConfigProfiles = (((Invoke-RestMethod -Uri $URL -Method GET -Headers $header -ContentType "application/json").paginatedconfigprofiles).baseinfo).id
}
Catch{Write-Host "";Write-Host "Authentication failed!" -ForegroundColor red;Return}
Write-Host ""
Write-Host "Authentication OK!" -ForegroundColor Green
Write-Host ""
function UploadConfigProfiles {
$URL = "https://admin.devicemgmt.teams.microsoft.com/api/v2/configProfiles"
$header = @{Authorization = "Bearer $token"}
$DeviceProfiles = Import-Csv (Read-Host "Enter full path of CSV file included filename")
foreach($DeviceProfile in $DeviceProfiles){
$body = @"
{
"identity":"$($DeviceProfile.identity)",
"deviceType":"$($DeviceProfile.deviceType)",
"description":"$($DeviceProfile.description)",
"configProfileCreationType":$($DeviceProfile.configProfileCreationType),
"deviceLock":"$($DeviceProfile.deviceLock)",
"deviceLockTimeout":"$($DeviceProfile.deviceLockTimeout)",
"deviceLockPin":"$($DeviceProfile.deviceLockPin)",
"language":"$($DeviceProfile.language)",
"timeZone":"$($DeviceProfile.timeZone)",
"timeZoneId":"$($DeviceProfile.timeZoneId)",
"dateFormat":"$($DeviceProfile.dateFormat)",
"timeFormat":"$($DeviceProfile.timeFormat)",
"displayScreenSaver":"$($DeviceProfile.displayScreenSaver)",
"screenSaverTimeout":"$($DeviceProfile.screenSaverTimeout)",
"displayBacklitBrightness":"$($DeviceProfile.displayBacklitBrightness)",
"displayBacklitTimeout":"$($DeviceProfile.displayBacklitTimeout)",
"displayHighContrast":"$($DeviceProfile.displayHighContrast)",
"silentMode":"$($DeviceProfile.silentMode)",
"officeStartHours":"$($DeviceProfile.officeStartHours)",
"officeEndHours":"$($DeviceProfile.officeEndHours)",
"powersaving":"$($DeviceProfile.powersaving)",
"loggingEnabled":"$($DeviceProfile.loggingEnabled)",
"loggingTypes":"$($DeviceProfile.loggingTypes)",
"networkDhcp":"$($DeviceProfile.networkDhcp)",
"networkWifi":"$($DeviceProfile.networkWifi)",
"networkPcPort":"$($DeviceProfile.networkPcPort)",
"deviceDefaultAdminPassword":"$($DeviceProfile.deviceDefaultAdminPassword)",
"screenCapture":"$($DeviceProfile.screenCapture)",
"dhcpEnabled":"$($DeviceProfile.dhcpEnabled)",
"hostName":"$($DeviceProfile.hostName)",
"domainName":"$($DeviceProfile.domainName)",
"ipAddress":"$($DeviceProfile.ipAddress)",
"subnetMask":"$($DeviceProfile.subnetMask)",
"defaultGateway":"$($DeviceProfile.defaultGateway)",
"primaryDNS":"$($DeviceProfile.primaryDNS)",
"secondaryDNS":"$($DeviceProfile.secondaryDNS)",
"theme":{"name":"$($DeviceProfile.theme)"},
"usageStateIndicatorBusyStateColor":{"name":"$($DeviceProfile.usageStateIndicatorBusyStateColor)"}
}
"@
Invoke-RestMethod -Uri $URL -Method POST -Headers $header -ContentType "application/json" -Body $body
}
Write-Host "Configuration profiles upload is completed!"
}
function DeleteConfigProfiles {
Write-Host ""
Write-Host "Are you sure you want to delete all configuration profiles?"
Write-Host ""
$input = Read-Host "Type Yes or No."
switch ($input)
{
'Yes' {
If($input -like "Yes"){
Write-Host ""
Write-Host "Deletion of configuration profiles initiated!"
Write-Host ""
}
} 'No' {
Write-Host ""
Write-Host 'Script aborted!' -ForegroundColor Red
return
}
}
$URL = "https://admin.devicemgmt.teams.microsoft.com/api/v2/configProfiles"
$header = @{Authorization = "Bearer $token"}
while ($ConfigProfilesCount -ne 0 ) {
$ConfigProfiles = (((Invoke-RestMethod -Uri $URL -Method GET -Headers $header -ContentType "application/json").paginatedconfigprofiles).baseinfo).id
$ConfigProfilesCount = $ConfigProfiles.Count
foreach($ConfigProfile in $ConfigProfiles){
$count2--
Write-Host ""
"Configuration profile deleted!"
$url2 = "https://admin.devicemgmt.teams.microsoft.com/api/v2/configProfiles/$ConfigProfile"
Invoke-RestMethod -Uri $URL2 -Method DELETE -Headers $header
}
}
If($input -like "Yes"){
write-Host ""
Write-Host "Configuration Profiles deleteion completed!"
}
}
Write-Host ""
Write-Host "1: Press '1' to uplload config prolfiles to Teams Admin Center."
Write-Host "2: Press '2' to delete all config profies from Teams Admin Center."
Write-Host "Q: Press 'Q' to quit."
Write-Host ""
$input = Read-Host "Please make a selection"
switch ($input)
{
'1' {
Write-Host ""
UploadConfigProfiles
} '2' {
Write-Host ""
DeleteConfigProfiles
}
'q' {
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment