tester81
tester81

Reputation: 595

Google Cloud - gcloud script using startup script stored on Cloud Storage

In GCP cloud I would like to deploy and then configure Windows Server using gcloud startup script. I have PowerShell script (for Windows Server configuration) prepared and tested. I have uploaded this PowerShell script into Cloud Storage. I have prepared gcloud command script for VM deployment (gcloud command script I have saved as a .sh script). I uploaded this .sh script into the Cloud Shell. In .sh script I am using metadata startup script section--metadata=startup-script-url="gs://bucketName/Server-Configuration-Script.ps1. in Server-Configuration-Script.ps1 PowerShell script I have 4 parameters that I have to pass, below srcipt params.

param (
    [Parameter(Mandatory=$true)][string] $paramA,
    [Parameter(Mandatory=$true)][string] $paramB,
    [Parameter(Mandatory=$true)][string] $paramC,
    [Parameter(Mandatory=$true)][string] $paramD
 )

My question is how can I use all four parameters in a startup script section --metadata=startup-script-url="gs://bucketName/Server-Configuration-Script.ps1 in my .sh script?

Upvotes: 0

Views: 748

Answers (1)

guillaume blaquiere
guillaume blaquiere

Reputation: 75745

I recommend you to set, in the metadata of the VM the value of your parameters.

gcloud compute instances add-metadata instance-name \
    --metadata ParamA=4,ParamB=25,ParamC=1,ParamD="Windows Server 2016"

Then, in the startup script, you can get them like this

paramA=$(curl http://metadata.google.internal/computeMetadata/v1/instance/attributes/ParamA -H "Metadata-Flavor: Google")

EDIT 1

After test on a windows machine, I found how to do at startup

Invoke-RestMethod -H @{'Metadata-Flavor'='Google'} -Uri 'http://metadata.google.internal/computeMetadata/v1/instance/attributes/ParamA'

Curl is an alias of Invoke-WebRequest that requires a first initialization, for the current user, of Internet Explorer configuration. When you run the startup script it runs as System account and you didn't configure IE for this user.

I'm not good in powershell. The command works when I redirect the output to a file, like > c:\Windows\Temp\paramA.txt. Adapt the code to a better PS script than I can do!

Upvotes: 2

Related Questions