Overview
When creating a CVM, you can configure an instance by specifying custom data. During the first launch of the CVM, the custom data will be passed into the CVM in text format and be executed. If you purchase multiple CVMs at a time, the custom data text will be executed on all CVMs during their first launch.
This document describes an example in which a PowerShell script is passed during the first launch of a Windows CVM.
Notes
Windows operating systems that support custom data include:
Windows Server 2019 IDC 64-bit English version
Windows Server 2016 IDC 64-bit English version
Windows Server 2012 R2 IDC 64-bit English version
A command can be executed by passing text only when a CVM is launched for the first time.
Before Base64 encoding, the size of the custom data cannot exceed 16 KB.
Custom data is Base64 encoded and then passed. If you directly copy a non-Base64 script file, do not select “The entry is Base64-encoded text”.
During launch, executing specified tasks in custom data will increase the amount of time it takes to launch the CVM. We recommend that you wait for a few minutes, and after the tasks are completed, test whether the tasks have been successfully executed.
In this example, specify the Windows PowerShell script by using the PowerShell label, for example, the <powershell></powershell> label.
Directions
Preparing text
Prepare text based on your actual requirements:
PowerShell script
Use the PowerShell label to prepare a PowerShell script.
For example, if you need to create a “tencentcloud.txt” file with the content of “Hello Tencent Cloud.” in the C drive (C:), use the PowerShell label to prepare the following content:
<powershell>
"Hello Tencent Cloud." | Out-File C:\\tencentcloud.txt
</powershell>
Base64 encoded script
1. Run the following command to create a PowerShell script named “script_text.ps1”.
2. Press i to switch to the editing mode, refer to the following content, write it into the file, and save the “script_text.ps1” script.
<powershell>
"Hello Tencent Cloud." | Out-File C:\\tencentcloud.txt
</powershell>
3. Run the following command to perform the Base64 encoding operation on the “script_text.ps1” script.
The following information is returned:
PHBvd2Vyc2hlbGw+CiJIZWxsbyBUZW5jZW50IENsb3VkLiIgfCBPdXQtRmlsZSAgQzpcdGVuY2VudGNsb3VkLnR4dAo8L3Bvd2Vyc2hlbGw+Cg==
Passing text
We provide multiple methods to launch an instance, and here we introduce two of them. Choose a method according to your requirements:
1. Purchase an instance (see Creating an Instance), and click Advanced settings under Other settings in 2. Configure network and host.
2. In Advanced settings, enter the text content you have prepared in the Custom data text box.
Base64 encoded script: Select “The above input is encoded with base64”, and then type in Base64 encoded text.
3. Follow the instruction to complete CVM creation.
When creating a CVM by using API, you can pass the text by assigning the value of the encoded result returned in Base64 encoded script to the UserData parameter of the RunInstances API.
The following is an sample CVM creation request with UserData: https://cvm.tencentcloudapi.com/?Action=RunInstances
&Version=2017-03-12
&Placement.Zone=ap-guangzhou-2
&ImageId=img-pmqg1cw7
&UserData=PHBvd2Vyc2hlbGw+CiJIZWxsbyBUZW5jZW50IENsb3VkLiIgfCBPdXQtRmlsZSAuXHRlbmNlbnRjbG91ZC50go=
&<Common Request Parameters>
Verifying custom data configuration
1. Log in to your CVM.
2. On the operating system interface, open the C drive (C:), and check whether the tencentcloud.txt
text file exists.
If the tencentcloud.txt
text file exists, the configuration is successful, as shown in the following figure:
Viewing execution logs
You can view the C:\\Program Files\\Cloudbase Solutions\\Cloudbase-Init\\log\\cloudbase-init.log
file to get the execution logs of the script.
Was this page helpful?