我正在尝试创建基于GUI的脚本,以便可以轻松地从列表视图中选择与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
}
答案 0 :(得分:0)
更新
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
请让我知道是否还有其他问题。