forked from ruudmens/LazyAdmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectTo-O365.ps1
98 lines (79 loc) · 2.74 KB
/
ConnectTo-O365.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
<#
.SYNOPSIS
Connect to Office 365 to manage admin setting. Optional save credentials for autonomous execution
.DESCRIPTION
Connect to Office 365. You can store your credentials in the script root. When
saved the script will connect automaticaly. if no credentials are found it will ask for them.
.REQUIREMENTS
Install the Microsoft Online Services Sign-In Assistant
https://www.microsoft.com/en-us/download/details.aspx?id=41950
Install the Windows Azure Active Directory Module
https://go.microsoft.com/fwlink/p/?linkid=236297
.EXAMPLE
Connecting to Office 365
.\ConnectTo-O365.ps1
.EXAMPLE
Save credentials in the script root
.\ConnectTo-O365.ps1 -save
.NOTES
Version: 1.3
Author: R. Mens
Blog: http://lazyadmin.nl
Creation Date: 29 mrt 2017
.LINK
https://github.com/ruudmens/SysAdminScripts/tree/master/Connectors
#>
#-----------------------------------------------------------[Execution]------------------------------------------------------------
[CmdletBinding()]
PARAM(
[parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Mandatory=$false)]
[switch]$save=$false
)
BEGIN
{
$computerName = $env:COMPUTERNAME
$uaPath = "$PSScriptRoot\useraccount-$computerName.txt"
$ssPath = "$PSScriptRoot\securestring-$computerName.txt"
}
PROCESS
{
#region save credentials
If ($save)
{
#create securestring and store credentials
Write-Host 'Running in config mode, storing credentials in script root location' -ForegroundColor Yellow
$username = Read-Host "Enter your email address"
$secureStringPwd = Read-Host -assecurestring "Please enter your password"
#Storing password as a securestring
$secureStringText = $secureStringPwd | ConvertFrom-SecureString
Set-Content $ssPath $secureStringText
#Storing username
Set-Content $uaPath $username
Write-Host 'Credentials saved' -ForegroundColor Green
}
#endregion
Write-Host 'Connecting to Microsoft Online services' -ForegroundColor Yellow
#Check if a securestring password is stored in the script root
If (Test-Path $ssPath)
{
$securePwd = Get-Content $ssPath | ConvertTo-SecureString
}
#Check if useraccount is stored in the script root
If (Test-Path $uaPath)
{
$username = Get-Content $uaPath
}
#If the useraccount or password is empty, ask for the credentials
if (!$securePwd -or !$username)
{
Write-Host 'No credentials stored. Run with -save option to store credentials' -ForegroundColor Yellow
$username = Read-Host "Enter your email address"
$securePwd = Read-Host -assecurestring "Please enter your password"
}
#Create credential object
$credObject = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $securePwd
#Connect to Office 365
Connect-MsolService -Credential $credObject
}