适用于 Windows 的自定义脚本扩展Custom Script Extension for Windows

自定义脚本扩展在 Azure 虚拟机上下载并执行脚本。The Custom Script Extension downloads and executes scripts on Azure virtual machines. 此扩展适用于部署后配置、软件安装或其他任何配置或管理任务。This extension is useful for post deployment configuration, software installation, or any other configuration or management tasks. 可以从 Azure 存储或 GitHub 下载脚本,或者在扩展运行时会脚本提供给 Azure 门户。Scripts can be downloaded from Azure storage or GitHub, or provided to the Azure portal at extension run time. 自定义脚本扩展与 Azure 资源管理器模板集成,并且可以通过 Azure CLI、PowerShell、Azure 门户或 Azure 虚拟机 REST API 来运行。The Custom Script Extension integrates with Azure Resource Manager templates, and can be run using the Azure CLI, PowerShell, Azure portal, or the Azure Virtual Machine REST API.

本文档详细说明如何通过 Azure PowerShell 模块、Azure 资源管理器模板使用自定义脚本扩展,同时详细说明 Windows 系统上的故障排除步骤。This document details how to use the Custom Script Extension using the Azure PowerShell module, Azure Resource Manager templates, and details troubleshooting steps on Windows systems.

先决条件Prerequisites

备注

不要在将同一 VM 作为参数的情况下使用自定义脚本扩展运行 Update-AzVM,因为它将等待它本身响应。Do not use Custom Script Extension to run Update-AzVM with the same VM as its parameter, since it will wait on itself.

操作系统Operating System

适用于 Windows 的自定义脚本扩展将在扩展支持的扩展 OS 上运行;The Custom Script Extension for Windows will run on the extension supported extension OSs;

WindowsWindows

  • Windows Server 2008 R2Windows Server 2008 R2
  • Windows Server 2012Windows Server 2012
  • Windows Server 2012 R2Windows Server 2012 R2
  • Windows 10Windows 10
  • Windows Server 2016Windows Server 2016
  • Windows Server 2016 CoreWindows Server 2016 Core
  • Windows Server 2019Windows Server 2019
  • Windows Server 2019 CoreWindows Server 2019 Core

脚本位置Script Location

可以将扩展配置为使用 Azure Blob 存储凭据来访问 Azure Blob 存储。You can configure the extension to use your Azure Blob storage credentials to access Azure Blob storage. 脚本位置可以是任何位置,只要 VM 可以路由到该终结点(例如 GitHub 或内部文件服务器)即可。The script location can be anywhere, as long as the VM can route to that end point, such as GitHub or an internal file server.

Internet 连接Internet Connectivity

如果需要从外部(例如 GitHub 或 Azure 存储)下载脚本,则需要打开其他防火墙和网络安全组端口。If you need to download a script externally such as from GitHub or Azure Storage, then additional firewall and Network Security Group ports need to be opened. 例如,如果脚本位于 Azure 存储中,可以使用 Azure NSG 服务标记对存储进行访问。For example, if your script is located in Azure Storage, you can allow access using Azure NSG Service Tags for Storage.

如果脚本位于本地服务器上,则可能仍需要打开其他防火墙和网络安全组端口。If your script is on a local server, then you may still need additional firewall and Network Security Group ports need to be opened.

