robarelli robarelli - 3 months ago 18
PowerShell Question

GCE Windows Instance not running startup scripts

I have been trying to apply my startup scripts to new Windows instances on Google Compute Engine as described here, however when I check the instances there is no trace of them ever being executed. Here is the gcloud command I am running:

gcloud compute instances create "my-instance"
--project "my-project"

--zone "us-central1-a"

--machine-type "g1-small"

--network "default"

--metadata "gce-initial-windows-user=my-user" "gce-initial-windows-password=my-pass"

--maintenance-policy "MIGRATE"

--scopes "storage-ro"
--tags "http-server" "https-server"

--image "https://www.googleapis.com/compute/v1/projects/windows-cloud/global/images/windows-server-2008-r2-dc-v20150110"

--boot-disk-type "pd-standard"

--boot-disk-device-name "my-instance"

--metadata-from-file sysprep-oobe-script-ps1=D:\Path\To\startup.ps1

I tried using all 3 startup types (sysprep-specialize-script-ps1, sysprep-oobe-script-ps1, windows-startup-script-ps1) but none worked. Can't see any indication in the Task Scheduler or Event Viewer either. The file on my system exists and does work when I run it manually. How can I get this working?

Answer

A good way to debug Powershell scripts is to have them write to the serial console (COM1). You'll be able to see the output of the script from GCE's serial port output.

gcloud compute instances get-serial-port-output my-instance --zone us-central1-a

If there's no script you'll see something like:

Calling oobe-script from metadata.
attributes/sysprep-oobe-script-bat value is not set or metadata server is not reachable.
attributes/sysprep-oobe-script-cmd value is not set or metadata server is not reachable.
attributes/sysprep-oobe-script-ps1 value is not set or metadata server is not reachable.
Running schtasks with arguments /run /tn GCEStartup
-->  SUCCESS: Attempted to run the scheduled task "GCEStartup".
-------------------------------------------------------------
Instance setup finished. windows is ready to use.
-------------------------------------------------------------
Booting on date 01/25/2015 06:26:26
attributes/windows-startup-script-bat value is not set or metadata server is not reachable.
attributes/windows-startup-script-cmd value is not set or metadata server is not reachable.
attributes/windows-startup-script-ps1 value is not set or metadata server is not reachable.

Make sure that contents of the ps1 file is actually attached to the instance.

gcloud compute instances describe my-instance --zone us-central1-a --format json

The JSON dump should contain the powershell script within it.

Lastly, a great way to debug Powershell startup scripts is to write the output to the serial console.

You can print log messages and see them in the Google Developer Console > Compute > Compute Engine > VM Instances > (Instance Name). Then scroll to the bottom and click the expand option for "Serial console".

Function Write-SerialPort ([string] $message) {
    $port = new-Object System.IO.Ports.SerialPort COM1,9600,None,8,one
    $port.open()
    $port.WriteLine($message)
    $port.Close()
}

Write-SerialPort ("Testing GCE Startup Script")

This command worked for me, I had to make sure that the script was written in ascii. Powershell ISE writes with a different encoding that breaks gcloud compute.

gcloud compute instances create testwin2 --zone us-central1-a --metadata-from-file sysprep-oobe-script-ps1=testconsole.ps1 --image windows-2008-r2