-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-MS365GlobalAdminAccounts.ps1
122 lines (86 loc) · 2.56 KB
/
get-MS365GlobalAdminAccounts.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
<#PSScriptInfo
.VERSION 1.2
.AUTHOR TechnologyWanderers Youtube Channel
.COMPANYNAME
.COPYRIGHT
.TAGS O365 Reprt
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES https://www.youtube.com/c/technologywanderers
#>
#requires -Version 3.0 -Modules MSOnline
#Provide O365 admin creds
#create crdential here this in only one time command # it once xml is generated.
#Get-Credential "[email protected]" | Export-Clixml .\creds.xml
#Authenticate with XML file
$cred = Import-Clixml "C:\Automation\credentials\automation.xml"
#Create Session
connect-msolservice -Credential $cred
# Creating a function that generates the reports
function Get-Office365AdminReport {
# Several reports
$a=Get-MsolRoleMember -RoleObjectId $(Get-MsolRole -RoleName "Company Administrator").ObjectId | Select-Object -Property RoleMemberType,DisplayName,EmailAddress,isLicensed,LastDirSyncTime | ConvertTo-Html
Get-MsolRoleMember -RoleObjectId $(Get-MsolRole -RoleName "Company Administrator").ObjectId | Select-Object -Property RoleMemberType,DisplayName,EmailAddress,isLicensed | export-csv ".\MS365AdminAccountSummary.csv"
# Creating the HTML Page
$Html = @"
<!DOCTYPE html>
<head>
<title>MS 365 License & Unlicensed users created last 7 days</title>
<style>
body {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
}
table, th, td {
white-space: nowrap;
border: 1px solid black;
text-align: center;
border-collapse: collapse;
padding: 4px;
}
th {
background-color: midnightblue;
color: white;
}
</style>
</head>
<body>
<br>
<centre>
<br>
<br>
<br>
<br>
MS 365 Global Admin Roles Report
</centre>
<br>
<centre>
<table class="Techwanderers">
<tr>
<td class="Techwanderers-1" colspan="26">$a</td>
</tr>
</table>
</centre>
<br>
<br>
<br>
<br>
<br>
<br>
Best Regards,<br>
Office 365 Automation Support<br>
URL: https://www.youtube.com/c/technologywanderers<br>
India<br>
"@
$date=(get-date).ToShortDateString()
##########################change your email address and other values here##########################
Send-Mailmessage -from "[email protected]" -To "[email protected]" -Credential $cred -SmtpServer smtp.office365.com -Port 587 -Subject "Automation: MS 365 Global Admin Roles Report - $date" -body $html -BodyAsHtml -Attachment .\MS365AdminAccountSummary.csv -UseSsl
}
# Running the function
Get-Office365AdminReport
Remove-Item -Path .\MS365AdminAccountSummary.csv -Force
exit