【问题标题】:Extending Azure Storage Gui扩展 Azure 存储 GUI
【发布时间】:2019-05-15 13:43:52
【问题描述】:

尝试扩展现有的 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() 

【问题讨论】:

  • 环境标签是什么?它的位置?据我了解,订阅/资源组/环境(不确定是否意味着位置)/已经存在,对吗?并且只需要创建存储帐户?

标签: azure-storage


【解决方案1】:

更新:(如果您有超过 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.如果您有超过1个订阅,您应该根据所选订阅将资源组等值动态添加到组合框中(选择订阅后,使用powershell命令获取其中的所有资源组,然后添加到组合框)。

【讨论】:

  • 感谢 Ivan 的帮助,我们如何获取订阅和资源组的值并将它们放在下拉列表中而不是硬编码?下面的选项对我不起作用~~~ @('Get-AzResourceGroup') | ForEach-Object {[void] $ComboBox1.Items.Add($_)} ~~~
  • 我稍后会更新代码以获取它们。如果有帮助,请您帮忙将其标记为答案吗?谢谢。
  • @Viswanath,请参阅更新的部分。我使用的是 az 模块,如果您使用的是 azurerm 模块,请改用 Get-AzureRmResourceGroup / Get-AzureRmSubscription。
猜你喜欢
  • 1970-01-01
  • 2017-03-26
  • 2012-09-05
  • 1970-01-01
  • 2017-03-29
  • 1970-01-01
  • 2014-02-05
  • 1970-01-01
  • 2021-09-28
相关资源
最近更新 更多