開機指令碼是檔案,會在虛擬機器 (VM) 執行個體的啟動程序中執行工作。開機指令碼可套用至專案中的所有 VM,或單一 VM。VM 層級中繼資料指定的開機指令碼會覆寫專案層級中繼資料指定的開機指令碼,且開機指令碼只會在網路可用時執行。本文說明如何在 Windows Server VM 執行個體上使用開機指令碼。如要瞭解如何新增專案層級的啟動指令碼,請參閱 gcloud compute project-info add-metadata。
Windows 啟動指令碼必須是命令殼層 (.cmd)、PowerShell (.ps1) 或批次檔指令碼 (.bat),且必須具有適當的副檔名。
如果您使用本文中的任一程序指定開機指令碼,Compute Engine 會執行下列操作:
將開機指令碼複製到 VM
VM 開機時,工作排程器會以
LocalSystem帳戶執行開機指令碼
如要瞭解開機指令碼的相關工作,以及執行各項工作的時機,請參閱開機指令碼總覽文件。
必要條件
如要在 VM 執行個體上執行儲存在中繼資料中的指令碼,必須安裝並執行訪客環境。
- 訪客環境包含訪客代理程式 (例如 Linux 上的 google-guest-agent),可從執行個體的中繼資料讀取指令碼內容或網址,並啟動執行作業。
- 所有 Compute Engine 公開映像檔都預先安裝了訪客環境。
- 如果您建立自訂映像檔,請務必手動安裝 Google 客體環境,確保中繼資料和其他 Google Cloud 功能的指令碼正常運作。
事前準備
- 閱讀開機指令碼總覽。
- 瞭解 VM 中繼資料的基本概念。
-
如果尚未設定驗證,請先完成設定。
驗證可確認您的身分,以便存取 Google Cloud 服務和 API。如要從本機開發環境執行程式碼或範例,請選取下列其中一個選項,向 Compute Engine 進行驗證:
選取這個頁面上的分頁,瞭解如何使用範例:
控制台
使用 Google Cloud 控制台存取 Google Cloud 服務和 API 時,無須設定驗證。
gcloud
-
安裝 Google Cloud CLI。 完成後,執行下列指令來初始化 Google Cloud CLI:
gcloud init若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
- 設定預設地區和區域。
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
安裝 Google Cloud CLI。
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
詳情請參閱 Google Cloud 驗證說明文件中的「使用 REST 進行驗證」。
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」(建立執行個體) 頁面。
在「開機磁碟」部分,選取「變更」,然後執行下列操作:
- 在「Public images」(公開映像檔) 分頁中,選擇 Windows Server 作業系統。
- 按一下「選取」。
展開「Advanced options」(進階選項) 區段,然後執行下列操作:
- 展開「管理」部分。
在「Metadata」(中繼資料) 區段,按一下「Add item」(新增項目),設定「Key」(鍵) 和「Value」(值):
金鑰:設為
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-ps1value:開機指令碼的內容
驗證開機指令碼
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 的metadata.fingerprint值:GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME
更改下列內容:
PROJECT_ID:VM 所在的專案 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:VM 所在的專案 ID。
ZONE:VM 的可用區。
VM_NAME:VM 的名稱。
FINGERPRINT:使用
instances.get方法取得的metadata.fingerprint值。
驗證開機指令碼
VM 啟動後,在網頁瀏覽器中查看外部 IP,確認開機指令碼已建立網站。您可能需要等待約 10 分鐘,範例啟動指令碼才會完成。
從本機檔案傳遞 Windows 開機指令碼
您可以在工作站的本機批次檔、命令殼層指令碼或未簽署的 PowerShell 指令碼中儲存開機指令碼,並在建立 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,並使用 gcloud compute instances create 指令搭配 --metadata-from-file 旗標,傳送要當做開機指令碼的本機檔案內容:
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,並使用 gcloud compute instances create 指令搭配 --metadata-from-file 旗標,傳送要當做開機指令碼的本機檔案內容:
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,並使用 gcloud compute instances create 指令搭配 --metadata-from-file 旗標,傳送要當做開機指令碼的本機檔案內容:
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 指令碼或 Cloud Storage 中的可執行檔,並在建立 VM 時傳遞該指令碼。將開機指令碼新增至 Cloud Storage 後,您會取得一個網址,可在建立 VM 時參照該網址。
從 Cloud Storage bucket 新增開機指令碼前,請先完成下列步驟:
建立檔案來儲存開機指令碼。這個範例使用 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」(建立執行個體) 頁面。
在「開機磁碟」部分,選取「變更」,然後執行下列操作:
- 在「Public images」(公開映像檔) 分頁中,選擇 Windows Server 作業系統。
- 按一下「選取」。
展開「Advanced options」(進階選項) 區段,然後執行下列操作:
- 展開「管理」部分。
在「Metadata」(中繼資料) 專區中,為下列項目新增值:
鍵:中繼資料鍵。指定
windows-startup-script-url中繼資料鍵,讓指令碼在首次開機後每次開機時執行。值:中繼資料值。使用下列其中一種格式,將開機指令碼檔案設為 Cloud Storage 位置:
- 已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE - gcloud storage URI:
gs://BUCKET/FILE
更改下列內容:
- BUCKET:包含開機指令碼檔案的 bucket 名稱
- FILE:開機指令碼檔案名稱
- 已驗證的網址:
按一下「Create」(建立),即可建立 VM。
將儲存在 Cloud Storage 中的開機指令碼傳遞至現有 VM
前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面。
按一下 VM 的「名稱」。
按一下 [編輯]。
在「Metadata」(中繼資料) 下新增下列值:
- 已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE - gcloud storage 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 storage 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 storage 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 storage 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 storage 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 storage 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 storage 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 storage URI:
gs://BUCKET/FILE
- 已驗證的網址:
將儲存在 Cloud Storage 中的開機指令碼傳遞至現有 VM
使用
instances.get方法取得 VM 的metadata.fingerprint值:GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances/VM_NAME
更改下列內容:
PROJECT_ID:VM 所在的專案 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:VM 所在的專案 ID。
ZONE:VM 的可用區。
VM_NAME:VM 的名稱。
FINGERPRINT:使用
instances.get方法取得的metadata.fingerprint值。CLOUD_STORAGE_URL:中繼資料值。使用下列其中一種格式,將開機指令碼檔案設為 Cloud Storage 位置:
- 已驗證的網址:
https://storage.googleapis.com/BUCKET/FILE - gcloud storage 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使用下列
gcloud compute instances create指令建立 VM 時,請設定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 中繼資料。