Untitled
unknown
plain_text
4 years ago
1.4 kB
581
Indexable
#Grab all devices
Function FetchAllDevices {
try{
$listOfDevices = Get-MgDevice -All $true
}catch{
Write-Error "Could not fetch devices"
}
return $listOfDevices
}
#Add device to target-group
Function AddNewMemberToTargetGroup {
param(
[Parameter(Mandatory=$true, Position=0)][Object[]]$device,
[Parameter(Mandatory=$true, Position=1)][String]$targetDeviceGroup
)
try{
New-MgGroupMember -GroupId $targetDeviceGroup -DirectoryObjectId $device.Id
Write-Output "New Member $($device.id) added to DeviceTargetGroup $($targetDeviceGroup)"
}catch{
Write-Error "Could not add new member $($device.id) to DeviceTargetGroup $($targetDeviceGroup)"
Write-Error "Maybe the device is already member of the group"
}
}
#Main Script
Function Main {
param(
[Parameter(Mandatory=$true, Position=1)][AllowNull()][Object[]]$devicesOfTargetGroup
)
try{
#Grab all devices
$listOfDevices = FetchAllDevices()
if($listOfDevices){
foreach ($device in $listOfDevices){
AddNewMemberToTargetGroup $device <OBJECT_ID of DeviceGroup>
}
}
}catch{
Write-Error $_
Write-Error "Unexpected error!"
}
}
# IT ALL STARTS HERE
try{
#Fetch all group-members
$devicesOfTargetGroup = Get-MgGroupMember -GroupId <OBJECT_ID of DeviceGroup>
Main $devicesOfTargetGroup
}catch{
Write-Error $_
Write-Error "Unexpected error!"
exit
}Editor is loading...