Powershell、组合框、get-aduser。根据条件以颜色输出用户

问题描述 投票:0回答:1

请帮助我。如何根据帐户状态在 WinForm ComboBox 中以字体颜色显示 AD 用户列表?例如:禁用用户为浅灰色,无限制密码为蓝色。

$Users = Get-ADuser -filter * -Properties Name
Foreach ($User in $Users)
{
$ComboBox1.Items.Add($User.Name) ;
}
powershell winforms combobox get-aduser
1个回答
0
投票

在组合框中具有不同字体颜色的唯一方法是覆盖

DrawItem
事件。为此,您还需要将
ComboBox.DrawMode
设置为
OwnerDrawFixed

下面的代码显示了使用该技术的演示表单

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form               = New-Object system.Windows.Forms.Form
$Form.StartPosition = 'CenterScreen'
$Form.Text          = 'Colored Combobox'
$Form.Width         = 500
$Form.Height        = 200
$Form.TopMost       = $true

$ComboBox1               = New-Object System.Windows.Forms.ComboBox
$ComboBox1.Location      = New-Object System.Drawing.Point(10,10)
$ComboBox1.Width         = $Form.Width - ($ComboBox1.Margin.Horizontal * 4) -10
$ComboBox1.DropDownStyle = 'DropDownList'
# just for demo a nice large bold font
$combobox1.Font          = [System.Drawing.Font]::New("Calibri", 12, [System.Drawing.FontStyle]::Bold)
# override rendering of the Combobox.
$ComboBox1.DrawMode      = [System.Windows.Forms.DrawMode]::OwnerDrawFixed

# get an array of user object with the needed properties
$Users = Get-ADUser -Filter * -Properties PasswordNeverExpires, PasswordNotRequired | Sort-Object Name

# add the user names to the combobox
foreach ($user in $Users) { 
    [void]$ComboBox1.Items.Add($user.Name)
}
# set the text for the first item
$ComboBox1.SelectedIndex = 0
# create a variable that will keep the chosen item's index after the form is closed
$SelectedIndex = 0         

# add an event that draws each item.
$ComboBox1.Add_DrawItem({
    param(
        [object]$s,                                 # sender object
        [System.Windows.Forms.DrawItemEventArgs]$e  # event arguments
    )

    # clear the background for this item with the default color
    $e.DrawBackground()  
    if ($e.Index -ge 0) {
        # get the user object from the $Users array using the event index
        $user  = $script:Users[$e.Index]
        # determine the color to use when displaying this item in the combobox
        # use any of the KnownColor names (https://learn.microsoft.com/en-us/dotnet/api/system.drawing.knowncolor)
        $color = if (!$user.Enabled) { 'Gray' }
                 elseif ($user.PasswordNeverExpires -or $user.PasswordNotRequired) { 'Blue' }
                 else { 'Black' }
        $brush = [System.Drawing.SolidBrush]::New([System.Drawing.Color]::FromName($color))

        # paint the text
        $e.Graphics.DrawString($user.Name, $this.Font, $brush, $e.Bounds.X, $e.Bounds.Top)

        # save memory by disposing of the brush object
        $brush.Dispose()
    }
})

# add another event to capture the selected index to use after the form is closed
$ComboBox1.Add_SelectedIndexChanged({
    $script:SelectedIndex = $this.SelectedIndex
})
# add the combobox to the form
$Form.Controls.Add($ComboBox1)

[void]$Form.ShowDialog()
# remove from memory
$Form.Dispose()

Write-Host ('You selected user {0}' -f $Users[$SelectedIndex].Name)

© www.soinside.com 2019 - 2024. All rights reserved.