0
votes

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?

1
Do you know the param before starting the VM?guillaume blaquiere
Yes, but i dont want to use values hardcoded im powershell script.tester81
I personally prefer the Compute Engine custom metadata instead of hardcoding value. That let you more flexibility and the capacity to reuse the same script on other VM without changing the content of the script. Any reason for hardcoded the value? Will you use the same script on other VM with different parameters?guillaume blaquiere
As I mentioned I am not going to use hardcoded values. In my PowerShell code stored in the Cloud Storage bucket I have four params, listed above in the post. in my cloud shell gcloud command I want to use metadata section --metadata=startup-script-url="gs://bucketName/Server-Configuration-Script.ps1 -paramA -paramB -paramC -paramD, but I am nor sure how to create this command, how to pass parama paramA...D.tester81
If you pass a startup script url, you can't provide parameters. That's why you need to provide them elsewhere, in metadata for exampleguillaume blaquiere

1 Answers

2
votes

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!