Powershell output in color

风流意气都作罢 提交于 2021-01-29 19:00:53

问题


I am using a Powershell form to output some data and I am wondering how I can get the output in color?

I am not using write-host. That is not what I am looking for. I know you can use -ForegroundColor for that.

It's for Get-ADUser -Filter "UserPrincipalName -like 'Username'" | Select Enabled

If output is False it needs to be in Red. If output is true just regular color.

Anyone who can help me?

Many thanks. Ralph.


回答1:


A follow-up to my comment

#region Begin functions and code behind

function RunCode { 
    $ProcessList = (Get-Process).Name
    If ($ProcessList -ge 10)
    {$DataSet.ForeColor = 'red'}
    else {$DataSet.ForeColor = 'black'}
    [void] $DataSet.Items.Addrange($ProcessList)
}

#endregion End functions and code behind


#region Begin GUI code
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '511,501'
$Form.text                       = "Form"
$Form.TopMost                    = $false

$RunCode                         = New-Object system.Windows.Forms.Button
$RunCode.text                    = "RunCode"
$RunCode.width                   = 90
$RunCode.height                  = 30
$RunCode.location                = New-Object System.Drawing.Point(19,17)
$RunCode.Font                    = 'Microsoft Sans Serif,10'

$DataSet                         = New-Object system.Windows.Forms.ListBox
$DataSet.text                    = "listBox"
$DataSet.width                   = 204
$DataSet.height                  = 144
$DataSet.location                = New-Object System.Drawing.Point(17,98)

$Form.controls.AddRange(@(
    $RunCode,
    $DataSet
))

$RunCode.Add_Click({ RunCode })

#endregion Begin GUI code


# Call the GUI
[void]$Form.ShowDialog()


来源:https://stackoverflow.com/questions/61804287/powershell-output-in-color

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