在 Windows VM 中使用開機指令碼


開機指令碼是檔案,可在虛擬機器 (VM) 執行個體的啟動程序期間執行工作。開機指令碼可套用至專案中的所有 VM,或單一 VM。VM 層級中繼資料指定的開機指令碼會覆寫專案層級中繼資料指定的開機指令碼,且開機指令碼只會在網路可用時執行。本文說明如何在 Windows Server VM 執行個體上使用開機指令碼。如要瞭解如何新增專案層級的啟動指令碼,請參閱 gcloud compute project-info add-metadata

Windows 啟動指令碼必須是命令殼層 (.cmd)、PowerShell (.ps1) 或批次檔指令碼 (.bat),且必須具有適當的副檔名。

如果您使用本文件中的任一程序指定開機指令碼,Compute Engine 會執行下列操作:

  1. 將開機指令碼複製到 VM

  2. 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

      1. 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.

      2. Set a default region and zone.
      3. REST

        如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 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.

        詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using 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

  1. 前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。

    前往「建立執行個體」

  2. 在「開機磁碟」部分,選取「變更」,然後執行下列操作:

    1. 在「Public images」(公開映像檔) 分頁中,選擇 Windows Server 作業系統。
    2. 按一下 [選取]。
  3. 展開「Advanced options」(進階選項) 區段,然後執行下列操作:

    1. 展開「管理」部分。
    2. 在「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
        
  4. 點選「建立」

將 Windows 開機指令碼直接傳遞至現有 VM

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往 VM 執行個體

  2. 按一下 VM 的「名稱」

  3. 按一下 [編輯]

  4. 在「中繼資料」下方,指定下列設定:

    • keywindows-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

  1. 使用 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 名稱。

  2. 在呼叫 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 之前,請先完成下列步驟:

  1. 建立本機 (.ps1) 檔案,用於儲存開機指令碼。

  2. 請注意從 gcloud CLI 到啟動指令碼的相對路徑。

  3. 在檔案中新增下列啟動指令碼:

    # 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:啟動指令碼檔案的相對路徑
將本機檔案中的 Windows 開機指令碼傳遞至現有 VM

使用下列 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:啟動指令碼檔案的相對路徑
將本機檔案中的 Windows 開機指令碼傳遞至現有 VM

使用下列 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:啟動指令碼檔案的相對路徑
將本機檔案中的 Windows 開機指令碼傳遞至現有 VM

使用下列 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 新增開機指令碼前,請先完成下列步驟:

  1. 建立檔案來儲存開機指令碼。這個範例使用 PowerShell (.ps1) 檔案。

  2. 將下列 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
    
  3. 建立 Cloud Storage 值區

  4. 將檔案新增至 Cloud Storage bucket

安全性影響

  • 根據預設,專案擁有者和專案編輯者可以存取相同專案中的 Cloud Storage 檔案,除非有明確的存取權控管機制禁止存取。

  • 如果 Cloud Storage 值區或物件的安全性低於中繼資料,一旦啟動指令碼遭到修改,且 VM 重新啟動,就可能發生權限提升的風險。這是因為 VM 重新啟動後,啟動指令碼會以 LocalSystem 執行,然後使用附加服務帳戶的權限存取其他資源。

主控台

將儲存在 Cloud Storage 中的開機指令碼傳遞至新的 VM

  1. 前往 Google Cloud 控制台的「Create an instance」(建立執行個體) 頁面。

    前往「建立執行個體」

  2. 在「開機磁碟」部分,選取「變更」,然後執行下列操作:

    1. 在「Public images」(公開映像檔) 分頁中,選擇 Windows Server 作業系統。
    2. 按一下 [選取]。
  3. 在「Identity and API access」(身分及 API 存取權) 區段中,選取具有「Storage 物件檢視者」角色的服務帳戶。

  4. 展開「Advanced options」(進階選項) 區段,然後執行下列操作:

    1. 展開「管理」部分。
    2. 在「Metadata」(中繼資料) 專區中,為下列項目新增值:

      • :中繼資料鍵。指定 windows-startup-script-url 中繼資料鍵,讓指令碼在首次開機後每次開機時執行。

      • :中繼資料值。使用下列其中一種格式,將開機指令碼檔案設為 Cloud Storage 位置:

        • 已驗證的網址https://storage.googleapis.com/BUCKET/FILE
        • gcloud storage URIgs://BUCKET/FILE

        更改下列內容:

        • BUCKET:包含開機指令碼檔案的 bucket 名稱
        • FILE:開機指令碼檔案的名稱
  5. 如要建立 VM,請按一下「建立」

將儲存在 Cloud Storage 中的開機指令碼傳遞至現有 VM

  1. 前往 Google Cloud 控制台的「VM instances」(VM 執行個體) 頁面

    前往 VM 執行個體

  2. 按一下 VM 的「名稱」

  3. 按一下 [編輯]

  4. 在「Metadata」(中繼資料) 下新增下列值:

    • 已驗證的網址https://storage.googleapis.com/BUCKET/FILE
    • gcloud storage URIgs://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 URIgs://BUCKET/FILE
將儲存在 Cloud Storage 中的開機指令碼傳遞至現有 VM

使用下列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 URIgs://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 URIgs://BUCKET/FILE
將儲存在 Cloud Storage 中的開機指令碼傳遞至現有 VM

使用下列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 URIgs://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 URIgs://BUCKET/FILE
將儲存在 Cloud Storage 中的開機指令碼傳遞至現有 VM

使用下列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 URIgs://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 URIgs://BUCKET/FILE

將儲存在 Cloud Storage 中的開機指令碼傳遞至現有 VM

  1. 使用 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 名稱

  2. 在呼叫 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 URIgs://BUCKET/FILE

驗證開機指令碼

在網路瀏覽器中查看外部 IP,確認開機指令碼已建立網站。您可能需要等待約 10 分鐘,範例啟動指令碼才會完成。

從 Windows 啟動指令碼存取中繼資料

您可以在開機指令碼中存取中繼資料值。舉例來說,您可以對多部 VM 使用相同指令碼,並將不同的中繼資料值傳遞至每部 VM,個別為每個指令碼設定參數。

如要從開機指令碼存取自訂中繼資料值,請按照下列步驟操作:

  1. 建立開機指令碼,查詢中繼資料鍵的值。舉例來說,下列 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
    
  2. 使用下列 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:啟動指令碼檔案的相對路徑

    如要進一步瞭解如何指定中繼資料鍵值對,請參閱設定及移除自訂中繼資料

  3. 在網路瀏覽器中查看外部 IP,確認啟動指令碼輸出 foo 的值。您可能需要等待約 10 分鐘,範例開機指令碼才會完成。

重新執行 Windows 開機指令碼

如要在 Windows VM 上重新執行開機指令碼,請按照下列步驟操作:

  1. 連線至 VM

  2. 執行下列指令:

    C:\Program Files\Google\Compute Engine\metadata_scripts\run_startup_scripts.cmd
    

查看 Windows 開機指令碼的輸出內容

使用下列任一方法查看 Windows Server 開機指令碼的輸出內容,並檢查 GCEMetadataScripts 事件:

後續步驟