-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd-IntuneDevicetoEntraGroup.ps1
More file actions
65 lines (49 loc) · 1.93 KB
/
Add-IntuneDevicetoEntraGroup.ps1
File metadata and controls
65 lines (49 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
##########################################################################
#Add-IntuneDevicetoEntraGroup.ps1
#Author: Sujin Nelladath
#LinkedIn : https://www.linkedin.com/in/sujin-nelladath-8911968a/
############################################################################
#Set-ExecutionPolicy
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
#Connect to Microsoft Graph
Connect-Graph -Scopes "GroupMember.ReadWrite.All", "Device.ReadWrite.All"
# Define Microsoft Graph API endpoint
$GraphBaseURL = "https://graph.microsoft.com/v1.0"
# Function to get Group ID by name
function Get-GroupID
{
param ($GroupName)
$GroupURL = "$GraphBaseURL/groups?`$filter=displayName eq '$GroupName'"
$Group = Invoke-MgGraphRequest -Uri $GroupURL -Method GET
return $Group.value[0].id
}
# Function to get Device ID by name
function Get-DeviceID
{
param ($DeviceName)
$DeviceURL = "$GraphBaseURL/devices?`$filter=displayName eq '$DeviceName'"
$Device = Invoke-MgGraphRequest -Uri $DeviceURL -Method GET
return $Device.value[0].id
}
# Prompt user for Group Name
$GroupName = Read-Host "Enter Intune group name"
$GroupName = $GroupName.Trim()
$GroupID = Get-GroupID -GroupName $GroupName
if (!$GroupID)
{
Write-Host "Group not found. Exiting.";
exit
}
# Prompt user for Device Name
$DeviceName = Read-Host "Enter device name"
$DeviceID = Get-DeviceID -DeviceName $DeviceName
if (!$DeviceID)
{
Write-Host "Device not found. Exiting.";
exit
}
# Add Device to Group
$AddMemberURL = "$GraphBaseURL/groups/$GroupID/members/`$ref"
$Body = @{ "@odata.id" = "$GraphBaseURL/directoryObjects/$DeviceID" } | ConvertTo-Json
Invoke-MgGraphRequest -Uri $AddMemberURL -Method POST -Body $Body
Write-Host "Device $DeviceName successfully added to group $GroupName"