【问题标题】:Azure Storage GuiAzure 存储 GUI
【发布时间】:2019-05-13 01:48:39
【问题描述】:

我正在尝试创建一个基于 GUI 的脚本,以便我可以轻松地选择与从 List View 选择的 Web 界面相同的内容。尝试使用 powershell 来实现这一点。我想直接使用我选择的位置创建 azure 资源组。同样的方法也适用于创建 Azure 存储帐户。让我知道是否有任何资源/指向网站,我可以在其中检查已经为 Azure 实现此类选项的示例脚本。 我尝试在参数下硬编码我需要的所有值作为 power shell 脚本的一部分,但希望在 GUI 中使用它。 任何指针都会非常有帮助。我尝试使用 Windows 表单 一直抛出错误的新对象 System.Windows.Forms.Label 。

我尝试了以下方法,但表单加载但无法使用我选择的参数:

Add-Type -Assembly Name System.Windows.Forms
Add-Type -Assembly Name 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.Start Position = 'Center Screen'
$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
[void] $listBox.Items.Add('West Europe')
[void] $listBox.Items.Add('East US')
[void] $listBox.Items.Add('West US')
$form.Controls.Add($listBox)
$form.Topmost = $true
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult   ]::OK)
{
 New-AzureRmResourceGroup -Name $resourceGroupName -Location 
 $resourceGroupLocation
 }   

【问题讨论】:

  • 你的问题解决了吗?
  • No Ivan 我确实重试了,但仍然遇到未知异常资源未找到错误的问题。下面的代码在尝试添加更多行时独立工作,它会引发错误。你能添加你的完整示例代码吗/?
  • 我添加了用于创建资源组的完整代码,如果您愿意,可以在 powershell ISE 中运行该代码。如果您有任何问题,请告诉我。如果它有效,请帮助将其标记为答案。谢谢:)

标签: azure-storage


【解决方案1】:

更新

 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 "your_resource_group_name" -Location $selectedLocation
 }

如果你使用powershell,你只需要对你的代码做一些修改。

1.使用Get-AzureRmLocation | select DisplayName获取所有位置,然后将所有位置添加到列表框:

$mylocations = Get-AzureRmLocation | select DisplayName

foreach($location in $mylocations){[void]$listBox.Items.Add($location.displayname)}

2.在对话框中选择位置后,使用$listBox.SelectedItem获取选择的位置。

$selectedLocation = $listBox.SelectedItem

#use the selected location to create resource group
New-AzureRmResourceGroup -Name $resourceGroupName -Location $selectedLocation 

如果还有问题请告诉我。

【讨论】:

  • 嗨,Ivan,当我尝试用于位置时,它工作得很好,我正在尝试将订阅和资源组也添加到下拉列表中,然后添加一个占位符以输入“存储”帐户”,我尝试使用 ~~~~ $mysubscriptions = Get-AzureRmSubscription |选择 DisplayName ~~~~ 还想将“your_resource_group_name”作为文本传递,而不是硬编码我使用 Input Text 将其读取为 Read-Host,如何将这些值传递给单个选择
  • @Viswanath,您介意为此发布一个新问题吗?并告诉我链接,谢谢:)。
  • 在此stackoverflow.com/questions/56124581/…的链接中发布了新问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-16
  • 2017-11-10
  • 1970-01-01
  • 1970-01-01
  • 2016-06-16
  • 1970-01-01
  • 2023-03-07
相关资源
最近更新 更多