forked from Sitecore/docker-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set-LicenseEnvironmentVariable.ps1
55 lines (46 loc) · 1.21 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
param(
[Parameter(Mandatory = $true)]
[ValidateScript( { Test-Path $_ -PathType "Leaf" })]
[string]$Path
,
[Parameter(Mandatory = $false)]
[switch]$PersistForCurrentUser
)
$licenseFileStream = [System.IO.File]::OpenRead($Path);
$licenseString = $null
try
{
$memory = [System.IO.MemoryStream]::new()
$gzip = [System.IO.Compression.GZipStream]::new($memory, [System.IO.Compression.CompressionLevel]::Optimal, $false);
$licenseFileStream.CopyTo($gzip);
$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
}
$licenseFileStream = $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, "User")
}