[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[[["\u003cp\u003eStartup scripts automate tasks during a virtual machine's boot process, and this guide details how to use them on VM instances.\u003c/p\u003e\n"],["\u003cp\u003eWindows startup scripts require a specific naming format, combining the script name and file extension, like \u003ccode\u003escript-name-ps1\u003c/code\u003e, and you can use the \u003ccode\u003e.ps1\u003c/code\u003e, \u003ccode\u003e.cmd\u003c/code\u003e, and \u003ccode\u003e.bat\u003c/code\u003e file extensions.\u003c/p\u003e\n"],["\u003cp\u003eWhen using multiple Windows startup scripts, the order of execution is PowerShell scripts first, then batch scripts, followed by command scripts, and if there are multiple with the same file extension, it runs them in alphabetical order by name.\u003c/p\u003e\n"],["\u003cp\u003eStartup scripts can be passed as either clear text for scripts up to 2048 bytes or as a Kubernetes secret for larger scripts, and they are configured within the \u003ccode\u003estartupScripts\u003c/code\u003e field in the VM's \u003ccode\u003espec\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eStartup scripts are executed on every boot with default root privileges, and it's crucial to verify the correct matching of \u003ccode\u003escriptSecretRef\u003c/code\u003e in the VM's spec with the \u003ccode\u003emetadata.name\u003c/code\u003e field in the Kubernetes secret.\u003c/p\u003e\n"]]],[],null,["# Use a startup script on Windows VMs\n\nA startup script performs tasks during the start-up process for a virtual machine\n(VM). This page provides you with the steps to follow for using startup scripts\non VM instances.\n\n\nBefore you begin\n----------------\n\nTo use `gdcloud` command-line interface (CLI) commands, ensure that you have downloaded, installed,\nand configured the [`gdcloud` CLI](/distributed-cloud/hosted/docs/latest/gdch/resources/gdcloud-overview).\nAll commands for Distributed Cloud use the `gdcloud` or\n`kubectl` CLI, and require an operating system (OS) environment.\n\n### Get the kubeconfig file path\n\nTo run commands against the Management API server, ensure you have the following\nresources:\n\n1. [Sign in and generate](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/iam/sign-in#cli) the\n kubeconfig file for the Management API server if you don't have one.\n\n2. Use the path to the kubeconfig file of the Management API server to replace\n \u003cvar translate=\"no\"\u003eMANAGEMENT_API_SERVER\u003c/var\u003e in these instructions.\n\n### Request permissions and access\n\nTo perform the tasks listed in this page, you must have the Project\nVirtualMachine Admin role. Follow the steps to either\n[verify](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vms/preparation)\nyour access or have your Project IAM Admin\n[assign](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vms/preparation)\nyou the Project VirtualMachine Admin (`project-vm-admin`) role in the namespace\nof the project where the VM resides.\n\nFormat Windows startup script names\n-----------------------------------\n\nStartup scripts for Windows VMs must follow a specific naming format with the\nscript name and the file extension appended, separated by dashes. For example,\n`create-directory-ps1`, with `create-directory` as the script name and `ps1`\nas the file extension. Incorrect formats include using periods in place of\ndashes and omitting the file extension, such as `create-directory.ps1` and\n`create-directory`.\n\nThe following shows the file extensions to use for Windows startup scripts:\n\n- **`ps1`**: Contains PowerShell commands.\n- **`cmd`**: Contains batch commands for the Command Prompt interpreter.\n- **`bat`**: Contains batch commands.\n\nRun Windows startup scripts in order\n------------------------------------\n\nYou can use multiple startup scripts. The type of file containing the\nscript impacts the order of running each script. The following table shows the\norder of running Windows startup scripts:\n\nIf two startup scripts have the same file extension, the scripts are run based\non the file extension and name in alphabetical order. For example, if your\nstartup script names are `alpha-script-ps1` and `beta-script-ps1`,\n`alpha-script-ps1` runs before `beta-script-ps1`.\n\nThe following example VM specification shows the Windows startup script order\nand structure in the `startupScripts` field: \n\n ...\n startupScripts:\n - name: create-directory-ps1\n script: |-\n New-Item -ItemType Directory -Path C:\\ -Name NewDir\n Get-ChildItem -Path C:\\\n - name: hello-world-bat\n script: |-\n ECHO \"Hello World!\"\n - name: list-directories-cmd\n script: |-\n dir C:\\Windows \u003e files.txt\n ...\n\nPass a startup script\n---------------------\n\nDistributed Cloud runs startup scripts in alphabetical order,\nbased on the name of each startup script.\n\nThe following table shows the script format to use based on your script size:\n\n### Define a startup script\n\nTo use a startup script you must add the `startupScripts` field to the VM `spec`\nfield. Within this field you can specify multiple startup scripts as clear\ntext or as a Kubernetes secret.\n\nThe following example specifies the startup scripts as clear text and a\nKubernetes secret: \n\n apiVersion: virtualmachine.gdc.goog/v1\n kind: VirtualMachine\n metadata:\n name: \"vm-test\"\n spec:\n ...\n startupScripts:\n - name: create-directory-ps1\n script: |-\n New-Item -ItemType Directory -Path C:\\ -Name NewDir\n Get-ChildItem -Path C:\\\n - name: list-directories-cmd\n scriptSecretRef:\n name: list-directories\n ---\n\n apiVersion: v1\n kind: Secret\n type: Opaque\n metadata:\n name: list-directories\n data: \n script:\n ZGlyIEM6XFdpbmRvd3MgID4gZmlsZXMudHh0Cg==\n\nReview the following considerations:\n\n- The startup script runs on *every boot*.\n- The startup script has default root privileges.\n- In the Kubernetes secret, the name of the `scriptSecretRef` in the VM `spec` must match the `metadata.name` field.\n- In the Kubernetes secret, specify the startup script content by adding a key `script` to the `data` field.\n\nCreate a new VM with a Windows startup script\n---------------------------------------------\n\nTo create a VM with a Windows startup script, follow the steps in\n[Create a new VM with a startup script](/distributed-cloud/hosted/docs/latest/gdch/application/ao-user/vms/manage-the-os/startup-script-vm-instance#create-new-vm-startup-script)."]]