Extending Azure Storage Gui

孤街醉人 提交于 2020-03-05 11:46:08

问题


Trying to extend existing GUI code to be able to select Azure Subscription and resource group . This need to create a Storage and Blob Storage Account. How do i add the function to create both storage and blob storage account for the account given in the input box ?

 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
 }

Using the above code it creates the resource group i'm trying to add additional parameters as Menu list like Subscription Name , Resource Group , Location , below is the sample code i have created how do i get the actions to be perfromed


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() 


回答1:


Update:(please also take a look at my Note section if you have more than 1 subscription)

For subscription:

$subid = Get-AzSubscription | select Name
$subid | ForEach-Object{[void] $ComboBox2.Items.Add($_.name)}

For resource group:

$myrg = Get-AzResourceGroup | select ResourceGroupName
$myrg | foreach{[void] $ComboBox1.Items.Add($_.ResourceGroupName)}

You should create a onclick function like $Create_OnClick ={your code to create storage account}, then add it to Add_Click() like $Create.Add_Click($Create_OnClick).

The sample code as below:

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() 

Test result:

Note:

1.when create storage account, you should provide a skuname which has the following values: Standard_LRS, Standard_ZRS, Standard_GRS, Standard_RAGRS, Premium_LRS.

In the sample, I just hardcoded it. You can create another combobox includes all the values, then use .SelectedItem to fetch the value.

2.If you have more than 1 subscription, you should add the values like resource group dynamically to the combobox as per the selected subscription(after selected a subscription, use powershell command to get all the resource groups in it, then add to combobox).



来源:https://stackoverflow.com/questions/56124581/extending-azure-storage-gui

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!