forked from directorcia/Office365
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmggraph-connect.ps1
69 lines (61 loc) · 2.42 KB
/
mggraph-connect.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
param(
[switch]$debug = $false, ## if -debug parameter don't prompt for input
[switch]$noupdate = $false, ## if -noupdate used then module will not be checked for more recent version
[switch]$noprompt = $false ## if -noprompt parameter used don't prompt user for input
)
<#CIAOPS
Script provided as is. Use at own risk. No guarantees or warranty provided.
Description - Connect to the Mirosoft Graph
Source -
Prerequisites = 1
1. Ensure Microsoft.Graph module is loaded
#>
## Variables
$systemmessagecolor = "cyan"
$processmessagecolor = "green"
$errormessagecolor = "red"
$warningmessagecolor = "yellow"
<#
# Increase the Function Count
$MaximumFunctionCount = 8192
# Increase the Variable Count
$MaximumVariableCount = 8192
#>
Clear-Host
if ($debug) {
write-host "Script activity logged at ..\mggraph-connect.txt"
start-transcript "..\mggraph-connect.txt" | Out-Null ## Log file created in parent directory that is overwritten on each run
}
write-host -foregroundcolor $systemmessagecolor "Microsoft Graph connect script started"
Try {
Import-Module Microsoft.Graph | Out-Null
}
catch {
Write-Host -ForegroundColor $errormessagecolor "`n[001] - Failed to import Graph module - ", $_.Exception.Message
if ($debug) {
Stop-Transcript | Out-Null
}
exit 1
}
write-host -foregroundcolor $processmessagecolor "Microsoft Graph module loaded"
try {
Connect-MgGraph | Out-Null
Select-MgProfile -name "beta" -ErrorAction stop # Use this to force a version of the Graph version (v1.0 or beta)
}
catch {
Write-Host -ForegroundColor $errormessagecolor "`n[002] - Failed to connect to Graph - ", $_.Exception.Message
if ($PSVersionTable.PSVersion.major -le 5) {
Write-Host -ForegroundColor $warningmessagecolor "`n **** WARNING ****"
Write-Host -ForegroundColor $warningmessagecolor " Old versions of PowerShell environment don't support full Microsoft Graph loads"
Write-Host -ForegroundColor $warningmessagecolor " Either use a newer PowerShell version or just load required modules to reduce memory usage"
}
if ($debug) {
Stop-Transcript | Out-Null
}
exit 2
}
write-host -foregroundcolor $processmessagecolor "Connected to Microsoft Graph`n"
Write-Host -ForegroundColor $systemmessagecolor "`nMicrosoft Graph connect script finished"
if ($debug) {
Stop-Transcript | Out-Null
}