提示和技巧Tips and Tricks

  • 脚本中的语法错误会导致此扩展失败率最高,应测试脚本运行正确无误,同时在脚本中添加其他日志记录,以便更轻松地找到失败位置。The highest failure rate for this extension is because of syntax errors in the script, test the script runs without error, and also put in additional logging into the script to make it easier to find where it failed.
  • 编写幂等脚本。Write scripts that are idempotent. 这样可以确保即使它们再次意外运行,也不会引起系统更改。This ensures that if they run again accidentally, it will not cause system changes.
  • 确保这些脚本在运行时不需要用户输入。Ensure the scripts don't require user input when they run.
  • 脚本可以运行 90 分钟,若运行时间超过 90 分钟,将导致扩展的预配失败。There's 90 minutes allowed for the script to run, anything longer will result in a failed provision of the extension.
  • 不要重启置于脚本内,此操作会导致所安装的其他扩展出现问题。Don't put reboots inside the script, this action will cause issues with other extensions that are being installed. 扩展不会在重启之后继续。Post reboot, the extension won't continue after the restart.
  • 如果你有可导致重启的脚本,则安装应用程序并运行该脚本,可使用 Windows 计划任务或 DSC、Chef 或 Puppet 扩展等工具来计划重启。If you have a script that will cause a reboot, then install applications and run scripts, you can schedule the reboot using a Windows Scheduled Task, or use tools such as DSC, Chef, or Puppet extensions.
  • 建议不要运行会导致 VM 代理停止或更新的脚本。It is not recommended to run a script that will cause a stop or update of the VM Agent. 这会使扩展处于“正在转换”状态,从而导致超时。This can let the extension in a Transitioning state, leading to a timeout.
  • 扩展将只运行脚本一次,如果想要在每次启动时运行脚本,则需要使用扩展创建 Windows 计划任务。The extension will only run a script once, if you want to run a script on every boot, then you need to use the extension to create a Windows Scheduled Task.
  • 如果想要计划脚本何时运行,应使用扩展创建 Windows 计划任务。If you want to schedule when a script will run, you should use the extension to create a Windows Scheduled Task.
  • 脚本运行时,Azure 门户或 CLI 中只会显示“正在转换”扩展状态。When the script is running, you will only see a 'transitioning' extension status from the Azure portal or CLI. 如果希望更频繁地更新正在运行的脚本的状态,需要创建自己的解决方案。If you want more frequent status updates of a running script, you'll need to create your own solution.
  • 自定义脚本扩展本身不支持代理服务器,但可以在脚本中使用支持代理服务器的文件传输工具,如 CurlCustom Script extension does not natively support proxy servers, however you can use a file transfer tool that supports proxy servers within your script, such as Curl
  • 请注意脚本或命令可能依赖的非默认目录位置,按逻辑对这种情况进行处理。Be aware of non-default directory locations that your scripts or commands may rely on, have logic to handle this situation.
  • 自定义脚本扩展将在 LocalSystem 帐户下运行Custom Script Extension will run under the LocalSystem Account
  • 如果你计划使用 storageAccountName 和 storageAccountKey 属性,这些属性必须并置在 protectedSettings 中 。If you plan to use the storageAccountName and storageAccountKey properties, these properties must be collocated in protectedSettings .

扩展架构Extension schema

自定义脚本扩展配置指定脚本位置和要运行命令等设置。The Custom Script Extension configuration specifies things like script location and the command to be run. 可将此配置存储在配置文件中、在命令行中指定,或者在 Azure 资源管理器模板中指定该配置。You can store this configuration in configuration files, specify it on the command line, or specify it in an Azure Resource Manager template.

可将敏感数据存储在受保护的配置中,此配置经过加密,只能在虚拟机内部解密。You can store sensitive data in a protected configuration, which is encrypted and only decrypted inside the virtual machine. 当执行命令包含机密(例如密码)时,受保护的配置相当有用。The protected configuration is useful when the execution command includes secrets such as a password.

这些项目应视为敏感数据,并在扩展的受保护设置配置中指定。These items should be treated as sensitive data and specified in the extensions protected setting configuration. Azure VM 扩展的受保护设置数据已加密,并且只能在目标虚拟机上解密。Azure VM extension protected setting data is encrypted, and only decrypted on the target virtual machine.

