Skip to content

Commit 4f98772

Browse files
authored
[06_Migration_Secure_AI_Ready] Update Bootstrap and ClientVM deployment (#311)
* Add logging for invocation line and bound parameters * Refactor Azure Bootstrap script for improved clarity * Update commandToExecute for PowerShell script
1 parent 4f6484c commit 4f98772

2 files changed

Lines changed: 34 additions & 15 deletions

File tree

03-Azure/01-03-Infrastructure/06_Migration_Secure_AI_Ready/resources/artifacts/Bootstrap.ps1

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,28 @@ New-Item -Path $Env:MHBoxDemoPageDir -ItemType directory -Force
8888

8989
Start-Transcript -Path $Env:MHBoxLogsDir\Bootstrap.log
9090

91+
Write-Host "Invocation line:"
92+
Write-Host $MyInvocation.Line
93+
94+
Write-Host "Bound parameters:"
95+
$PSBoundParameters.GetEnumerator() | Sort-Object Name | ForEach-Object {
96+
Write-Host "$($_.Key) = $($_.Value)"
97+
}
98+
99+
# Early Az init - before any module install/update in this session
100+
$ErrorActionPreference = 'Stop'
101+
102+
Disable-AzContextAutosave -Scope Process | Out-Null
103+
104+
Import-Module Az.Accounts -Force
105+
Import-Module Az.Resources -Force
106+
Import-Module Az.KeyVault -Force
107+
108+
Connect-AzAccount -Identity -Tenant $tenantId -Subscription $subscriptionId | Out-Null
109+
Set-AzContext -Subscription $subscriptionId | Out-Null
110+
111+
Write-Host "Az context initialized successfully"
112+
91113
# Set SyncForegroundPolicy to 1 to ensure that the scheduled task runs after the client VM joins the domain
92114
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" "SyncForegroundPolicy" 1
93115

@@ -110,8 +132,6 @@ foreach ($module in $modules) {
110132
}
111133

112134
# Add Key Vault Secrets
113-
Connect-AzAccount -Identity
114-
115135
$DeploymentProgressString = "Started bootstrap-script..."
116136

117137
$tags = Get-AzResourceGroup -Name $resourceGroup | Select-Object -ExpandProperty Tags
@@ -336,10 +356,10 @@ if ($flavor -eq "ITPro") {
336356
Register-ScheduledTask -TaskName "MHServersLogonScript" -User $adminUsername -Action $Action -RunLevel "Highest" -Force
337357
}
338358

339-
# Disabling Windows Server Manager Scheduled Task
340-
Get-ScheduledTask -TaskName ServerManager | Disable-ScheduledTask
359+
# Disabling Windows Server Manager Scheduled Task
360+
Get-ScheduledTask -TaskName ServerManager | Disable-ScheduledTask
341361

342-
if ($flavor -eq "ITPro") {
362+
if ($flavor -eq "ITPro") {
343363

344364
Write-Header "Installing Hyper-V"
345365

@@ -349,14 +369,13 @@ if ($flavor -eq "ITPro") {
349369
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -NoRestart
350370
Install-WindowsFeature -Name Hyper-V -IncludeAllSubFeature -IncludeManagementTools -Restart
351371

352-
}
353-
354-
# Clean up Bootstrap.log
355-
Write-Host "Clean up Bootstrap.log"
356-
Stop-Transcript
357-
$logSuppress = Get-Content $Env:MHBoxLogsDir\Bootstrap.log | Where-Object { $_ -notmatch "Host Application: $ScheduledTaskExecutable" }
358-
$logSuppress | Set-Content $Env:MHBoxLogsDir\Bootstrap.log -Force
372+
}
359373

374+
# Clean up Bootstrap.log
375+
Write-Host "Clean up Bootstrap.log"
376+
Stop-Transcript
377+
$logSuppress = Get-Content $Env:MHBoxLogsDir\Bootstrap.log | Where-Object { $_ -notmatch "Host Application: $ScheduledTaskExecutable" }
378+
$logSuppress | Set-Content $Env:MHBoxLogsDir\Bootstrap.log -Force
360379

361380
# Restart computer
362-
Restart-Computer
381+
Restart-Computer

03-Azure/01-03-Infrastructure/06_Migration_Secure_AI_Ready/resources/bicep/clientVm/clientVm.bicep

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ resource vm 'Microsoft.Compute/virtualMachines@2024-07-01' = {
225225
fileUris: [
226226
uri(templateBaseUrl, 'artifacts/Bootstrap.ps1')
227227
]
228-
//commandToExecute: 'powershell.exe -ExecutionPolicy Bypass -File Bootstrap.ps1 -adminUsername ${windowsAdminUsername} -tenantId ${tenantId} -spnAuthority ${spnAuthority} -subscriptionId ${subscription().subscriptionId} -resourceGroup ${resourceGroup().name} -acceptEula ${acceptEula} -registryUsername ${registryUsername} -azureLocation ${location} -templateBaseUrl ${templateBaseUrl} -flavor ${flavor} -githubUser ${githubUser} -githubBranch ${githubBranch} -vmAutologon ${vmAutologon} -rdpPort ${rdpPort} -namingPrefix ${namingPrefix} -debugEnabled ${debugEnabled} -sqlServerEdition ${sqlServerEdition} -autoShutdownEnabled ${autoShutdownEnabled}'
229-
commandToExecute: 'powershell.exe -ExecutionPolicy Bypass -File Bootstrap.ps1 -adminUsername ${windowsAdminUsername} -tenantId ${tenantId} -spnAuthority ${spnAuthority} -subscriptionId ${subscription().subscriptionId} -resourceGroup ${resourceGroup().name} -acceptEula ${acceptEula} -registryUsername ${registryUsername} -azureLocation ${location} -templateBaseUrl ${templateBaseUrl} -flavor ${flavor} -githubUser ${githubUser} -githubBranch ${githubBranch} -vmAutologon ${vmAutologon} -rdpPort ${rdpPort} -namingPrefix "MHBox" -debugEnabled ${debugEnabled} -sqlServerEdition ${sqlServerEdition} -autoShutdownEnabled ${autoShutdownEnabled}'
228+
//commandToExecute: 'powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File Bootstrap.ps1 -adminUsername ${windowsAdminUsername} -tenantId ${tenantId} -spnAuthority ${spnAuthority} -subscriptionId ${subscription().subscriptionId} -resourceGroup ${resourceGroup().name} -acceptEula ${acceptEula} -registryUsername ${registryUsername} -azureLocation ${location} -templateBaseUrl ${templateBaseUrl} -flavor ${flavor} -githubUser ${githubUser} -githubBranch ${githubBranch} -vmAutologon ${vmAutologon} -rdpPort ${rdpPort} -namingPrefix ${namingPrefix} -debugEnabled ${debugEnabled} -sqlServerEdition ${sqlServerEdition} -autoShutdownEnabled ${autoShutdownEnabled}'
229+
commandToExecute: 'powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File Bootstrap.ps1 -adminUsername ${windowsAdminUsername} -tenantId ${tenantId} -spnAuthority ${spnAuthority} -subscriptionId ${subscription().subscriptionId} -resourceGroup ${resourceGroup().name} -acceptEula ${acceptEula} -registryUsername ${registryUsername} -azureLocation ${location} -templateBaseUrl ${templateBaseUrl} -flavor ${flavor} -githubUser ${githubUser} -githubBranch ${githubBranch} -vmAutologon ${vmAutologon} -rdpPort ${rdpPort} -namingPrefix "MHBox" -debugEnabled ${debugEnabled} -sqlServerEdition ${sqlServerEdition} -autoShutdownEnabled ${autoShutdownEnabled}'
230230
}
231231
}
232232
}

0 commit comments

Comments
 (0)