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-30 09:57:24 +03:00
function Download-File([string]$sourceUrl , [ string ] $destinationFile )
{
AddToStatus " Downloading $destinationFile "
Remove-Item -Path $destinationFile -Force -ErrorAction Ignore
[ Net.ServicePointManager ] :: SecurityProtocol = [ Net.ServicePointManager ] :: SecurityProtocol -bor [ Net.SecurityProtocolType ] :: Tls12
( New-Object System . Net . WebClient ) . DownloadFile ( $sourceUrl , $destinationFile )
}
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-29 08:05:44 +03:00
if ( $nchBranch -eq " preview " ) {
2020-07-28 22:15:22 +03:00
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-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
2023-05-07 08:09:21 +03:00
if ( -not ( Get-InstalledModule " Microsoft.Graph " -ErrorAction SilentlyContinue ) ) {
AddToStatus " Installing Microsoft.Graph module "
Install-Module " Microsoft.Graph " -Force
}
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
}