{
    "apiVersion": "2018-06-01",
    "type": "Microsoft.Compute/virtualMachines/extensions",
    "name": "virtualMachineName/config-app",
    "location": "[resourceGroup().location]",
    "dependsOn": [
        "[concat('Microsoft.Compute/virtualMachines/', variables('vmName'),copyindex())]",
        "[variables('musicstoresqlName')]"
    ],
    "tags": {
        "displayName": "config-app"
    },
    "properties": {
        "publisher": "Microsoft.Compute",
        "type": "CustomScriptExtension",
        "typeHandlerVersion": "1.10",
        "autoUpgradeMinorVersion": true,
        "settings": {
            "fileUris": [
                "script location"
            ],
            "timestamp":123456789
        },
        "protectedSettings": {
            "commandToExecute": "myExecutionCommand",
            "storageAccountEndPoint": "https://core.chinacloudapi.cn/",
            "storageAccountName": "myStorageAccountName",
            "storageAccountKey": "myStorageAccountKey",
            "managedIdentity" : {}
        }
    }
}

备注

managedIdentity 属性 不能 与 storageAccountName 或 storageAccountKey 属性结合使用managedIdentity property must not be used in conjunction with storageAccountName or storageAccountKey properties

备注

在某个时间点,一个 VM 上只能安装一个扩展版本,在同一资源管理器模板中为同一 VM 指定两次自定义脚本将会失败。Only one version of an extension can be installed on a VM at a point in time, specifying custom script twice in the same Resource Manager template for the same VM will fail.

备注

可以在 VirtualMachine 资源内部使用此架构,也可将其用作独立的资源。We can use this schema inside the VirtualMachine resource or as a standalone resource. 如果在 ARM 模板中将此扩展用作独立的资源,则该资源的名称必须采用“virtualMachineName/extensionName”格式。The name of the resource has to be in this format "virtualMachineName/extensionName", if this extension is used as a standalone resource in the ARM template.

属性值Property values

名称Name 值/示例Value / Example 数据类型Data Type
apiVersionapiVersion 2015-06-152015-06-15 datedate
publisherpublisher Microsoft.ComputeMicrosoft.Compute 字符串string
typetype CustomScriptExtensionCustomScriptExtension 字符串string
typeHandlerVersiontypeHandlerVersion 1.101.10 intint
fileUris(例如)fileUris (e.g) https://raw.githubusercontent.com/Microsoft/dotnet-core-sample-templates/master/dotnet-core-music-windows/scripts/configure-music-app.ps1 arrayarray
timestamp(示例)timestamp (e.g) 123456789123456789 32-bit integer32-bit integer
commandToExecute(例如)commandToExecute (e.g) powershell -ExecutionPolicy Unrestricted -File configure-music-app.ps1powershell -ExecutionPolicy Unrestricted -File configure-music-app.ps1 字符串string
storageAccountName(例如)storageAccountName (e.g) examplestorageacctexamplestorageacct 字符串string
storageAccountKey(例如)storageAccountKey (e.g) TmJK/1N3AbAZ3q/+hOXoi/l73zOqsaxXDhqa9Y83/v5UpXQp2DQIBuv2Tifp60cE/OaHsJZmQZ7teQfczQj8hg==TmJK/1N3AbAZ3q/+hOXoi/l73zOqsaxXDhqa9Y83/v5UpXQp2DQIBuv2Tifp60cE/OaHsJZmQZ7teQfczQj8hg== 字符串string
managedIdentity(例如)managedIdentity (e.g) { } 或 { "clientId":"31b403aa-c364-4240-a7ff-d85fb6cd7232" } 或 { "objectId":"12dd289c-0583-46e5-b9b4-115d5c19ef4b" }{ } or { "clientId": "31b403aa-c364-4240-a7ff-d85fb6cd7232" } or { "objectId": "12dd289c-0583-46e5-b9b4-115d5c19ef4b" } json 对象json object

备注

这些属性名称区分大小写。These property names are case-sensitive. 要避免部署问题,请使用如下所示的名称。To avoid deployment problems, use the names as shown here.

