inogen/deployment/initialize-admin.ps1

53 lines
2.2 KiB
PowerShell

[CmdletBinding()]
param(
[string]$UserName,
[string]$DisplayName = 'مدیر سامانه'
)
$ErrorActionPreference = 'Stop'
$setup = Join-Path $PSScriptRoot 'tools\Inogen.Setup\Inogen.Setup.exe'
if (-not (Test-Path -LiteralPath $setup)) { throw 'The customer setup tool is missing.' }
if ([string]::IsNullOrWhiteSpace($UserName)) {
$bytes = New-Object byte[] 4
$random = [Security.Cryptography.RandomNumberGenerator]::Create()
try { $random.GetBytes($bytes) } finally { $random.Dispose() }
$UserName = 'admin-' + [BitConverter]::ToString($bytes).Replace('-', '').ToLowerInvariant()
}
if ($UserName -notmatch '^[A-Za-z0-9._-]{5,64}$') {
throw 'User name must contain 5-64 Latin letters, digits, dots, underscores or hyphens.'
}
if ([string]::IsNullOrWhiteSpace($env:INOGEN_TARGET_CONNECTION)) {
throw 'Set the private target connection and target allowlist before creating the administrator.'
}
function To-PlainText([Security.SecureString]$secret) {
$pointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secret)
try { [Runtime.InteropServices.Marshal]::PtrToStringBSTR($pointer) }
finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($pointer) }
}
$password = To-PlainText (Read-Host 'Administrator password' -AsSecureString)
$confirmation = To-PlainText (Read-Host 'Confirm administrator password' -AsSecureString)
if ($password -cne $confirmation) {
$password = $null
$confirmation = $null
throw 'Passwords do not match.'
}
try {
$env:INOGEN_BOOTSTRAP_ADMIN_USER = $UserName
$env:INOGEN_BOOTSTRAP_ADMIN_DISPLAY_NAME = $DisplayName
$env:INOGEN_BOOTSTRAP_ADMIN_PASSWORD = $password
& $setup provision-admin
if ($LASTEXITCODE -ne 0) { throw 'Administrator creation failed.' }
Write-Host "Administrator user name for handover: $UserName"
Write-Host 'Deliver the password through a separate secure channel; it is not stored in the package.'
}
finally {
Remove-Item Env:INOGEN_BOOTSTRAP_ADMIN_USER -ErrorAction SilentlyContinue
Remove-Item Env:INOGEN_BOOTSTRAP_ADMIN_DISPLAY_NAME -ErrorAction SilentlyContinue
Remove-Item Env:INOGEN_BOOTSTRAP_ADMIN_PASSWORD -ErrorAction SilentlyContinue
$password = $null
$confirmation = $null
}