forked from microsoft/navcontainerhelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InitializeModule.ps1
85 lines (76 loc) · 3.07 KB
/
InitializeModule.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
param(
[switch] $Silent,
[string[]] $bcContainerHelperConfigFile = @(),
[string] $moduleName,
[string[]] $moduleDependencies = @()
)
Set-StrictMode -Version 2.0
$verbosePreference = "SilentlyContinue"
$warningPreference = 'Continue'
$errorActionPreference = 'Stop'
if (!([environment]::Is64BitProcess)) {
throw "ContainerHelper cannot run in Windows PowerShell (x86), need 64bit mode"
}
$isPsCore = $PSVersionTable.PSVersion -ge "6.0.0"
if ($isPsCore) {
$byteEncodingParam = @{ "asByteStream" = $true }
$allowUnencryptedAuthenticationParam = @{ "allowUnencryptedAuthentication" = $true }
}
else {
$byteEncodingParam = @{ "Encoding" = "byte" }
$allowUnencryptedAuthenticationParam = @{ }
$isWindows = $true
$isLinux = $false
$IsMacOS = $false
}
$myUsername = (whoami)
$isInsideContainer = ($myUsername -eq "user manager\containeradministrator")
if ($isWindows) {
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdministrator = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}
else {
$isAdministrator = ((id -u) -eq 0)
}
$BcContainerHelperVersion = Get-Content (Join-Path $PSScriptRoot "Version.txt")
if (!$silent) {
Write-Host "$($moduleName.SubString(0,$moduleName.Length-5)) version $BcContainerHelperVersion"
}
$isInsider = $BcContainerHelperVersion -like "*-dev" -or $BcContainerHelperVersion -like "*-preview*"
$dotNetRuntimeVersionInstalled = [System.Version]::new(0,0,0)
if ($isWindows) {
$dotNetSharedFolder = 'C:\Program Files\dotnet\shared'
if (Test-Path $dotNetSharedFolder) {
$netCoreAppFolder = Join-Path $dotNetSharedFolder 'Microsoft.NETCore.App'
if (Test-Path $netCoreAppFolder) {
$versions = Get-ChildItem $netCoreAppFolder | ForEach-Object {
try {
if (Test-Path (Join-Path $dotNetSharedFolder "Microsoft.AspNetCore.App/$($_.Name)")) {
[System.Version]$_.Name
}
}
catch {
}
}
$dotNetRuntimeVersionInstalled = $versions | Sort-Object -Descending | Select-Object -First 1
}
}
}
$depVersion = "0.0"
if (!$isInsider) {
$depVersion = $BcContainerHelperVersion.split('-')[0]
}
$moduleDependencies | ForEach-Object {
$module = Get-Module $_
if ($module -ne $null -and $module.Version -lt [Version]$depVersion) {
Write-Error "Module $_ is already loaded in version $($module.Version). This module requires version $depVersion. Please reinstall BC modules."
}
elseif ($module -eq $null) {
if ($isInsider) {
Import-Module (Join-Path $PSScriptRoot "$_.psm1") -DisableNameChecking -Global -ArgumentList $silent,$bcContainerHelperConfigFile
}
else {
Import-Module $_ -MinimumVersion $depVersion -DisableNameChecking -Global -ArgumentList $silent,$bcContainerHelperConfigFile
}
}
}