属性值详细信息Property value details

  • commandToExecute:( 必需 ,字符串)要执行的入口点脚本。commandToExecute: ( required , string) the entry point script to execute. 如果命令包含机密(例如密码)或者 fileUris 敏感,请改用此字段。Use this field instead if your command contains secrets such as passwords, or your fileUris are sensitive.
  • fileUris:(可选,字符串数组)要下载的文件的 URL。fileUris: (optional, string array) the URLs for file(s) to be downloaded.
  • timestamp(可选,32 位整数)仅当需要更改此字段的值来触发脚本的重新运行时,才使用此字段。timestamp (optional, 32-bit integer) use this field only to trigger a rerun of the script by changing value of this field. 任何整数值都是可以接受的,前提是必须不同于以前的值。Any integer value is acceptable; it must only be different than the previous value.
  • storageAccountName:(可选,字符串)存储帐户的名称。storageAccountName: (optional, string) the name of storage account. 如果指定存储凭据,所有 fileUris 都必须是 Azure Blob 的 URL。If you specify storage credentials, all fileUris must be URLs for Azure Blobs.
  • storageAccountKey:(可选,字符串)存储帐户的访问密钥storageAccountKey: (optional, string) the access key of storage account
  • managedIdentity:(可选,json 对象)用于下载文件的托管标识managedIdentity: (optional, json object) the managed identity for downloading file(s)
    • clientId:(可选,字符串)托管标识的客户端 IDclientId: (optional, string) the client ID of the managed identity
    • objectId:(可选,字符串)托管标识的对象 IDobjectId: (optional, string) the object ID of the managed identity

可以在公共设置或受保护设置中设置以下值,但扩展会拒绝任何同时在公共设置和受保护设置中设置以下值的配置。The following values can be set in either public or protected settings, the extension will reject any configuration where the values below are set in both public and protected settings.

  • commandToExecute

虽然使用公共设置可能对调试很有用,但建议使用受保护设置。Using public settings maybe useful for debugging, but it's recommended that you use protected settings.

公共设置会以明文形式发送到将执行脚本的 VM。Public settings are sent in clear text to the VM where the script will be executed. 受保护设置使用只有 Azure 和 VM 知道的密钥进行加密。Protected settings are encrypted using a key known only to the Azure and the VM. 这些设置会在发送时保存到 VM 中,也就是说,如果设置已加密,则会在 VM 上加密保存。The settings are saved to the VM as they were sent, that is, if the settings were encrypted they're saved encrypted on the VM. 用于对已加密值解密的证书存储在 VM 上,该证书用于在运行时对设置解密(如必要)。The certificate used to decrypt the encrypted values is stored on the VM, and used to decrypt settings (if necessary) at runtime.

属性:managedIdentityProperty: managedIdentity

备注

此属性只能在受保护的设置中指定。This property must be specified in protected settings only.

CustomScript(版本 1.10 及更高版本)支持用于通过“fileUris”设置中提供的 URL 下载文件的托管标识CustomScript (version 1.10 onwards) supports managed identity for downloading file(s) from URLs provided in the "fileUris" setting. 它允许 CustomScript 访问 Azure 存储专用 Blob 或容器,而无需用户传递 SAS 令牌或存储帐户密钥等机密。It allows CustomScript to access Azure Storage private blobs or containers without the user having to pass secrets like SAS tokens or storage account keys.

若要使用此功能,用户必须将系统分配的用户分配的标识添加到需要运行 CustomScript 的 VM 或 VMSS,并授予托管标识访问 Azure 存储容器或 Blob 的权限To use this feature, the user must add a system-assigned or user-assigned identity to the VM or VMSS where CustomScript is expected to run, and grant the managed identity access to the Azure Storage container or blob.

若要在目标 VM/VMSS 上使用系统分配的标识,请将“managedidentity”字段设置为空的 json 对象。To use the system-assigned identity on the target VM/VMSS, set "managedidentity" field to an empty json object.

示例:Example:

{
  "fileUris": ["https://mystorage.blob.core.chinacloudapi.cn/privatecontainer/script1.ps1"],
  "commandToExecute": "powershell.exe script1.ps1",
  "managedIdentity" : {}
}

若要在目标 VM/VMSS 上使用用户分配的标识,请将“managedidentity”字段配置为托管标识的客户端 ID 或对象 ID。To use the user-assigned identity on the target VM/VMSS, configure "managedidentity" field with the client ID or the object ID of the managed identity.

