尝试扩展现有的GUI代码以能够选择Azure订阅和资源组。这需要创建一个存储和Blob存储帐户。 如何为输入框中指定的帐户添加创建存储帐户和Blob存储帐户的功能?
Add-Type -AssemblyName system.drawing
$form = New-Object System.Windows.Forms.Form
$form.Text ='Select a Resource Group'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition ='CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text ='Please select a Resource group'
$form.Controls.Add($label)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height=80
#get all the available location
$mylocations = Get-AzureRmLocation | select DisplayName
foreach($location in $mylocations){[void]$listBox.Items.Add($location.displayname)}
$form.Controls.Add($listBox)
$form.TopMost = $true
$result = $form.ShowDialog()
$selectedLocation = $listBox.SelectedItem
Write-Output $selectedLocation
if($result -eq [System.Windows.Forms.DialogResult]::OK)
{
New-AzureRmResourceGroup -Name "resource_group_name" -Location $selectedLocation
}
使用上面的代码,它创建了我要添加的资源组 诸如菜单列表之类的其他参数,例如“订阅名称”,“资源组”,“位置”,下面是我创建的示例代码,我该如何获取要执行的操作
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '364,393'
$Form.text = "Form"
$Form.TopMost = $false
$subscriptionName = New-Object system.Windows.Forms.Label
$subscriptionName.text = "Azure Subscription"
$subscriptionName.AutoSize = $true
$subscriptionName.width = 25
$subscriptionName.height = 10
$subscriptionName.location = New-Object System.Drawing.Point(13,107)
$subscriptionName.Font = 'Microsoft Sans Serif,10'
$resourceGroupName = New-Object system.Windows.Forms.Label
$resourceGroupName.text = "ResourceGroup"
$resourceGroupName.AutoSize = $true
$resourceGroupName.width = 25
$resourceGroupName.height = 10
$resourceGroupName.location = New-Object System.Drawing.Point(17,139)
$resourceGroupName.Font = 'Microsoft Sans Serif,10'
$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline = $false
$TextBox1.width = 199
$TextBox1.height = 20
$TextBox1.location = New-Object System.Drawing.Point(136,190)
$TextBox1.Font = 'Microsoft Sans Serif,10'
$Create = New-Object system.Windows.Forms.Button
$Create.text = "Create"
$Create.width = 86
$Create.height = 30
$Create.location = New-Object System.Drawing.Point(137,300)
$Create.Font = 'Microsoft Sans Serif,10'
$Cancel = New-Object system.Windows.Forms.Button
$Cancel.text = "Cancel"
$Cancel.width = 60
$Cancel.height = 30
$Cancel.location = New-Object System.Drawing.Point(257,299)
$Cancel.Font = 'Microsoft Sans Serif,10'
$Storageaccountname = New-Object system.Windows.Forms.Label
$Storageaccountname.text = "StorageAccount"
$Storageaccountname.AutoSize = $true
$Storageaccountname.width = 25
$Storageaccountname.height = 10
$Storageaccountname.location = New-Object System.Drawing.Point(19,192)
$Storageaccountname.Font = 'Microsoft Sans Serif,10'
$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text = ""
$ComboBox1.width = 199
$ComboBox1.height = 20
@('ResourceGroup1','Resource Group2','Resource Group 3','Resource Group 4') | ForEach-Object {[void] $ComboBox1.Items.Add($_)}
$ComboBox1.location = New-Object System.Drawing.Point(135,131)
$ComboBox1.Font = 'Microsoft Sans Serif,10'
$ComboBox2 = New-Object system.Windows.Forms.ComboBox
$ComboBox2.text = ""
$ComboBox2.width = 199
$ComboBox2.height = 20
@('abcd','efgh','ijk') | ForEach-Object {[void] $ComboBox2.Items.Add($_)}
$ComboBox2.location = New-Object System.Drawing.Point(135,102)
$ComboBox2.Font = 'Microsoft Sans Serif,10'
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Environment"
$Label1.AutoSize = $true
$Label1.width = 25
$Label1.height = 10
$Label1.location = New-Object System.Drawing.Point(17,165)
$Label1.Font = 'Microsoft Sans Serif,10'
$ComboBox3 = New-Object system.Windows.Forms.ComboBox
$ComboBox3.text = ""
$ComboBox3.width = 198
$ComboBox3.height = 20
@('A','B','C') | ForEach-Object {[void] $ComboBox3.Items.Add($_)}
$ComboBox3.location = New-Object System.Drawing.Point(135,160)
$ComboBox3.Font = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(@($subscriptionName,$resourceGroupName,$TextBox1,$Create,$Cancel,$Storageaccountname,$ComboBox1,$ComboBox2,$Label1,$ComboBox3))
$Create.Add_Click({ create_storage$this $_ })
$Form.ShowDialog()
答案 0 :(得分:0)
更新:(如果您有1个以上的订阅,还请查看我的注释部分)
订阅:
$subid = Get-AzSubscription | select Name
$subid | ForEach-Object{[void] $ComboBox2.Items.Add($_.name)}
对于资源组:
$myrg = Get-AzResourceGroup | select ResourceGroupName
$myrg | foreach{[void] $ComboBox1.Items.Add($_.ResourceGroupName)}
您应创建一个$Create_OnClick ={your code to create storage account}
之类的onclick函数,然后将其添加至$Create.Add_Click($Create_OnClick)
之类的Add_Click()中。
示例代码如下:
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '364,393'
$Form.text = "Form"
$Form.TopMost = $false
$subscriptionName = New-Object system.Windows.Forms.Label
$subscriptionName.text = "Azure Subscription"
$subscriptionName.AutoSize = $true
$subscriptionName.width = 25
$subscriptionName.height = 10
$subscriptionName.location = New-Object System.Drawing.Point(13,107)
$subscriptionName.Font = 'Microsoft Sans Serif,10'
$resourceGroupName = New-Object system.Windows.Forms.Label
$resourceGroupName.text = "ResourceGroup"
$resourceGroupName.AutoSize = $true
$resourceGroupName.width = 25
$resourceGroupName.height = 10
$resourceGroupName.location = New-Object System.Drawing.Point(17,139)
$resourceGroupName.Font = 'Microsoft Sans Serif,10'
#input the storage account name
$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline = $false
$TextBox1.width = 199
$TextBox1.height = 20
$TextBox1.location = New-Object System.Drawing.Point(136,190)
$TextBox1.Font = 'Microsoft Sans Serif,10'
$Create = New-Object system.Windows.Forms.Button
$Create.text = "Create"
$Create.width = 86
$Create.height = 30
$Create.location = New-Object System.Drawing.Point(137,300)
$Create.Font = 'Microsoft Sans Serif,10'
$Cancel = New-Object system.Windows.Forms.Button
$Cancel.text = "Cancel"
$Cancel.width = 60
$Cancel.height = 30
$Cancel.location = New-Object System.Drawing.Point(257,299)
$Cancel.Font = 'Microsoft Sans Serif,10'
$Storageaccountname = New-Object system.Windows.Forms.Label
$Storageaccountname.text = "StorageAccount"
$Storageaccountname.AutoSize = $true
$Storageaccountname.width = 25
$Storageaccountname.height = 10
$Storageaccountname.location = New-Object System.Drawing.Point(19,192)
$Storageaccountname.Font = 'Microsoft Sans Serif,10'
#resource group
$ComboBox1 = New-Object system.Windows.Forms.ComboBox
$ComboBox1.text = ""
$ComboBox1.width = 199
$ComboBox1.height = 20
@('ivanrg2','Resource Group2','ivanrg','Resource Group 4') | ForEach-Object {[void] $ComboBox1.Items.Add($_)}
$ComboBox1.location = New-Object System.Drawing.Point(135,131)
$ComboBox1.Font = 'Microsoft Sans Serif,10'
#subscription
$ComboBox2 = New-Object system.Windows.Forms.ComboBox
$ComboBox2.text = ""
$ComboBox2.width = 199
$ComboBox2.height = 20
@('abcd','efgh','ijk') | ForEach-Object {[void] $ComboBox2.Items.Add($_)}
$ComboBox2.location = New-Object System.Drawing.Point(135,102)
$ComboBox2.Font = 'Microsoft Sans Serif,10'
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "Environment"
$Label1.AutoSize = $true
$Label1.width = 25
$Label1.height = 10
$Label1.location = New-Object System.Drawing.Point(17,165)
$Label1.Font = 'Microsoft Sans Serif,10'
#environment(location)
$ComboBox3 = New-Object system.Windows.Forms.ComboBox
$ComboBox3.text = ""
$ComboBox3.width = 198
$ComboBox3.height = 20
@('A','eastus','C') | ForEach-Object {[void] $ComboBox3.Items.Add($_)}
$ComboBox3.location = New-Object System.Drawing.Point(135,160)
$ComboBox3.Font = 'Microsoft Sans Serif,10'
#when create storage account, you need the skuname
$skuName ="Standard_LRS"
$Form.controls.AddRange(@($subscriptionName,$resourceGroupName,$TextBox1,$Create,$Cancel,$Storageaccountname,$ComboBox1,$ComboBox2,$Label1,$ComboBox3))
$Create_OnClick =
{
#switch the subscription based on your choice
Set-AzureRmContext -SubscriptionName $ComboBox2.SelectedItem
New-AzureRmStorageAccount -ResourceGroupName $ComboBox1.SelectedItem -Name $TextBox1.text -Location $ComboBox3.SelectedItem -SkuName $skuName
}
$Create.Add_Click($Create_OnClick)
$Form.ShowDialog()
测试结果:
注意:
1。创建存储帐户时,应提供一个具有以下值的skuname:Standard_LRS,Standard_ZRS,Standard_GRS,Standard_RAGRS,Premium_LRS。
在示例中,我只是对其进行了硬编码。您可以创建另一个包含所有值的组合框,然后使用.SelectedItem获取值。
2。如果您有多个订阅,则应根据所选订阅将诸如资源组之类的值动态添加到组合框(选择订阅后,使用powershell命令获取其中的所有资源组,然后添加到组合框)。