Skip to content

Commit

Permalink
Remove Microsoft.Copilot package before sysprep. (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
lpleahy authored Oct 16, 2024
1 parent b2c7d67 commit cee2f33
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions packaging/googet/google-compute-engine-sysprep.goospec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"path": "sysprep/sysprep_uninstall.ps1"
},
"releaseNotes": [
"3.22.0 - Remove Microsoft.Copilot package before sysprep.",
"3.21.0 - Updating activate_instance.ps1.",
"3.20.0 - Remove MTLS MDS certificates from certificate store and disk during sysprep.",
"3.19.0 - Migrating MTU & Firewall rule modifications to use PowerShell cmdlets instead of netsh for Win10/2016 and above.",
Expand Down
39 changes: 34 additions & 5 deletions sysprep/sysprep.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ $script:instance_setup_script_loc = "$gce_install_dir\sysprep\instance_setup.ps1
$script:sysprep_tag = 'C:\Windows\System32\Sysprep\Sysprep_succeeded.tag'
$script:setupscripts_dir_loc = "$env:WinDir\Setup\Scripts"
$script:setupcomplete_loc = "$script:setupscripts_dir_loc\SetupComplete.cmd"
$script:sysprep_output_file_loc = "C:\Windows\System32\Sysprep\Panther\setupact.log"

# Check if the help parameter was called.
if ($help) {
Expand Down Expand Up @@ -140,6 +141,19 @@ function Clear-TempFolders {
}
}

function Clear-Packages {
<#
.SYNOPSIS
Remove all unwanted packages.
.DESCRIPTION
This function defines an array which contains all packages that need to be removed.
We use Get-AppxPackage with Remove-AppxPackage to remove each package in the array.
#>
@("Microsoft.Copilot*") | ForEach-Object {
Get-AppxPackage $_ | Remove-AppxPackage -ErrorAction Ignore
}
}

function Test-Admin {
<#
.SYNOPSIS
Expand Down Expand Up @@ -200,6 +214,7 @@ try {
Invoke-ExternalCommand schtasks /change /tn GCEStartup /disable -ErrorAction SilentlyContinue

# Do some clean up.
Clear-Packages
Clear-TempFolders
Clear-EventLogs

Expand All @@ -208,12 +223,26 @@ try {
Remove-Item $script:sysprep_tag
}

# Run sysprep.
# Run sysprep
Invoke-ExternalCommand C:\Windows\System32\Sysprep\sysprep.exe /generalize /oobe /quit /unattend:$ans_file

Write-Log 'Waiting for sysprep to complete.'
while (-not (Test-Path $script:sysprep_tag)) {
Start-Sleep -Seconds 15
$sysprepExitCode = $LASTEXITCODE
if ($sysprepExitCode -ne 0) {
Write-Log "Sysprep ExitCode:"
Write-Log $sysprepexitCode

# Capture the last lines of the log for debugging.
$lastLines = Get-Content $script:sysprep_output_file_loc -Tail 15
Write-Log "Last 15 lines from setupact.log:"
$lastLines | ForEach-Object { Write-Log $_ }

# Sysprep failed or timed out.
Write-LogError
exit 1
} else {
Write-Log 'Waiting for sysprep to complete.'
while (-not (Test-Path $script:sysprep_tag)) {
Start-Sleep -Seconds 15
}
}

Write-Log 'Stopping GCEAgent.'
Expand Down

0 comments on commit cee2f33

Please sign in to comment.