示例:Examples:

{
  "fileUris": ["https://mystorage.blob.core.chinacloudapi.cn/privatecontainer/script1.ps1"],
  "commandToExecute": "powershell.exe script1.ps1",
  "managedIdentity" : { "clientId": "31b403aa-c364-4240-a7ff-d85fb6cd7232" }
}
{
  "fileUris": ["https://mystorage.blob.core.chinacloudapi.cn/privatecontainer/script1.ps1"],
  "commandToExecute": "powershell.exe script1.ps1",
  "managedIdentity" : { "objectId": "12dd289c-0583-46e5-b9b4-115d5c19ef4b" }
}

备注

managedIdentity 属性 不能 与 storageAccountName 或 storageAccountKey 属性结合使用managedIdentity property must not be used in conjunction with storageAccountName or storageAccountKey properties

模板部署Template deployment

可使用 Azure Resource Manager 模板部署 Azure VM 扩展。Azure VM extensions can be deployed with Azure Resource Manager templates. 可以在 Azure 资源管理器模板中使用上一部分中详细介绍的 JSON 架构,以便在部署过程中运行自定义脚本扩展。The JSON schema, which is detailed in the previous section can be used in an Azure Resource Manager template to run the Custom Script Extension during deployment. 以下示例显示如何使用自定义脚本扩展:The following samples show how to use the Custom Script extension:

PowerShell 部署PowerShell deployment

可以使用 Set-AzVMCustomScriptExtension 命令将自定义脚本扩展添加到现有虚拟机。The Set-AzVMCustomScriptExtension command can be used to add the Custom Script extension to an existing virtual machine. 有关详细信息,请参阅 Set-AzVMCustomScriptExtensionFor more information, see Set-AzVMCustomScriptExtension.

Set-AzVMCustomScriptExtension -ResourceGroupName <resourceGroupName> `
    -VMName <vmName> `
    -Location myLocation `
    -FileUri <fileUrl> `
    -Run 'myScript.ps1' `
    -Name DemoScriptExtension

其他示例Additional examples

使用多个脚本Using multiple scripts

在本示例中,你有三个用于构建服务器的脚本。In this example, you have three scripts that are used to build your server. commandToExecute 调用第一个脚本,然后可以选择调用其他脚本的方式。The commandToExecute calls the first script, then you have options on how the others are called. 例如,可以使用主脚本来控制执行,其中包含正确的错误处理、日志记录和状态管理。For example, you can have a master script that controls the execution, with the right error handling, logging, and state management. 将脚本下载到本地计算机以运行。The scripts are downloaded to the local machine for running. 例如,在 1_Add_Tools.ps1 中,可以通过在脚本中添加 .\2_Add_Features.ps1 来调用 2_Add_Features.ps1,并对在 $settings 中定义的其他脚本重复此过程。For example in 1_Add_Tools.ps1 you would call 2_Add_Features.ps1 by adding .\2_Add_Features.ps1 to the script, and repeat this process for the other scripts you define in $settings.

$fileUri = @("https://xxxxxxx.blob.core.chinacloudapi.cn/buildServer1/1_Add_Tools.ps1",
"https://xxxxxxx.blob.core.chinacloudapi.cn/buildServer1/2_Add_Features.ps1",
"https://xxxxxxx.blob.core.chinacloudapi.cn/buildServer1/3_CompleteInstall.ps1")

$settings = @{"fileUris" = $fileUri};

$storageAcctName = "xxxxxxx"
$storageKey = "1234ABCD"
$protectedSettings = @{"storageAccountName" = $storageAcctName; "storageAccountKey" = $storageKey; "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File 1_Add_Tools.ps1"};

#run command
Set-AzVMExtension -ResourceGroupName <resourceGroupName> `
    -Location <locationName> `
    -VMName <vmName> `
    -Name "buildserver1" `
    -Publisher "Microsoft.Compute" `
    -ExtensionType "CustomScriptExtension" `
    -TypeHandlerVersion "1.10" `
    -Settings $settings    `
    -ProtectedSettings $protectedSettings `

