-
Notifications
You must be signed in to change notification settings - Fork 4
/
get-enrolled-devices.ps1
141 lines (93 loc) · 2.86 KB
/
get-enrolled-devices.ps1
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<#
.SYNOPSIS
This script is used to verify enrolled devices in MDM and Azure. Runbook will be used on schedule and will calculate new
joiners based on enrollemnt date
PAramters are check inteval = how long back in time interval
#>
[cmdletbinding()]
param
(
[Parameter(Mandatory=$true)] $interval
)
###########################
#### Create-Cred
##########################
function Create-Cred {
param
(
[Parameter(Mandatory=$true)][string]$user,
[Parameter(Mandatory=$true)][string]$pfile
)
$myCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, (Get-Content $pfile | ConvertTo-SecureString)
return $myCred
}
#################################
## Get-AuthToken-Pass
#################################
Function Get-AuthToken-Pass {
[cmdletbinding()]
param
(
[Parameter(Mandatory=$true)][string]$tenantName,
[Parameter(Mandatory=$true)][System.Management.Automation.PSCredential]$cred
)
try{
return (Get-MSIntuneAuthToken -TenantName $tenantName -credential $cred)
}
catch {
write-output "Error Getting token"
}
}
#################################
## End-AuthToken-Pass
#################################
###################################
## Function Get-enrolled-devs
###################################
Function get-enrolled-devs {
[cmdletbinding()]
param
(
[Parameter(Mandatory=$true)] $intervMin
)
# Defining Variables
$graphApiVersion = "beta"
$Resource = "deviceManagement/managedDevices"
$checkTime=(get-date).AddMinutes($intervMin)
write-output "checkTime is "$checkTime
$uri = "https://graph.microsoft.com/$graphApiVersion/$Resource"
$devices = (Invoke-RestMethod -Uri $uri -Headers $authToken -Method Get).Value
$retdevs =@()
if ($devices){
# $devices
foreach ($dev in $devices ){
# if ($dev.autopilotEnrolled = $true)
if ($dev.managementState = 'managed'){
# write-output "Device " $dev.devicename " enrollment time " $dev.enrolledDateTime
if ( [datetime]$dev.enrolledDateTime -gt [datetime]$checkTime ){
$retdevs = $retdevs += $dev
write-output "New Device " $dev.devicename
}
}
}
}
return $retdevs
}
######################################
#### End Get-enrolled-devices
######################################
# Import required modules
try {
Import-Module -Name AzureAD -ErrorAction Stop
Import-Module -Name PSIntuneAuth -ErrorAction Stop
# Import-Module -Name UnofficialIntuneManagement -ErrorAction Stop
}
catch {
Write-Warning -Message "Failed to import modules"
}
$tenant="yourten.com"
$passfile="youruser.pass"
$user = "youruser@yourten.com"
$cred = Create-Cred -user $user -pfile $passfile
$authtoken = Get-AuthToken-Pass -tenantName $tenant -cred $cred
get-enrolled-devs -intervMin $interval