您可以通过现有 Compute Engine Windows Server 映像创建自定义 Windows Server 映像。使用这些自定义映像可创建与现有实例具有相同启动磁盘的实例。
这些自定义映像十分实用,可用于保存现有实例的操作系统配置,并重复使用相同的配置来创建其他实例。
本部分未介绍以下任务:
- 通过公共映像或自定义映像创建虚拟机:请改为参阅创建新的 Windows 实例。
- 安排永久性磁盘的备份:请改为参阅永久性磁盘快照。
- 导入 Windows Server 或 SQL Server 系统;请改为使用虚拟机迁移服务。
准备工作
-
如果您尚未设置身份验证,请进行设置。身份验证是通过其进行身份验证以访问 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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
- 停用
GCEStartup
计划任务。 - 删除临时文件夹位置中的所有文件。
- 清除所有 Windows 事件日志。
- 运行 sysprep.exe /generalize /oobe /quit /unattend
- 将
instance_setup.ps1
配置为在首次启动虚拟机实例时运行。 - 删除 RDP 证书。
- 移除永久性磁盘的存储列表。
- 启用 RDP 和 WinRM 防火墙规则。
- 停用 Google OS Config 服务。
- 关停实例。
以管理权限运行
GCESysprep
。GCESysprep
创建映像。
- 永久性磁盘,即使该磁盘已连接到实例。
- 永久性磁盘的快照
- 您项目中的另一个映像
- 通过另一个项目共享的映像
存储在 Cloud Storage 中的导入的映像
- 在 Google Cloud 控制台中,前往创建新映像页面。
- 指定映像名称。
- 选择含有 Windows 操作系统并且您希望根据其创建映像的源磁盘。
- 指定用于存储映像的位置。从基于来源磁盘位置(默认)(Based on source disk location (default)) 下拉菜单中选择映像位置。例如,指定
us
可将映像存储在us
多区域中,或指定us-central1
可将其存储在us-central1
区域中。如果您未选择位置,Compute Engine 会将映像存储在最靠近映像源位置的多区域中。 - 指定您的映像的属性。例如,您可以为映像指定映像系列名称,以将此映像纳入映像系列中。
- 点击创建。
[DISK_NAME]
是要在其中创建映像的源磁盘的名称。[ZONE]
是磁盘的地区。[LOCATION]
是一个可选标志,允许您指定要存储映像的区域或多区域。 例如,指定us
可将映像存储在us
多区域中,或指定us-central1
可将其存储在us-central1
区域中。如果您未选择位置,Compute Engine 会将映像存储在最靠近映像来源位置的多区域中。--force
是一个可选标志,即使在磁盘已挂接到仍在运行的实例的情况下,也会创建映像。但这一选项可能会损害映像的完整性。如果可能,请在创建映像前停止实例。[PROJECT_ID]
是此请求的项目 ID。[FORCE_OPTION]
是一个选项,如果使用该选项,那么即使来源磁盘已挂接到正在运行的实例,也会创建映像。请在 POST 行的末尾指定forceCreate=true
来设置此选项。但这一选项可能会损害映像的完整性。如果可能,请在创建映像前停止实例。[IMAGE_NAME]
是为此映像指定的名称。[ZONE]
是根据其创建映像的来源磁盘的可用区。[DISK_NAME]
是来源磁盘的名称。[LOCATION]
是一个可选参数,可让您选择映像的多区域或区域存储位置。例如,指定us
可将映像存储在us
多区域中,或指定us-central1
可将其存储在us-central1
区域中。如果您未选择位置,Compute Engine 会将映像存储在最靠近映像源位置的多区域中。
Go
如需在本地开发环境中使用本页面上的 Go 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
Java
如需在本地开发环境中使用本页面上的 Java 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
Node.js
如需在本地开发环境中使用本页面上的 Node.js 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
Python
如需在本地开发环境中使用本页面上的 Python 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。
如需了解详情,请参阅 Set up authentication for a local development environment。 如需了解详情,请参阅身份验证文档中的为本地开发环境设置 ADC。
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 身份验证文档中的使用 REST 时进行身份验证。
准备创建 Windows Server 映像
在创建映像之前,请先在实例上运行
GCESysprep
,以便为映像创建过程做好准备。GCESysprep
通过完成以下步骤准备实例成为 Compute Engine 映像:GCESysprep
操作会记录到 Windows 事件日志和串行端口 1。 Sysprep 写入多个日志文件。如何使用
GCESysprep
创建 Compute Engine 映像:指定映像位置
创建自定义映像时,您可以为该映像指定除了双区域位置之外的 Cloud Storage 位置。指定映像存储位置后,您可以通过确保跨区域实现冗余来满足数据存储区域的法规和合规性要求以及高可用性需求。
存储位置功能是可选的。如果您未选择位置,Compute Engine 会将映像存储在最靠近映像源的多区域位置。您可以根据源磁盘、映像、快照或存储在 Cloud Storage 中的映像创建自定义映像。您可以使用这些映像创建新的虚拟机实例。
此功能发布之前的所有现有映像都保留在原来的位置。 唯一的变化是您可以查看所有映像的位置。如果要移动现有映像,您必须在目标位置重新创建该映像。
创建 Windows 映像
您可从以下来源创建磁盘映像:
控制台
gcloud
可将
gcloud compute
与images create
搭配使用,并提供您希望在其中创建映像的源永久性磁盘。(可选)添加--force
标志,可以在即使磁盘挂接到正在运行的实例的情况下,继续创建映像。gcloud compute images create example-image --source-disk [DISK_NAME] \ --source-disk-zone [ZONE] \ --storage-location [LOCATION] \ [--force]
运行此命令时,
gcloud compute
会根据您提供的永久性磁盘创建新映像,并将其添加到您的集合中。您可以通过运行以下命令来确认您的映像已成功创建:gcloud compute images list
Go
试用此示例之前,请按照《Compute Engine 快速入门:使用客户端库》中的 Go 设置说明进行操作。 如需了解详情,请参阅 Compute Engine Go API 参考文档。
如需向 Compute Engine 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Java
试用此示例之前,请按照《Compute Engine 快速入门:使用客户端库》中的 Java 设置说明进行操作。 如需了解详情,请参阅 Compute Engine Java API 参考文档。
如需向 Compute Engine 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Node.js
Node.js
试用此示例之前,请按照《Compute Engine 快速入门:使用客户端库》中的 Node.js 设置说明进行操作。 如需了解详情,请参阅 Compute Engine Node.js API 参考文档。
如需向 Compute Engine 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
Python
Python
试用此示例之前,请按照《Compute Engine 快速入门:使用客户端库》中的 Python 设置说明进行操作。 如需了解详情,请参阅 Compute Engine Python API 参考文档。
如需向 Compute Engine 进行身份验证,请设置应用默认凭证。如需了解详情,请参阅为本地开发环境设置身份验证。
REST
向
images().insert
方法发出POST
请求,并在请求正文中添加sourceDisk
网址。POST https://compute.googleapis.com/compute/v1/projects/[PROJECT_ID]/global/images?[FORCE_OPTION] { "name": "[IMAGE_NAME]", "sourceDisk": "zones/[ZONE]/disks/[DISK_NAME]", "storageLocations": "[LOCATION]", }
其中:
有关添加映像的更多信息,请参阅映像参考。
更新 Windows 代理和脚本
Compute Engine 会不定期发布含有最新代理和脚本的新 Windows 映像。这些代理和脚本可以在启动和关停过程、账号管理和地址管理方面为 Windows 实例提供帮助。
从 Windows 映像版本
v20160112
开始,Windows 代理将通过上游版本自行更新。您可以通过将disable-agent-updates
实例元数据键设置为true
来停用这些代理更新。如果您的实例基于较早映像版本,则可以为这些实例手动更新 Windows 代理。后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-06-23。
-