从本地共享运行脚本Running scripts from a local share

在本示例中,建议使用本地 SMB 服务器作为脚本位置。In this example, you may want to use a local SMB server for your script location. 这样就不需要提供任何其他设置,但 commandToExecute 除外。By doing this, you don't need to provide any other settings, except commandToExecute .

$protectedSettings = @{"commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File \\filesvr\build\serverUpdate1.ps1"};

Set-AzVMExtension -ResourceGroupName <resourceGroupName> `
    -Location <locationName> `
    -VMName <vmName> `
    -Name "serverUpdate"
    -Publisher "Microsoft.Compute" `
    -ExtensionType "CustomScriptExtension" `
    -TypeHandlerVersion "1.10" `
    -ProtectedSettings $protectedSettings

如何使用 CLI 多次运行自定义脚本How to run custom script more than once with CLI

如果想要多次运行自定义脚本扩展,则只能在以下条件下执行此操作:If you want to run the custom script extension more than once, you can only do this action under these conditions:

  • 扩展的“Name”参数与之前部署的扩展相同。The extension Name parameter is the same as the previous deployment of the extension.
  • 请更新配置,否则不会重新执行命令。Update the configuration otherwise the command won't be re-executed. 可以将动态属性添加到命令中,如时间戳。You can add in a dynamic property into the command, such as a timestamp.

或者,可以将 ForceUpdateTag 属性设置为“true”。Alternatively, you can set the ForceUpdateTag property to true .

使用 Invoke-WebRequestUsing Invoke-WebRequest

如果在脚本中使用 Invoke-WebRequest,则必须指定参数 -UseBasicParsing,否则你将在检查详细信息状态时收到以下错误:If you are using Invoke-WebRequest in your script, you must specify the parameter -UseBasicParsing or else you will receive the following error when checking the detailed status:

The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer's first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again.

虚拟机规模集Virtual Machine Scale Sets

要在规模集上部署自定义脚本扩展,请参阅 Add-AzVmssExtensionTo deploy the Custom Script Extension on a Scale Set, see Add-AzVmssExtension

经典 VMClassic VMs

重要

经典 VM 将于 2023 年 3 月 1 日停用。Classic VMs will be retired on March 1, 2023.

如果从 ASM 使用 IaaS 资源,请在 2023 年 3 月 1 日之前完成迁移。If you use IaaS resources from ASM, please complete your migration by March 1, 2023. 我们建议你尽快进行切换,以利用 Azure 资源管理器中的许多增强功能。We encourage you to make the switch sooner to take advantage of the many feature enhancements in Azure Resource Manager.

有关详细信息,请参阅在 2023 年 3 月 1 日之前将 IaaS 资源迁移到 Azure 资源管理器For more information, see Migrate your IaaS resources to Azure Resource Manager by March 1, 2023.

要在经典 VM 上部署自定义脚本扩展,可以使用 Azure 门户或经典 Azure PowerShell cmdlet。To deploy the Custom Script Extension on classic VMs, you can use the Azure portal or the Classic Azure PowerShell cmdlets.

Azure 门户Azure portal

导航到经典 VM 资源。Navigate to your Classic VM resource. 选择“设置”下面的“扩展” 。Select Extensions under Settings .

单击“+ 添加”,并在资源列表中选择“自定义脚本扩展” 。Click + Add and in the list of resources choose Custom Script Extension .

在“安装扩展”页上,选择本地 PowerShell 文件,然后填写任何参数并单击“确定” 。On the Install extension page, select the local PowerShell file, and fill out any arguments and click Ok .

PowerShellPowerShell

可使用 Set-AzureVMCustomScriptExtension cmdlet 将自定义脚本扩展添加到现有虚拟机。Use the Set-AzureVMCustomScriptExtension cmdlet can be used to add the Custom Script extension to an existing virtual machine.

# define your file URI
$fileUri = 'https://xxxxxxx.blob.core.chinacloudapi.cn/scripts/Create-File.ps1'

