Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Not able to perform 'shutdown without sysprep' using powershell in windows server 2022

Hi Everyone,

I am in the process of creating GCE Instance Templates for Windows Server 2022. After configuring the machine, I need to perform a 'shutdown without sysprep' using PowerShell . I'm able to  perform this action using the wizard, but I'm having trouble with the PowerShell command.

Any help or assistance in this regard would be highly appreciated.

Thanks!

3 2 291
2 REPLIES 2

Hi Everyone,

I have manually run the following PowerShell script to prepare the machine without using Sysprep, and it works:

# Clean Up Temporary Files
Remove-Item -Path "C:\Windows\Temp\*" -Recurse -Force
Remove-Item -Path "C:\Windows\Prefetch\*" -Recurse -Force
Remove-Item -Path "C:\Users\*\AppData\Local\Temp\*" -Recurse -Force

# Clear Event Logs
Get-WinEvent -ListLog * | Where-Object { $_.RecordCount -gt 0 } | ForEach-Object { [System.Diagnostics.Eventing.Reader.EventLogSession]::GlobalSession.ClearLog($_.LogName) }

# Remove Self-Signed Certificates
Get-ChildItem -Path "Cert:\LocalMachine\Remote Desktop" | Where-Object { $_.Issuer -eq $_.Subject } | Remove-Item
Get-ChildItem -Path "Cert:\LocalMachine\My" | Where-Object { $_.Issuer -eq $_.Subject } | Remove-Item

# Configure Firewall Rules
New-NetFirewallRule -DisplayName "Windows Remote Management (HTTPS-In)" -Direction Inbound -LocalPort 5986 -Protocol TCP -Action Allow -Profile Any
Set-NetFirewallRule -DisplayGroup "Remote Desktop" -Enabled True

# Enable Remote Desktop and WinRM
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server" -Name "fDenyTSConnections" -Value 0
Enable-PSRemoting -Force

# Disable Google OS Config Agent
Set-Service google_osconfig_agent -StartupType Disabled

# Ensure GCE Scripts are Present
# Assuming the scripts are already present in the correct location

# Prepare SetupComplete.cmd
$setupCompleteCmdPath = "C:\Windows\Setup\Scripts\SetupComplete.cmd"
$setupCompleteCmdContent = '@echo off
powershell.exe -NoProfile -NoLogo -ExecutionPolicy Unrestricted -File "C:\Program Files\Google\Compute Engine\sysprep\instance_setup.ps1"'

if (!(Test-Path -Path $setupCompleteCmdPath)) {
    New-Item -ItemType File -Path $setupCompleteCmdPath -Force
}
Set-Content -Path $setupCompleteCmdPath -Value $setupCompleteCmdContent

# Shut Down the System
shutdown /s /t 00 /f

However, when I try to automate this process using Packer, I am unable to RDP into the machine. Can anyone help me resolve this issue?

Thanks,
Suriya 

Adding comments to enhance reachability and make the ticket more discoverable to other developers.