2020-06-11 13:57:10 +03:00
function AddToStatus ( [ string ] $line , [ string ] $color = " Gray " ) {
2020-06-11 16:02:55 +03:00
( " <font color= "" $color "" > " + [ DateTime ] :: Now . ToString ( [ System.Globalization.DateTimeFormatInfo ] :: CurrentInfo . ShortTimePattern . replace ( " :mm " , " :mm:ss " ) ) + " $line </font> " ) | Add-Content -Path " c:\demo\status.txt " -Force -ErrorAction SilentlyContinue
2017-10-11 04:55:05 +03:00
}
2020-07-28 19:33:26 +03:00
function Register-NativeMethod([string]$dll , [ string ] $methodSignature )
{
$script:nativeMethods + = [ PSCustomObject ] @ { Dll = $dll ; Signature = $methodSignature ; }
}
function Add-NativeMethods ( )
{
$nativeMethodsCode = $script:nativeMethods | % { "
[ DllImport ( `" $ ( $_ . Dll ) `" ) ]
public static extern $ ( $_ . Signature ) ;
" }
Add-Type @"
using System ;
using System . Text ;
using System . Runtime . InteropServices ;
public class NativeMethods {
$nativeMethodsCode
}
" @
}
2020-02-19 17:16:52 +03:00
2020-06-11 13:57:10 +03:00
AddToStatus " SetupStart, User: $env:USERNAME "
2018-06-12 12:54:03 +03:00
2017-10-11 04:55:05 +03:00
. ( Join-Path $PSScriptRoot " settings.ps1 " )
2020-07-28 19:33:26 +03:00
$ComputerInfo = Get-ComputerInfo
$WindowsInstallationType = $ComputerInfo . WindowsInstallationType
$WindowsProductName = $ComputerInfo . WindowsProductName
2020-02-19 11:59:15 +03:00
2020-07-28 22:15:22 +03:00
if ( $nchBranch -eq " dev " ) {
AddToStatus " Installing Latest BcContainerHelper preview from PowerShell Gallery "
Install-Module -Name bccontainerhelper -Force -AllowPrerelease
Import-Module -Name bccontainerhelper -DisableNameChecking
AddToStatus ( " Using BcContainerHelper version " + ( get-module BcContainerHelper ) . Version . ToString ( ) )
}
elseif ( $nchBranch -eq " " ) {
AddToStatus " Installing Latest Business Central Container Helper from PowerShell Gallery "
Install-Module -Name bccontainerhelper -Force
Import-Module -Name bccontainerhelper -DisableNameChecking
AddToStatus ( " Using BcContainerHelper version " + ( get-module BcContainerHelper ) . Version . ToString ( ) )
2020-07-28 19:33:26 +03:00
} else {
2020-07-28 22:15:22 +03:00
if ( $nchBranch -notlike " https://* " ) {
$nchBranch = " https://github.com/Microsoft/navcontainerhelper/archive/ $( $nchBranch ) .zip "
}
AddToStatus " Using BcContainerHelper from $nchBranch "
Download-File -sourceUrl $nchBranch -destinationFile " c:\demo\bccontainerhelper.zip "
[ Reflection.Assembly ] :: LoadWithPartialName ( " System.IO.Compression.Filesystem " ) | Out-Null
[ System.IO.Compression.ZipFile ] :: ExtractToDirectory ( " c:\demo\bccontainerhelper.zip " , " c:\demo " )
$module = Get-Item -Path " C:\demo\*\BcContainerHelper.psm1 "
AddToStatus " Loading BcContainerHelper from $( $module . FullName ) "
Import-Module $module . FullName -DisableNameChecking
2020-07-28 19:33:26 +03:00
}
2019-10-12 10:30:07 +03:00
2020-07-28 22:15:22 +03:00
if ( $AddTraefik -eq " Yes " ) {
if ( $certificatePfxUrl -ne " " -and $certificatePfxPassword -ne " " ) {
AddToStatus -color Red " Certificate specified, cannot add Traefik "
$AddTraefik = " No "
}
if ( -not $ContactEMailForLetsEncrypt ) {
AddToStatus -color Red " Contact EMail for LetsEncrypt not specified, cannot add Traefik "
$AddTraefik = " No "
}
if ( $clickonce -eq " Yes " ) {
AddToStatus -color Red " ClickOnce specified, cannot add Traefik "
$AddTraefik = " No "
}
if ( $AddTraefik -eq " Yes " ) {
Setup-TraefikContainerForNavContainers -overrideDefaultBinding -PublicDnsName $publicDnsName -ContactEMailForLetsEncrypt $ContactEMailForLetsEncrypt
}
else {
Get-VariableDeclaration -name " AddTraefik " | Add-Content $settingsScript
}
}
#if (Test-Path -Path "C:\demo\bccontainerhelper-dev\BcContainerHelper.psm1") {
# Import-module "C:\demo\bccontainerhelper-dev\BcContainerHelper.psm1" -DisableNameChecking
#} else {
# Import-Module -name bccontainerhelper -DisableNameChecking
#}
2019-10-12 10:18:38 +03:00
if ( " $ContactEMailForLetsEncrypt " -ne " " -and $AddTraefik -ne " Yes " ) {
2020-02-19 15:04:10 +03:00
if ( -not ( Get-InstalledModule ACME-PS -ErrorAction SilentlyContinue ) ) {
2019-10-12 10:18:38 +03:00
2020-07-28 19:33:26 +03:00
AddToStatus " Installing ACME-PS PowerShell Module "
Install-Module -Name ACME-PS -RequiredVersion " 1.1.0-beta " -AllowPrerelease -Force
2019-10-12 10:18:38 +03:00
2020-06-11 13:57:10 +03:00
AddToStatus " Using Lets Encrypt certificate "
2019-10-12 10:18:38 +03:00
# Use Lets encrypt
# If rate limits are hit, log an error and revert to Self Signed
try {
$plainPfxPassword = [ GUID ] :: NewGuid ( ) . ToString ( )
2020-07-28 19:33:26 +03:00
$certificatePfxFilename = " c:\ProgramData\bccontainerhelper\certificate.pfx "
2019-10-12 10:18:38 +03:00
New-LetsEncryptCertificate -ContactEMailForLetsEncrypt $ContactEMailForLetsEncrypt -publicDnsName $publicDnsName -CertificatePfxFilename $certificatePfxFilename -CertificatePfxPassword ( ConvertTo-SecureString -String $plainPfxPassword -AsPlainText -Force )
# Override SetupCertificate.ps1 in container
2019-10-24 17:04:08 +03:00
( ' if ( [ int ] ( get-item " C:\Program Files\Microsoft Dynamics NAV\* " ) . Name -le 100 ) {
Write-Host " WARNING: This version doesn''t support LetsEncrypt certificates, reverting to self-signed "
. " C:\run\SetupCertificate.ps1 "
2019-10-12 10:18:38 +03:00
}
2019-10-24 17:04:08 +03:00
else {
2019-10-24 19:12:18 +03:00
. ( Join-Path $PSScriptRoot " InstallCertificate.ps1 " )
2019-10-12 10:18:38 +03:00
}
' ) | Set-Content " c:\myfolder\SetupCertificate.ps1 "
2019-10-24 19:12:18 +03:00
( '$CertificatePfxPassword = ConvertTo-SecureString -String "' + $plainPfxPassword + ' " -AsPlainText -Force
$certificatePfxFile = " '+ $certificatePfxFilename +' "
$cert = New-Object System . Security . Cryptography . X509Certificates . X509Certificate2 ( $certificatePfxFile , $certificatePfxPassword )
$certificateThumbprint = $cert . Thumbprint
Write-Host " Certificate File Thumbprint $certificateThumbprint "
if ( ! ( Get-Item Cert : \ LocalMachine \ my \ $certificateThumbprint -ErrorAction SilentlyContinue ) ) {
Write-Host " Import Certificate to LocalMachine\my "
Import-PfxCertificate -FilePath $certificatePfxFile -CertStoreLocation cert : \ localMachine \ my -Password $certificatePfxPassword | Out-Null
}
$dnsidentity = $cert . GetNameInfo ( " SimpleName " , $false )
if ( $dnsidentity . StartsWith ( " * " ) ) {
$dnsidentity = $dnsidentity . Substring ( $dnsidentity . IndexOf ( " . " ) +1 )
}
' ) | Set-Content " c:\myfolder\InstallCertificate.ps1 "
2019-10-12 10:18:38 +03:00
# Create RenewCertificate script
( '$CertificatePfxPassword = ConvertTo-SecureString -String "' + $plainPfxPassword + ' " -AsPlainText -Force
$certificatePfxFile = " '+ $certificatePfxFilename +' "
$publicDnsName = " '+ $publicDnsName +' "
Renew-LetsEncryptCertificate -publicDnsName $publicDnsName -certificatePfxFilename $certificatePfxFile -certificatePfxPassword $certificatePfxPassword
2019-10-12 19:37:44 +03:00
Start-Sleep -seconds 30
2019-10-12 10:18:38 +03:00
Restart-NavContainer -containerName navserver -renewBindings
' ) | Set-Content " c:\demo\RenewCertificate.ps1 "
} catch {
2020-06-11 13:57:10 +03:00
AddToStatus -color Red $_ . Exception . Message
AddToStatus -color Red " Reverting to Self Signed Certificate "
2019-10-12 10:18:38 +03:00
}
2020-02-19 11:59:15 +03:00
}
2019-10-12 10:18:38 +03:00
}
2020-02-19 15:04:10 +03:00
if ( -not ( Get-InstalledModule Az -ErrorAction SilentlyContinue ) ) {
2020-06-11 13:57:10 +03:00
AddToStatus " Installing Az module "
2020-02-19 11:59:15 +03:00
Install-Module Az -Force
}
2018-06-22 15:52:53 +03:00
2020-02-19 15:04:10 +03:00
if ( -not ( Get-InstalledModule AzureAD -ErrorAction SilentlyContinue ) ) {
2020-06-11 13:57:10 +03:00
AddToStatus " Installing AzureAD module "
2020-02-19 11:59:15 +03:00
Install-Module AzureAD -Force
}
2019-10-17 17:45:17 +03:00
2020-02-19 15:04:10 +03:00
if ( -not ( Get-InstalledModule SqlServer -ErrorAction SilentlyContinue ) ) {
2020-06-11 13:57:10 +03:00
AddToStatus " Installing SqlServer module "
2020-02-19 11:59:15 +03:00
Install-Module SqlServer -Force
}
2019-10-30 12:33:47 +03:00
2018-06-09 15:23:25 +03:00
$securePassword = ConvertTo-SecureString -String $adminPassword -Key $passwordKey
$plainPassword = [ System.Runtime.InteropServices.Marshal ] :: PtrToStringAuto ( [ System.Runtime.InteropServices.Marshal ] :: SecureStringToBSTR ( $SecurePassword ) )
2018-11-07 07:49:44 +03:00
if ( $requestToken ) {
if ( ! ( Get-ScheduledTask -TaskName request -ErrorAction Ignore ) ) {
2020-06-11 13:57:10 +03:00
AddToStatus " Registering request task "
2018-11-07 07:49:44 +03:00
$xml = [ System.IO.File ] :: ReadAllText ( " c:\demo\RequestTaskDef.xml " )
Register-ScheduledTask -TaskName request -User $vmadminUsername -Password $plainPassword -Xml $xml
}
2018-11-06 14:00:11 +03:00
}
2018-12-17 02:47:14 +03:00
if ( " $createStorageQueue " -eq " yes " ) {
2020-02-19 15:04:10 +03:00
if ( -not ( Get-InstalledModule AzTable -ErrorAction SilentlyContinue ) ) {
2020-06-11 13:57:10 +03:00
AddToStatus " Installing AzTable Module "
2020-02-19 15:04:10 +03:00
Install-Module AzTable -Force
2019-10-17 10:43:32 +03:00
2020-07-28 19:33:26 +03:00
$taskName = " RunQueue "
$startupAction = New-ScheduledTaskAction -Execute " powershell.exe " -Argument " -NoProfile -WindowStyle Hidden -ExecutionPolicy UnRestricted -File c:\demo\RunQueue.ps1 "
$startupTrigger = New-ScheduledTaskTrigger -AtStartup
$startupTrigger . Delay = " PT5M "
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd
$task = Register-ScheduledTask -TaskName $taskName `
-Action $startupAction `
-Trigger $startupTrigger `
-Settings $settings `
-RunLevel Highest `
-User $vmAdminUsername `
-Password $plainPassword
$task . Triggers . Repetition . Interval = " PT5M "
$task | Set-ScheduledTask -User $vmAdminUsername -Password $plainPassword | Out-Null
Start-ScheduledTask -TaskName $taskName
}
2018-12-17 02:47:14 +03:00
}
2019-08-27 17:47:34 +03:00
$taskName = " RestartContainers "
2020-02-19 21:08:50 +03:00
if ( -not ( Get-ScheduledTask -TaskName $taskName -ErrorAction Ignore ) ) {
2020-06-11 13:57:10 +03:00
AddToStatus " Register RestartContainers Task to start container delayed "
2020-02-19 21:08:50 +03:00
$startupAction = New-ScheduledTaskAction -Execute " powershell.exe " -Argument " -NoProfile -WindowStyle Hidden -ExecutionPolicy UnRestricted -file c:\demo\restartcontainers.ps1 "
$startupTrigger = New-ScheduledTaskTrigger -AtStartup
$startupTrigger . Delay = " PT5M "
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd
$task = Register-ScheduledTask -TaskName $taskName `
-Action $startupAction `
-Trigger $startupTrigger `
-Settings $settings `
-RunLevel Highest `
-User $vmadminUsername `
-Password $plainPassword
}
2020-02-20 09:23:54 +03:00
2020-07-28 19:33:26 +03:00
if ( $WindowsInstallationType -eq " Server " ) {
if ( Get-ScheduledTask -TaskName SetupVm -ErrorAction Ignore ) {
schtasks / DELETE / TN SetupVm / F | Out-Null
}
2020-02-20 09:23:54 +03:00
2020-06-11 13:57:10 +03:00
AddToStatus " Launch SetupVm "
2020-02-20 09:23:54 +03:00
$onceAction = New-ScheduledTaskAction -Execute " powershell.exe " -Argument " -NoProfile -WindowStyle Hidden -ExecutionPolicy UnRestricted -File c:\demo\setupVm.ps1 "
Register-ScheduledTask -TaskName SetupVm `
-Action $onceAction `
-RunLevel Highest `
-User $vmAdminUsername `
-Password $plainPassword | Out-Null
Start-ScheduledTask -TaskName SetupVm
2020-07-28 19:33:26 +03:00
}
else {
if ( Get-ScheduledTask -TaskName SetupStart -ErrorAction Ignore ) {
schtasks / DELETE / TN SetupStart / F | Out-Null
}
2020-02-20 09:23:54 +03:00
$startupAction = New-ScheduledTaskAction -Execute " powershell.exe " -Argument " -NoProfile -WindowStyle Hidden -ExecutionPolicy UnRestricted -File c:\demo\SetupVm.ps1 "
$startupTrigger = New-ScheduledTaskTrigger -AtStartup
$startupTrigger . Delay = " PT1M "
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd -WakeToRun
Register-ScheduledTask -TaskName " SetupVm " `
-Action $startupAction `
-Trigger $startupTrigger `
-Settings $settings `
-RunLevel " Highest " `
-User $vmAdminUsername `
-Password $plainPassword | Out-Null
2020-06-11 13:57:10 +03:00
AddToStatus -color Yellow " Restarting computer. After restart, please Login to computer using RDP in order to resume the installation process. This is not needed for Windows Server. "
2020-02-20 14:28:56 +03:00
2020-02-20 09:23:54 +03:00
Shutdown -r -t 60
2020-02-20 14:28:56 +03:00
2020-02-20 09:23:54 +03:00
}