-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate-Environment.ps1
More file actions
78 lines (75 loc) · 2.86 KB
/
Create-Environment.ps1
File metadata and controls
78 lines (75 loc) · 2.86 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
#Script: Create-Environment.ps1 https://github.com/TjWheeler/office365-devscripts
#Author: Tim Wheeler (http://timwheeler.io)
#Version: 0.4
#Purpose: Create/Update an environment file with encrypted passwords
#Remarks: An environment file is named [EnviornmentName].[environmentType].environment and stores data in JSON format
# : Environment files are used by other scripts to get credentials
#Example: .\create-environment.ps1 -siteUrl "https://mytenancy.sharepoint.com/sites/mysitecollection" -name "dev1" -username "my@email.com" -credentialType SharePointOnline -environmentType Dev -csomVersion:Online
param(
[String] $siteUrl = $(Read-Host "Specify SiteUrl"),
[String] $username = $(Read-Host "Specify Username"),
[String] $password = $null,
[String] $name = $(Read-Host "Specify Environment Name"),
[ValidateSet("Network","SharePointOnline")]
[String] $credentialType = "SharePointOnline",
[ValidateSet("Dev","Test","UAT","Prod")]
[String] $environmentType = $(Read-Host "Specify Environment Type Dev, Test, UAT, Prod"),
[ValidateSet("Online","2013","2016","2019")]
[String] $csomVersion = $(Read-Host "Specify CSOM Version Online, 2013, 2016, 2019")
)
&("$PSScriptRoot\Start.ps1")
$envPath = "$PSScriptRoot\env"
$filename = "$envPath\$name.$environmentType.environment"
$environment = $null
$schemaVersion = "1"
$securePassword = $null
if([string]::IsNullOrEmpty($password))
{
$psCredential = Get-Credential $username
$securePassword = Encrypt-PasswordFromSecureString $psCredential.Password
$username = $psCredential.UserName
}
else
{
$securePassword = Encrypt-PasswordFromString $password
}
function Validate-Environment($toValidate)
{
#TODO: validate url and sponline username contains @.
}
function Encrypt-Password([SecureString]$password)
{
return Encrypt-PasswordFromSecureString $password
}
if(Test-Path $filename)
{
$jsonData = get-content $filename
$environment = $jsonData | ConvertFrom-Json
$environment.siteUrl = $siteUrl
$environment.username = $username
$environment.encryptedPassword = $securePassword
$environment.name = $name
$environment.credentialType = $credentialType
$environment.environmentType = $environmentType
$environment.schemaVersion = $schemaVersion
$environment.csomVersion = $scomVersion
}
else
{
$environment = @{
schemaVersion = $schemaVersion
siteUrl = $siteUrl
username = $username
encryptedPassword = $securePassword
name = $name
credentialType = $credentialType
environmentType = $environmentType
csomVersion = $csomVersion
}
}
Validate-Environment $environment
if(-not [IO.Directory]::Exists($envPath))
{
New-Item -ItemType directory -Path $envPath
}
$environment | ConvertTo-Json | set-content $filename