gpt4 book ai didi

azure - 垂直排列的 Powershell GUI 按钮

转载 作者:行者123 更新时间:2023-12-02 08:33:04 24 4
gpt4 key购买 nike

我编写了一个与 ISE 启动一起运行的 PS 函数,因此我可以调用它并通过单击按钮切换 Azure 订阅,但 GUI 中的按钮是水平列出的,如何将其更改为垂直显示?

这是我的代码:

using namespace System.Management.Automation.Host
$sub = get-azsubscription | where-object -property State -eq 'Enabled'

function Set-Sub {
$Title = "Powershell ISE"
$Question = "Select default subscription"

$0 = [ChoiceDescription]::new($sub.Name[0], $sub.Name[0])
$1 = [ChoiceDescription]::new($sub.Name[1], $sub.Name[1])
$2 = [ChoiceDescription]::new($sub.Name[2], $sub.Name[2])
$3 = [ChoiceDescription]::new($sub.Name[3], $sub.Name[3])
$4 = [ChoiceDescription]::new($sub.Name[4], $sub.Name[4])

$options = [ChoiceDescription[]]($0, $1, $2, $3, $4)

$result = $host.ui.PromptForChoice($Title, $Question, $options, 0)

switch ($result) {
0 {
Write-Host -BackgroundColor Green -ForegroundColor White ("Setting default subscription to "+$sub.Name[0])
select-azsubscription -subscriptionname $sub.Name[0]
}
1 {
Write-Host -BackgroundColor Green -ForegroundColor White ("Setting default subscription to "+$sub.Name[1])
select-azsubscription -subscriptionname $sub.Name[1]
}
2 {
Write-Host -BackgroundColor Green -ForegroundColor White ("Setting default subscription to "+$sub.Name[2])
select-azsubscription -subscriptionname $sub.Name[2]
}
3 {
Write-Host -BackgroundColor Green -ForegroundColor White ("Setting default subscription to "+$sub.Name[3])
select-azsubscription -subscriptionname $sub.Name[3]
}
4 {
Write-Host -BackgroundColor Green -ForegroundColor White ("Setting default subscription to "+$sub.Name[4])
select-azsubscription -subscriptionname $sub.Name[4]
}
}

}

$context = Get-AzContext
Write-Host -BackgroundColor Red -ForegroundColor White ("Currently used subscription is: "+$context.Name)
Write-Host -ForegroundColor White (" ")

这就是 GUI 的样子:

enter image description here

最佳答案

如果您正在寻找一种使其垂直选择的方法,目前不可能,尽管您可以采取不同的方式来提示每个选项,以便最终用户可以选择它:代码如下所示:

$t = "Location"
$msg = "What location?"
$CFT = New-Object System.Management.Automation.Host.ChoiceDescription "&A choice 1", "1"
[Environment]::NewLine
$CON = New-Object System.Management.Automation.Host.ChoiceDescription "&B choice 2", "2"
$ELP = New-Object System.Management.Automation.Host.ChoiceDescription "&C choice 3", "3"
$CFT1 = New-Object System.Management.Automation.Host.ChoiceDescription "&A1 choice 1", "4"
$CON1 = New-Object System.Management.Automation.Host.ChoiceDescription "&B1 choice 2", "5"
$ELP1 = New-Object System.Management.Automation.Host.ChoiceDescription "&C1 choice 3", "6"
[int]$defaultchoice = 2
$options = [System.Management.Automation.Host.ChoiceDescription[]]($CFT, $CON, $ELP,$CFT1, $CON1, $ELP1)
$result = $host.ui.PromptForChoice($t, $msg, $options[0], 0)
$result = $host.ui.PromptForChoice($t, $msg, $options[1], 0)

这种方法的主要问题是它一次会显示一个菜单并带有不同的提示框。

但是为了让 PowerShell Console Host 更好的 PromptForChoice ,您可以像下面这样使用它:

内置 PromptForChoice 的替代方案,在不同主机之间提供一致的 UI

function Get-Choice {
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,Position=0)]
$Title,

[Parameter(Mandatory=$true,Position=1)]
[String[]]
$Options,

[Parameter(Position=2)]
$DefaultChoice = -1
)
if ($DefaultChoice -ne -1 -and ($DefaultChoice -gt $Options.Count -or $DefaultChoice -lt 1)){
Write-Warning "DefaultChoice needs to be a value between 1 and $($Options.Count) or -1 (for none)"
exit
}
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()
$script:result = ""
$form = New-Object System.Windows.Forms.Form
$form.FormBorderStyle = [Windows.Forms.FormBorderStyle]::FixedDialog
$form.BackColor = [Drawing.Color]::White
$form.TopMost = $True
$form.Text = $Title
$form.ControlBox = $False
$form.StartPosition = [Windows.Forms.FormStartPosition]::CenterScreen
#calculate width required based on longest option text and form title
$minFormWidth = 100
$formHeight = 44
$minButtonWidth = 70
$buttonHeight = 23
$buttonY = 12
$spacing = 10
$buttonWidth = [Windows.Forms.TextRenderer]::MeasureText((($Options | sort Length)[-1]),$form.Font).Width + 1
$buttonWidth = [Math]::Max($minButtonWidth, $buttonWidth)
$formWidth = [Windows.Forms.TextRenderer]::MeasureText($Title,$form.Font).Width
$spaceWidth = ($options.Count+1) * $spacing
$formWidth = ($formWidth, $minFormWidth, ($buttonWidth * $Options.Count + $spaceWidth) | Measure-Object -Maximum).Maximum
$form.ClientSize = New-Object System.Drawing.Size($formWidth,$formHeight)
$index = 0
#create the buttons dynamically based on the options
foreach ($option in $Options){
Set-Variable "button$index" -Value (New-Object System.Windows.Forms.Button)
$temp = Get-Variable "button$index" -ValueOnly
$temp.Size = New-Object System.Drawing.Size($buttonWidth,$buttonHeight)
$temp.UseVisualStyleBackColor = $True
$temp.Text = $option
$buttonX = ($index + 1) * $spacing + $index * $buttonWidth
$temp.Add_Click({
$script:result = $this.Text; $form.Close()
})
$temp.Location = New-Object System.Drawing.Point($buttonX,$buttonY)
$form.Controls.Add($temp)
$index++
}
$shownString = '$this.Activate();'
if ($DefaultChoice -ne -1){
$shownString += '(Get-Variable "button$($DefaultChoice-1)" -ValueOnly).Focus()'
}
$shownSB = [ScriptBlock]::Create($shownString)
$form.Add_Shown($shownSB)
[void]$form.ShowDialog()
$result
}

通过这种方式,您可以更好地控制表单 UI,您可以放置​​一个空白文本 block 来创建新行,并且在循环中您可以创建不同的按钮控件。

其他引用:

https://gist.github.com/DBremen/73d7999094e7ac342ad6#file-get-choice-ps1

希望有帮助。

关于azure - 垂直排列的 Powershell GUI 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59090755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com