-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintune_check_sync.ps1
More file actions
132 lines (96 loc) · 3.67 KB
/
Copy pathintune_check_sync.ps1
File metadata and controls
132 lines (96 loc) · 3.67 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
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
# Define a function to post messages to Teams
function Send-TeamsMessage {
param (
[string]$WebhookUrl,
[string]$Summary,
[string]$Text
)
$Headers = @{
"Content-Type" = "application/json"
}
$Body = @{
summary = $Summary
text = $Text
} | ConvertTo-Json
Invoke-RestMethod -Uri $WebhookUrl -Method Post -Headers $Headers -Body $Body
}
# Set your Teams webhook URL
$TeamsWebhookUrl = ""
# Function to check and send Intune device status
function Check-IntuneDeviceStatus {
param (
[int]$days
)
$daysago = "{0:s}" -f (Get-Date).AddDays(-$days) + "Z"
$CurrentTime = [System.DateTimeOffset]::Now
$result = @() # Initialize an empty array to store results
Write-Host
Write-Host "Checking to see if there are devices that haven't synced in the last $days days..." -f Yellow
Write-Host
# Filter devices not synced in the past $days days
$Devices = Get-IntuneManagedDevice -Filter "lastSyncDateTime le $daysago" | Sort-Object deviceName
$Devices = $Devices | Where-Object { $_.managementAgent -ne "eas" }
# If there are devices not synced in the past $days days, loop through and gather details
if ($Devices) {
$DeviceCount = $Devices.Count
Write-Host "There are $DeviceCount devices that have not synced in the last $days days..." -ForegroundColor Red
Write-Host
# Loop through the devices and gather details
foreach ($Device in $Devices) {
Write-Host "------------------------------------------------------------------"
Write-Host
$DeviceDetails = @"
Devices which have not been Synced in the last $days days, these will be needing to be synced in the near future.
<hr>
**Device Name :**
$($Device.deviceName)
**Management State :**
$($Device.managementState)
**Operating System :**
$($Device.operatingSystem)
**Device Type :**
$($Device.deviceType)
**Last Sync Date Time :**
$($Device.lastSyncDateTime)
**Enrolled Date Time :**
$($Device.enrolledDateTime)
**Jail Broken :**
$($Device.jailBroken)
**Compliance State :**
$($Device.complianceState)
**AAD Registered :**
$($Device.aadRegistered)
**Management Agent :**
$($Device.managementAgent)
**Days Since Last Sync :**
$([string]($CurrentTime - $Device.lastSyncDateTime).Days)
"@
# Add the device details to the result array
$result += $DeviceDetails
}
# Join the device details with line breaks to separate the results
$resultText = $result -join "`n`n"
# Post the result to Teams with a summary
Send-TeamsMessage -WebhookUrl $TeamsWebhookUrl -Summary "Intune Check Result" -Text $resultText
} else {
Write-Host "------------------------------------------------------------------"
Write-Host
Write-Host "No devices not checked in the last $days days found..." -f green
Write-Host
# Post a message to Teams indicating no devices found
$noDevicesMessage = @{
"Message" = "No devices not checked in the last $days days found..."
} | ConvertTo-Json
# Post the message to Teams
Send-TeamsMessage -WebhookUrl $TeamsWebhookUrl -Summary "Intune Check Result" -Text $noDevicesMessage
}
}
# Define the interval in seconds (6 hours)
$IntervalInSeconds = 6 * 60 * 60
# Loop indefinitely with a 6-hour interval
while ($true) {
Write-Host "Running Intune check..." -ForegroundColor Yellow
Check-IntuneDeviceStatus -days 30 # Call the function to check and send Intune device status
Write-Host "Update complete. Sleeping for 6 hours..." -ForegroundColor Green
Start-Sleep -Seconds $IntervalInSeconds
}