# create vm object
$vm = Get-AzureVM -Name <vmName> -ServiceName <cloudServiceName>

# set extension
Set-AzureVMCustomScriptExtension -VM $vm -FileUri $fileUri -Run 'Create-File.ps1'

# update vm
$vm | Update-AzureVM

故障排除和支持Troubleshoot and support

疑难解答Troubleshoot

有关扩展部署状态的数据可以从 Azure 门户和使用 Azure PowerShell 模块进行检索。Data about the state of extension deployments can be retrieved from the Azure portal, and by using the Azure PowerShell module. 若要查看给定 VM 的扩展部署状态,请运行以下命令:To see the deployment state of extensions for a given VM, run the following command:

Get-AzVMExtension -ResourceGroupName <resourceGroupName> -VMName <vmName> -Name myExtensionName

扩展输出将记录到可在目标虚拟机上的以下目录中找到的文件中。Extension output is logged to files found under the following folder on the target virtual machine.

C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension

指定的文件将下载到目标虚拟机上的以下文件夹中。The specified files are downloaded into the following folder on the target virtual machine.

C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.*\Downloads\<n>

其中,<n> 是一个十进制整数,可以在不同的扩展执行之间更改。where <n> is a decimal integer, which may change between executions of the extension. 1.* 值与扩展的 typeHandlerVersion 的当前实际值匹配。The 1.* value matches the actual, current typeHandlerVersion value of the extension. 例如,实际目录可能是 C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.8\Downloads\2For example, the actual directory could be C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.8\Downloads\2.

执行 commandToExecute 命令时,扩展会将该目录(例如 ...\Downloads\2)设置为当前工作目录。When executing the commandToExecute command, the extension sets this directory (for example, ...\Downloads\2) as the current working directory. 此过程使得可以通过 fileURIs 属性使用相对路径查找下载的文件。This process enables the use of relative paths to locate the files downloaded via the fileURIs property. 请参阅下表中的示例。See the table below for examples.

绝对下载路径可能会随时间而变化,因此在可能情况下,最好是在 commandToExecute 字符串中选择使用相对的脚本/文件路径。Since the absolute download path may vary over time, it's better to opt for relative script/file paths in the commandToExecute string, whenever possible. 例如:For example:

"commandToExecute": "powershell.exe . . . -File \"./scripts/myscript.ps1\""

对于通过 fileUris 属性列表下载的文件,将保留第一个 URI 段之后的路径信息。Path information after the first URI segment is kept for files downloaded via the fileUris property list. 如下表所示,下载的文件映射到下载子目录中,以便反映 fileUris 值的结构。As shown in the table below, downloaded files are mapped into download subdirectories to reflect the structure of the fileUris values.

下载的文件的示例Examples of Downloaded Files

fileUris 中的 URIURI in fileUris 相对下载位置Relative downloaded location 绝对下载位置 1Absolute downloaded location 1
https://someAcct.blob.core.chinacloudapi.cn/aContainer/scripts/myscript.ps1 ./scripts/myscript.ps1 C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.8\Downloads\2\scripts\myscript.ps1
https://someAcct.blob.core.chinacloudapi.cn/aContainer/topLevel.ps1 ./topLevel.ps1 C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.8\Downloads\2\topLevel.ps1

1 绝对目录路径可在 VM 的生存期内更改,但不可在 CustomScript 扩展的某次执行期间更改。1 The absolute directory paths change over the lifetime of the VM, but not within a single execution of the CustomScript extension.

支持Support

如果对本文中的任何观点存在疑问,可以联系 Azure 支持上的 Azure 专家。If you need more help at any point in this article, you can contact the Azure experts on the Azure support. 还可以提出 Azure 支持事件。You can also file an Azure support incident. 请转到 Azure 支持站点提交请求。Go to the Azure support site and submit your request. 有关使用 Azure 支持的信息,请阅读 Azure 支持常见问题For information about using Azure Support, read the Azure support FAQ.