-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJob-ActiveSyncReport.ps1
86 lines (55 loc) · 5.06 KB
/
Job-ActiveSyncReport.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
<#
.SYNOPSIS
Scan Exchange enviroment for activesynce devices active in the last 30 days and export them to a spreadsheet
.DESCRIPTION
Exports the Name, Device Model, Device Type, Device OS and Last sync time.
.AUTHOR
Nate Stewart
#>
#create excel object
$excel = New-Object -ComObject "Excel.Application"
$excel.Visible = $true
$workbook = $excel.Workbooks.Add()
$worksheet = $workbook.Worksheets.Add()
$worksheet = $workbook.Activesheet
$worksheet.name = "ActivesyncDevicesReport"
$ocells = $worksheet.Cells
$filename = 'C:\scripts\' + 'ActivesyncReport-' + (Get-Date -Format M-dd-yy) + '.xlsx'
Function AddWorksheetHeaders() {
#This function adds the headers that are passed, then sets the column width.
$col = 1
$oColumns=$worksheet.Columns
foreach ($arg in $args) {
$ocells.item(1,$col)=$arg
$ocells.item(1,$col).font.bold=$True
$ocells.item(1,$col).font.underline=$True
$oColumns.item($col).ColumnWidth = 30
$col++
}
}
AddWorksheetHeaders "Name" "DeviceModel" "DeviceType" "DeviceOS" "LastSyncTime"
$row = 2
$Mailboxes = Get-CASMailbox -ResultSize Unlimited | Where {$_.HasActiveSyncDevicePartnership -eq $True}
$Devices = @()
#Populate spreadsheet
foreach ($mailbox in $mailboxes) {
$Devices= Get-ActiveSyncDeviceStatistics -Mailbox $mailbox.name | where { $_.LastSuccessSync -gt ((Get-Date).AddDays(-30)) }
ForEach ($device in $devices) {
$ocells.item($row,1) = $mailbox.name
$ocells.item($row,2) = $Device.DeviceModel
$ocells.item($row,3) = $Device.DeviceType
$ocells.item($row,4) = $Device.DeviceOS
$ocells.item($row,5) = $Device.LastSuccessSync
$row++
}
}
#close out excel com objects
$workbook.SaveAs($filename)
$workbook.Close()
$excel.Quit()
#There is a bug in powershell with excel com objects not releasing correctly and causing excel.exe process to not close
while( [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ocells)){}
while( [System.Runtime.Interopservices.Marshal]::ReleaseComObject($worksheet)){}
while( [System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook)){}
while( [System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)){}
Write-Host "`n""Saving file to $filename $directory"