開機指令碼是指在虛擬機器 (VM) 執行個體的啟動程序中執行工作時使用的檔案。開機指令碼可套用至專案中的所有 VM,或套用至單一 VM。由 VM 層級中繼資料指定的開機指令碼會覆寫由專案層級中繼資料指定的開機指令碼,且開機指令碼只會在網路可用時執行。本文件說明如何在 Windows Server VM 執行個體上使用啟動指令碼。如要瞭解如何新增專案層級的啟動指令碼,請參閱 gcloud compute project-info add-metadata
。
Windows 啟動指令碼必須是指令碼殼層 (.cmd
)、PowerShell (.ps1
) 或批次檔案指令碼 (.bat
),且必須具有適當的檔案副檔名。
如果您使用本文件中的其中一個程序指定開機指令碼,Compute Engine 會執行以下操作:
將開機指令碼複製到 VM
當 VM 開機時,工作排程器會以
LocalSystem
帳戶的權限執行開機指令碼
如要進一步瞭解與開機指令碼相關的各種工作,以及執行每項工作的時機,請參閱開機指令碼總覽文件。
事前準備
- 請參閱開機指令碼總覽。
- 閱讀VM 中繼資料的基本概念。
-
如果尚未設定,請先設定驗證機制。驗證是指驗證身分,以便存取 Google Cloud 服務和 API 的程序。如要在本機開發環境中執行程式碼或範例,您可以選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
- Set a default region and zone.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
-
Windows 啟動指令碼的中繼資料鍵
開機指令碼會從中繼資料鍵指定的位置傳遞至 VM。中繼資料鍵會指定開機指令碼是儲存在本機、儲存在 Cloud Storage 中,還是直接傳遞至 VM。您使用的中繼資料鍵也可能取決於開機指令碼的大小或檔案類型。
下表列出可用於 Windows 啟動指令碼的中繼資料鍵,並提供有關使用哪個鍵的資訊,這取決於啟動指令碼的儲存位置、大小和檔案類型。
中繼資料鍵 | 用於 |
---|---|
sysprep-specialize-script-ps1 |
傳遞儲存在本機或直接新增的未簽署 PowerShell 指令碼,大小上限為 256 KB |
sysprep-specialize-script-cmd |
傳遞儲存在本機或直接新增的大小不超過 256 KB 的指令殼層指令碼 |
sysprep-specialize-script-bat |
傳遞儲存在本機或直接新增的批次檔案指令碼,且大小不超過 256 KB |
sysprep-specialize-script-url |
傳遞儲存在 Cloud Storage 中且大小超過 256 KB 的批次檔案、指令稿殼層、已簽署/未簽署的 PowerShell 指令碼或可執行檔 |
windows-startup-script-ps1 |
傳遞儲存在本機或直接新增的未簽署 PowerShell 指令碼,大小上限為 256 KB |
windows-startup-script-cmd |
傳遞儲存在本機或直接新增的大小不超過 256 KB 的指令殼層指令碼 |
windows-startup-script-bat |
傳遞儲存在本機或直接新增的批次檔案指令碼,且大小不超過 256 KB |
windows-startup-script-url |
傳遞儲存在 Cloud Storage 中且大小超過 256 KB 的批次檔案、指令稿殼層、已簽署/未簽署的 PowerShell 指令碼或可執行檔 |
如要進一步瞭解各種開機指令碼的執行順序,請參閱 GitHub 上的 GoogleCloudPlatform/compute-image-windows 存放區。
Windows 啟動指令碼的執行順序
您可以使用多個開機指令碼。儲存在本機或直接新增的開機指令碼,會在儲存在 Cloud Storage 的開機指令碼之前執行。包含指令碼的檔案類型也會影響執行順序。下表根據中繼資料鍵,顯示 Windows 開機指令碼的執行順序。
中繼資料鍵 | 執行順序 |
---|---|
sysprep-specialize-script-ps1 |
首先在初始啟動期間 |
sysprep-specialize-script-cmd |
在初始開機期間執行 |
sysprep-specialize-script-bat |
第三種是在初始啟動期間 |
sysprep-specialize-script-url |
第四個:在初始開機期間 |
windows-startup-script-ps1 |
第一次開機後的每次開機 |
windows-startup-script-cmd |
在初始開機後的每次開機期間執行 |
windows-startup-script-bat |
第三次:在初始啟動後的每次啟動 |
windows-startup-script-url |
第四次:在初始開機後的每次開機 |
直接傳遞 Windows 啟動指令碼
將批次檔案、指令殼層或未簽署的 PowerShell 啟動指令碼的內容直接傳送至 Windows Server VM。下列程序說明如何傳遞未簽署的 PowerShell 指令碼。
主控台
直接將 Windows 開機指令碼傳遞至新的 VM
前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。
在「Boot disk」部分,選取「Change」,然後執行下列操作:
- 在「Public images」分頁中,選擇 Windows Server 作業系統。
- 按一下 [選取]。
展開「Advanced options」(進階選項) 區段,然後執行下列操作:
- 展開「管理」部分。
在「中繼資料」專區中,點選「新增項目」來設定「鍵」和「值」:
Key:設為
windows-startup-script-ps1
。使用以
windows-startup-script
開頭的中繼資料鍵,在 VM 初始開機後的每次開機時,將開機指令碼傳遞至 VM。值:新增以下腳本:
# Installing IIS Import-Module servermanager Install-WindowsFeature Web-Server -IncludeAllSubFeature # Ensure the directory exists if (-not (Test-Path("C:\inetpub\wwwroot"))) {New-Item "C:\inetpub\wwwroot" -Type Directory} # Write the expanded string out to the file, overwriting the file if it already exists. "<html><body><p>Windows startup script added directly.</p></body></html>" | Out-File -FilePath C:\inetpub\wwwroot\index.html -Encoding ascii -Force
按一下 [建立]。
將 Windows 開機指令碼直接傳遞至現有 VM
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
按一下 VM 的「名稱」。
按一下 [編輯]。
在「中繼資料」下方指定下列內容:
key
:windows-startup-script-ps1
value
:開機指令碼的內容
驗證開機指令碼
VM 啟動後,請在網路瀏覽器中查看外部 IP,確認開機指令碼是否已建立網站。您可能需要等待約 10 分鐘,等待範例啟動指令碼完成。
gcloud (Bash)
直接將 Windows 啟動指令碼傳遞至新的 VM
使用下列 gcloud compute
instances create
指令,即可在建立 Windows Server VM 時,直接將開機指令碼的內容傳遞至 VM:
gcloud compute instances create VM_NAME \ --image-project=windows-cloud \ --image-family=windows-2019-core \ --metadata=windows-startup-script-ps1='Import-Module servermanager Install-WindowsFeature Web-Server -IncludeAllSubFeature "<html><body><p>Windows startup script added directly.</p></body></html>" > C:\inetpub\wwwroot\index.html'
將 VM_NAME 替換為 VM 名稱。
將 Windows 開機指令碼直接傳遞至現有 VM
使用下列 gcloud compute instances add-metadata
指令,即可直接將開機指令碼新增至現有的 VM:
gcloud compute instances add-metadata VM_NAME \ --image-project=windows-cloud \ --image-family=windows-2019-core \ --metadata=windows-startup-script-ps1='Import-Module servermanager Install-WindowsFeature Web-Server -IncludeAllSubFeature "<html><body><p>Windows startup script added directly.</p></body></html>" > C:\inetpub\wwwroot\index.html'
將 VM_NAME 替換為 VM 名稱。
驗證開機指令碼VM 啟動後,請在網路瀏覽器中查看外部 IP,確認開機指令碼是否已建立網站。您可能需要等待約 10 分鐘,等待範例啟動指令碼完成。
gcloud (命令提示字元)
直接將 Windows 啟動指令碼傳遞至新的 VM
使用下列 gcloud compute
instances create
指令,即可在建立 Windows Server VM 時,直接將開機指令碼的內容傳遞至 VM:
gcloud compute instances create VM_NAME ^ --image-project=windows-cloud ^ --image-family=windows-2019-core ^ --metadata=windows-startup-script-ps1='Import-Module servermanager Install-WindowsFeature Web-Server -IncludeAllSubFeature "<html><body><p>Windows startup script added directly.</p></body></html>" > C:\inetpub\wwwroot\index.html'
將 VM_NAME 替換為 VM 名稱。
將 Windows 開機指令碼直接傳遞至現有 VM
使用下列 gcloud compute instances add-metadata
指令,即可直接將開機指令碼新增至現有的 VM:
gcloud compute instances add-metadata VM_NAME ^ --image-project=windows-cloud ^ --image-family=windows-2019-core ^ --metadata=windows-startup-script-ps1='Import-Module servermanager Install-WindowsFeature Web-Server -IncludeAllSubFeature "<html><body><p>Windows startup script added directly.</p></body></html>" > C:\inetpub\wwwroot\index.html'
將 VM_NAME 替換為 VM 名稱。
驗證開機指令碼VM 啟動後,請在網路瀏覽器中查看外部 IP,確認開機指令碼是否已建立網站。您可能需要等待約 10 分鐘,等待範例啟動指令碼完成。
gcloud (PowerShell)
直接將 Windows 啟動指令碼傳遞至新的 VM
使用下列 gcloud compute
instances create
指令,即可在建立 Windows Server VM 時,直接將開機指令碼的內容傳遞至 VM:
gcloud compute instances create VM_NAME ` --image-project=windows-cloud ` --image-family=windows-2019-core ` --metadata=windows-startup-script-ps1='Import-Module servermanager Install-WindowsFeature Web-Server -IncludeAllSubFeature "<html><body><p>Windows startup script added directly.</p></body></html>" > C:\inetpub\wwwroot\index.html'
將 VM_NAME 替換為 VM 名稱。
將 Windows 開機指令碼直接傳遞至現有 VM
使用下列 gcloud compute instances add-metadata
指令,即可直接將開機指令碼新增至現有的 VM:
gcloud compute instances add-metadata VM_NAME ` --image-project=windows-cloud ` --image-family=windows-2019-core ` --metadata=windows-startup-script-ps1='Import-Module servermanager Install-WindowsFeature Web-Server -IncludeAllSubFeature "<html><body><p>Windows startup script added directly.</p></body></html>" > C:\inetpub\wwwroot\index.html'
將 VM_NAME 替換為 VM 名稱。
驗證開機指令碼VM 啟動後,請在網路瀏覽器中查看外部 IP,確認開機指令碼是否已建立網站。您可能需要等待約 10 分鐘,等待範例啟動指令碼完成。
REST
直接將 Windows 開機指令碼傳遞至新的 VM
使用下列 instances.insert
方法,在建立 Windows Server VM 時,將開機指令碼的內容直接傳遞至 VM。
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances { ... "networkInterfaces": [ { "accessConfigs": [ { "type": "ONE_TO_ONE_NAT" } ] } ], "metadata": { "items": [ { "key": "windows-startup-script-ps1", "value": "Import-Module servermanager\nInstall-WindowsFeature Web-Server -IncludeAllSubFeature\necho '<html><body><p>Windows startup script added directly.</p></body></html>' > C:\\inetpub\\wwwroot\\index.html" } ] }, ... }
更改下列內容:
PROJECT_ID:專案 ID
ZONE:建立新 VM 的可用區
將 Windows 開機指令碼直接傳遞至現有 VM
使用
instances.get
方法取得 VM 的tags.fingerprint
值。GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME
更改下列內容:
PROJECT_ID:專案 ID
ZONE:VM 的可用區
VM_NAME:VM 的可用區
在呼叫
instances.setMetadata
方法時,使用fingerprint
值,以及開機指令碼的中繼資料鍵和值,傳遞開機指令碼:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/setMetadata { "fingerprint": FINGERPRINT, "items": [ { "key": "windows-startup-script-ps1", "value": "Import-Module servermanager\nInstall-WindowsFeature Web-Server -IncludeAllSubFeature\necho '<html><body><p>Windows startup script added directly.</p></body></html>' > C:\\inetpub\\wwwroot\\index.html" } ], ... }
更改下列內容:
PROJECT_ID:專案 ID
ZONE:VM 的可用區
VM_NAME:VM 的可用區
FINGERPRINT:使用
instances.get
方法取得的tags.fingerprint
值
驗證開機指令碼
VM 啟動後,請在網路瀏覽器中查看外部 IP,確認開機指令碼是否已建立網站。您可能需要等待約 10 分鐘,等待範例啟動指令碼完成。
從本機檔案傳遞 Windows 開機指令碼
您可以在工作站上的本機批次檔案、指令殼層指令碼或未簽署的 PowerShell 指令碼中儲存開機指令碼,並在建立 VM 時將本機檔案做為中繼資料傳遞給 VM。您無法將儲存在 VM 中的檔案用作開機指令碼。
將 Windows 啟動指令碼從本機檔案傳遞至 VM 前,請先執行下列操作:
建立本機 (
.ps1
) 檔案來儲存啟動指令碼。請注意 gcloud CLI 與啟動指令碼之間的相對路徑。
在檔案中新增下列開機指令碼:
# Installing IIS Import-Module servermanager Install-WindowsFeature Web-Server -IncludeAllSubFeature # Ensure the directory exists if (-not (Test-Path("C:\inetpub\wwwroot"))) {New-Item "C:\inetpub\wwwroot" -Type Directory} # Write the expanded string out to the file, overwriting the file if it already exists. "<html><body><p>Windows startup script passed from a file on your local workstation.</p></body></html>" | Out-File -FilePath C:\inetpub\wwwroot\index.html -Encoding ascii -Force
gcloud (Bash)
將 Windows 啟動指令碼從本機檔案傳遞至新的 VM
建立 VM,並使用 --metadata-from-file
標記搭配 gcloud compute instances create
指令,傳遞要用於開機指令碼的本機檔案內容:
gcloud compute instances create VM_NAME \ --image-project=windows-cloud \ --image-family=windows-2019-core \ --metadata-from-file=windows-startup-script-ps1=FILE_PATH
請依指示取代下列項目:
- VM_NAME:VM 名稱
- FILE_PATH:開機指令碼檔案的相對路徑
使用下列 gcloud compute instances add-metadata
指令,將開機指令碼從本機檔案傳遞至現有的 VM:
gcloud compute instances add-metadata VM_NAME \ --metadata-from-file=windows-startup-script-ps1=FILE_PATH
請依指示取代下列項目:
- VM_NAME:VM 名稱
- FILE_PATH:開機指令碼檔案的相對路徑
在網路瀏覽器中查看外部 IP,確認開機指令碼是否已建立網站。您可能需要等待約 10 分鐘,等待範例啟動指令碼完成。
gcloud (命令提示字元)
將 Windows 啟動指令碼從本機檔案傳遞至新的 VM
建立 VM,並使用 --metadata-from-file
標記搭配 gcloud compute instances create
指令,傳遞要用於開機指令碼的本機檔案內容:
gcloud compute instances create VM_NAME ^ --image-project=windows-cloud ^ --image-family=windows-2019-core ^ --metadata-from-file=windows-startup-script-ps1=FILE_PATH
請依指示取代下列項目:
- VM_NAME:VM 名稱
- FILE_PATH:開機指令碼檔案的相對路徑
使用下列 gcloud compute instances add-metadata
指令,將開機指令碼從本機檔案傳遞至現有的 VM:
gcloud compute instances add-metadata VM_NAME ^ --metadata-from-file=windows-startup-script-ps1=FILE_PATH
請依指示取代下列項目:
- VM_NAME:VM 名稱
- FILE_PATH:開機指令碼檔案的相對路徑
在網路瀏覽器中查看外部 IP,確認開機指令碼是否已建立網站。您可能需要等待約 10 分鐘,等待範例啟動指令碼完成。
gcloud (PowerShell)
將 Windows 啟動指令碼從本機檔案傳遞至新的 VM
建立 VM,並使用 --metadata-from-file
標記搭配 gcloud compute instances create
指令,傳遞本機檔案的內容,以做為開機指令碼:
gcloud compute instances create VM_NAME ` --image-project=windows-cloud ` --image-family=windows-2019-core ` --metadata-from-file=windows-startup-script-ps1=FILE_PATH
請依指示取代下列項目:
- VM_NAME:VM 名稱
- FILE_PATH:開機指令碼檔案的相對路徑
使用下列 gcloud compute instances add-metadata
指令,將開機指令碼從本機檔案傳遞至現有的 VM:
gcloud compute instances add-metadata VM_NAME ` --metadata-from-file=windows-startup-script-ps1=FILE_PATH
請依指示取代下列項目:
- VM_NAME:VM 名稱
- FILE_PATH:開機指令碼檔案的相對路徑
在網路瀏覽器中查看外部 IP,確認開機指令碼是否已建立網站。您可能需要等待約 10 分鐘,等待範例啟動指令碼完成。
透過 Cloud Storage 傳遞 Windows 開機指令碼
您可以將啟動指令碼儲存為批次檔案、指令殼層指令碼、已簽署/未簽署的 PowerShell 指令碼或可執行檔,並在建立 VM 時將其傳遞給 VM。將開機指令碼新增至 Cloud Storage 後,您將取得一個網址,可用於在建立 VM 時參照開機指令碼。
在從 Cloud Storage 值區新增開機指令碼之前,請先執行下列操作:
建立用於儲存開機指令碼的檔案。本範例使用 PowerShell (
.ps1
) 檔案。將下列 PowerShell 指令碼加入檔案,以便安裝網路伺服器並建立簡單的網頁:
# Installing IIS Import-Module servermanager Install-WindowsFeature Web-Server -IncludeAllSubFeature # Ensure the directory exists if (-not (Test-Path("C:\inetpub\wwwroot"))) {New-Item "C:\inetpub\wwwroot" -Type Directory} # Write the expanded string out to the file, overwriting the file if it already exists. "<html><body><p>Windows startup script passed from Cloud Storage.</p></body></html>" | Out-File -FilePath C:\inetpub\wwwroot\index.html -Encoding ascii -Force
安全性影響
根據預設,專案擁有者和專案編輯者可以存取相同專案中的 Cloud Storage 檔案,除非有明確存取權控管機制禁止。
如果 Cloud Storage 值區或物件的安全性低於中繼資料,在修改啟動指令碼並重新啟動 VM 時,就有可能發生權限提升的風險。這是因為在 VM 重新啟動後,啟動指令碼會以
LocalSystem
的身份執行,然後可使用所附服務帳戶的權限存取其他資源。
主控台
將儲存在 Cloud Storage 中的開機指令碼傳遞至新的 VM
前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。
在「Boot disk」部分,選取「Change」,然後執行下列操作:
- 在「Public images」分頁中,選擇 Windows Server 作業系統。
- 按一下 [選取]。
在「Identity and API access」(身分及 API 存取權) 區段中,選取具有「Storage Object Viewer」角色的服務帳戶。
展開「進階選項」部分,然後執行下列操作:
- 展開「管理」部分。
在「Metadata」專區中,新增下列值:
鍵:中繼資料鍵。指定
windows-startup-script-url
中繼資料鍵,讓指令碼在初始開機後的每次開機時執行。值:中繼資料值。請使用下列其中一種格式,將其設為開機指令碼檔案的 Cloud Storage 位置:
- 已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE
- gcloud 儲存空間 URI:
gs://BUCKET/FILE
更改下列內容:
- BUCKET:包含開機指令碼檔案的 bucket 名稱
- FILE:開機指令碼檔案的名稱
- 已驗證的網址:
如要建立 VM,請按一下「建立」。
將儲存在 Cloud Storage 中的開機指令碼傳遞至現有 VM
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
按一下 VM 的「名稱」。
按一下 [編輯]。
在「中繼資料」下方新增下列值:
- 已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE
- gcloud 儲存空間 URI:
gs://BUCKET/FILE
- 已驗證的網址:
驗證開機指令碼
在網路瀏覽器中查看外部 IP,確認開機指令碼已建立網站。您可能需要等待約 10 分鐘,等待範例啟動指令碼完成。
gcloud (Bash)
將儲存在 Cloud Storage 中的開機指令碼傳遞至新的 VM
使用下列 gcloud compute instances create
指令,將儲存在 Cloud Storage 中的開機指令碼傳遞至 Windows Server VM。如要設定 --scopes
標記的值,請使用 storage-ro
,讓 VM 能夠存取 Cloud Storage。
gcloud compute instances create VM_NAME \ --image-project=windows-cloud \ --image-family=windows-2019-core \ --scopes=storage-ro \ --metadata=windows-startup-script-url=CLOUD_STORAGE_URL
請依指示取代下列項目:
- VM_NAME:VM 名稱。
-
CLOUD_STORAGE_URL:中繼資料值。使用下列其中一種格式,設定為開機指令碼檔案的位置:
-
已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE
-
gcloud 儲存空間 URI:
gs://BUCKET/FILE
-
已驗證的網址:
使用下列 gcloud compute instances add-metadata
指令,將儲存在 Cloud Storage 中的開機指令碼傳遞至現有 VM:
gcloud compute instances add-metadata VM_NAME \ --metadata=windows-startup-script-url=CLOUD_STORAGE_URL
請依指示取代下列項目:
- VM_NAME:VM 名稱。
-
CLOUD_STORAGE_URL:中繼資料值。使用下列其中一種格式,設定為開機指令碼檔案的位置:
-
已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE
-
gcloud 儲存空間 URI:
gs://BUCKET/FILE
-
已驗證的網址:
在網路瀏覽器中查看外部 IP,確認開機指令碼是否已建立網站。您可能需要等待約 10 分鐘,等待範例啟動指令碼完成。
gcloud (命令提示字元)
將儲存在 Cloud Storage 中的開機指令碼傳遞至新的 VM
使用下列 gcloud compute instances create
指令,將儲存在 Cloud Storage 中的開機指令碼傳遞至 Windows Server VM。如要設定 --scopes
標記的值,請使用 storage-ro
,讓 VM 能夠存取 Cloud Storage。
gcloud compute instances create VM_NAME ^ --image-project=windows-cloud ^ --image-family=windows-2019-core ^ --scopes=storage-ro ^ --metadata=windows-startup-script-url=CLOUD_STORAGE_URL
請依指示取代下列項目:
- VM_NAME:VM 名稱。
-
CLOUD_STORAGE_URL:中繼資料值。使用下列其中一種格式,設定為開機指令碼檔案的位置:
-
已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE
-
gcloud 儲存空間 URI:
gs://BUCKET/FILE
-
已驗證的網址:
使用下列 gcloud compute instances add-metadata
指令,將儲存在 Cloud Storage 中的開機指令碼傳遞至現有 VM:
gcloud compute instances add-metadata VM_NAME ^ --metadata=windows-startup-script-url=CLOUD_STORAGE_URL
請依指示取代下列項目:
- VM_NAME:VM 名稱。
-
CLOUD_STORAGE_URL:中繼資料值。使用下列其中一種格式,設定為開機指令碼檔案的位置:
-
已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE
-
gcloud 儲存空間 URI:
gs://BUCKET/FILE
-
已驗證的網址:
在網路瀏覽器中查看外部 IP,確認開機指令碼是否已建立網站。您可能需要等待約 10 分鐘,等待範例啟動指令碼完成。
gcloud (PowerShell)
將儲存在 Cloud Storage 中的開機指令碼傳遞至新的 VM
使用下列 gcloud compute instances create
指令,將儲存在 Cloud Storage 中的開機指令碼傳遞至 Windows Server VM。如要設定 --scopes
標記的值,請使用 storage-ro
,讓 VM 能夠存取 Cloud Storage。
gcloud compute instances create VM_NAME ` --image-project=windows-cloud ` --image-family=windows-2019-core ` --scopes=storage-ro ` --metadata=windows-startup-script-url=CLOUD_STORAGE_URL
請依指示取代下列項目:
- VM_NAME:VM 名稱。
-
CLOUD_STORAGE_URL:中繼資料值。使用下列其中一種格式,設定為開機指令碼檔案的位置:
-
已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE
-
gcloud 儲存空間 URI:
gs://BUCKET/FILE
-
已驗證的網址:
使用下列 gcloud compute instances add-metadata
指令,將儲存在 Cloud Storage 中的開機指令碼傳遞至現有 VM:
gcloud compute instances add-metadata VM_NAME ` --metadata=windows-startup-script-url=CLOUD_STORAGE_URL
請依指示取代下列項目:
- VM_NAME:VM 名稱。
-
CLOUD_STORAGE_URL:中繼資料值。使用下列其中一種格式,設定為開機指令碼檔案的位置:
-
已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE
-
gcloud 儲存空間 URI:
gs://BUCKET/FILE
-
已驗證的網址:
在網路瀏覽器中查看外部 IP,確認開機指令碼是否已建立網站。您可能需要等待約 10 分鐘,等待範例啟動指令碼完成。
REST
將儲存在 Cloud Storage 中的開機指令碼傳遞至新的 VM
使用下列 instances.insert
方法,將儲存在 Cloud Storage 的開機指令碼傳遞至 Windows Server VM。在 scopes
欄位中新增 https://www.googleapis.com/auth/devstorage.read_only
,讓 VM 能夠存取 Cloud Storage。
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances { ... "networkInterfaces": [ { "accessConfigs": [ { "type": "ONE_TO_ONE_NAT" } ] } ], "serviceAccounts": [ { "email": "default", "scopes": [ "https://www.googleapis.com/auth/devstorage.read_only" ] } ], "metadata": { "items": [ { "key": "windows-startup-script-url", "value": "CLOUD_STORAGE_URL" }, ... ] }, ... }
更改下列內容:
PROJECT_ID:專案 ID。
ZONE:建立新 VM 的可用區。
CLOUD_STORAGE_URL:中繼資料值。使用下列格式,將其設為開機指令碼檔案的 Cloud Storage 位置:
- 已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE
- gcloud 儲存空間 URI:
gs://BUCKET/FILE
- 已驗證的網址:
將儲存在 Cloud Storage 中的開機指令碼傳遞至現有 VM
使用
instances.get
方法取得 VM 的tags.fingerprint
值。GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME
更改下列內容:
PROJECT_ID:專案 ID
ZONE:VM 的可用區
VM_NAME:VM 的可用區
在呼叫
instances.setMetadata
方法時,使用fingerprint
值,以及開機指令碼的中繼資料鍵和值,傳遞開機指令碼:POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME/setMetadata { "fingerprint": FINGERPRINT, "items": [ { "key": "windows-startup-script-url", "value": "CLOUD_STORAGE_URL" } ], ... }
更改下列內容:
PROJECT_ID:專案 ID。
ZONE:VM 的可用區。
VM_NAME:VM 的可用區。
FINGERPRINT:使用
instances.get
方法取得的tags.fingerprint
值。CLOUD_STORAGE_URL:中繼資料值。使用下列格式,將其設為開機指令碼檔案的 Cloud Storage 位置:
- 已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE
- gcloud 儲存空間 URI:
gs://BUCKET/FILE
- 已驗證的網址:
驗證開機指令碼
在網路瀏覽器中查看外部 IP,確認開機指令碼已建立網站。您可能需要等待約 10 分鐘,等待範例啟動指令碼完成。
透過 Windows 啟動指令碼存取中繼資料
您可以在開機指令碼中存取中繼資料值。舉例來說,您可以為多個 VM 使用相同的指令碼,並將不同的中繼資料值傳遞至每個 VM,以便個別設定每個指令碼的參數。
如要透過開機指令碼存取自訂中繼資料值,請按照下列步驟操作:
建立開機指令碼,查詢中繼資料鍵的值。舉例來說,下列 PowerShell (
.ps1
) 開機指令碼會查詢foo
中繼資料鍵的值。$METADATA_VALUE = (Invoke-RestMethod -Headers @{'Metadata-Flavor' = 'Google'} -Uri "http://metadata.google.internal/computeMetadata/v1/instance/attributes/foo") # Installing IIS Import-Module servermanager Install-WindowsFeature Web-Server -IncludeAllSubFeature # Ensure the directory exists if (-not (Test-Path("C:\inetpub\wwwroot"))) {New-Item "C:\inetpub\wwwroot" -Type Directory} # Write the expanded string out to the file, overwriting the file if it already exists. "<html><body><p>Accessing metadata value of foo: $METADATA_VALUE</p></body></html>" | Out-File -FilePath C:\inetpub\wwwroot\index.html -Encoding ascii -Force
建立 VM 時,請使用下列
gcloud compute instances create
指令設定foo
中繼資料鍵的值。在本例中,開機指令碼會從本機檔案傳遞至 VM。gcloud
gcloud compute instances create VM_NAME \ --image-project=windows-cloud \ --image-family=windows-2019 \ --metadata-from-file=windows-startup-script-ps1=FILE_PATH \ --metadata=foo=bar
更改下列內容:
VM_NAME:VM 名稱
FILE_PATH:啟動指令碼檔案的相對路徑
如要進一步瞭解如何指定中繼資料鍵值組合,請參閱「設定及移除自訂中繼資料」。
在網路瀏覽器中查看外部 IP,確認開機指令碼是否會輸出
foo
的值。您可能需要等待約 10 分鐘,讓範例開機指令碼完成。
重新執行 Windows 啟動指令碼
如要在 Windows VM 上重新執行開機指令碼,請按照下列步驟操作:
執行下列指令:
C:\Program Files\Google\Compute Engine\metadata_scripts\run_startup_scripts.cmd
查看 Windows 啟動指令碼的輸出內容
使用下列任一方法查看 Windows Server 開機指令碼的輸出內容,並檢查 GCEMetadataScripts
事件:
在 Google Cloud 控制台中選擇序列埠 1。詳情請參閱「查看序列埠輸出內容」。
Windows 事件檢視器的應用程式記錄。
透過 Windows 工作站的 IAP Desktop。詳情請參閱 GitHub 上的 GoogleCloudPlatform/iap-desktop 存放區。
後續步驟
瞭解如何在 Linux VM 中使用開機指令碼。
瞭解如何新增關閉指令碼。
進一步瞭解VM 中繼資料。