如何使用.net SDK创建Azure CustomScriptExtension?

时间:2017-02-15 22:40:20

标签: azure-virtual-machine azure-sdk-.net

用于自定义脚本扩展的powershell命令行开关似乎支持一堆不错的参数选项,但用于VM扩展的.net客户端API并没有配置这么好的强类型参数集,并且需要使用这些publicSettings / privateSettings的东西...用.net API做正确的方法吗?

1 个答案:

答案 0 :(得分:0)

使用新的流畅的SDK这是我迄今为止最好的尝试......:

var blobClient = new CloudBlobClient(
    new Uri(tenantStorageAccount.EndPoints.Primary.Blob),
    new StorageCredentials(tenantStorageAccount.Name, tenantStorageKeys.First().Value));

var scriptText = @"Write-Host ""Success!""

&#34 ;;

var bootContainer = blobClient.GetContainerReference("boot");
await bootContainer.CreateIfNotExistsAsync();
var bootstrapBlob = bootContainer.GetBlockBlobReference("bootstrap.ps1");
await bootstrapBlob.UploadTextAsync(scriptText);

var sasToken = bootstrapBlob.GetSharedAccessSignature(
   new SharedAccessBlobPolicy
   {
       SharedAccessExpiryTime = DateTime.UtcNow.AddYears(1),
       Permissions = SharedAccessBlobPermissions.Read
   });

string bootstrapBlobSasUri = bootstrapBlob.Uri + sasToken;

var extensionTasks = new List<Task<IVirtualMachine>>();
await vms.ForEachAsync(async vm =>
{
    await vm.Update().DefineNewExtension("bootstrapper")
       .WithPublisher("Microsoft.Compute")
       .WithType("CustomScriptExtension")
       .WithVersion("1.8")
       .WithPublicSetting("fileUris", new string[] { bootstrapBlobSasUri })
       .WithProtectedSetting("storageAccountName", tenantStorageAccount.Name)
       .WithProtectedSetting("storageAccountKey", tenantStorageKeys[0])
       .WithProtectedSetting("commandToExecute", "powershell -ExecutionPolicy Unrestricted -File bootstrap.ps1")
       .Attach().ApplyAsync();
});