2017-02-15 38 views
0

自定義腳本擴展有一個powershell命令行程序似乎支持一堆很好的參數選項,但用於VM擴展的.net客戶端API沒有這樣一個很好的強類型參數集來配置,並且需要使用這些publicSettings/privateSettings的東西...什麼是正確的方式來與.net APIs做到這一點?如何使用.net SDK創建Azure CustomScriptExtension?

回答

0

使用新的流暢SDK這是我最好的嘗試,到目前爲止...:

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

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

「;

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(); 
}); 
相關問題