forked from Sitecore/Sitecore.Demo.Platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set-LicenseEnvironmentVariable.ps1
56 lines (47 loc) · 1.27 KB
/
Set-LicenseEnvironmentVariable.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
param(
[Parameter(Mandatory = $true)]
[ValidateScript( { Test-Path $_ -PathType "Leaf" })]
[string]$Path
,
[Parameter(Mandatory = $false)]
[switch]$PersistForCurrentUser
)
$licenseFileBytes = [System.IO.File]::ReadAllBytes($Path)
$licenseString = $null
try
{
$memory = [System.IO.MemoryStream]::new()
# gzip license file content
$gzip = [System.IO.Compression.GZipStream]::new($memory, [System.IO.Compression.CompressionLevel]::Optimal, $true)
$gzip.Write($licenseFileBytes, 0, $licenseFileBytes.Length);
$gzip.Close()
# base64 encode the gzipped content
$licenseString = [System.Convert]::ToBase64String($memory.ToArray())
}
finally
{
# cleanup
if ($null -ne $gzip)
{
$gzip.Dispose()
$gzip = $null
}
if ($null -ne $memory)
{
$memory.Dispose()
$memory = $null
}
$licenseFileBytes = $null
}
# sanity check
if ($licenseString.Length -le 100)
{
throw "Unknown error, the gzipped and base64 encoded string '$licenseString' is too short."
}
# persist in current session
$env:SITECORE_LICENSE = $licenseString
if ($PersistForCurrentUser)
{
# persist on machine for future sessions
[Environment]::SetEnvironmentVariable("SITECORE_LICENSE", $licenseString, "Machine")
}