Powershell - 某些结果的输出颜色 [英] Powershell - Output Color for certain results

查看:36
本文介绍了Powershell - 某些结果的输出颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行下面的代码并且它有效.我想要做的是,在 Enabled 属性为 True 时的结果中,我希望文本 'True' 前景色为绿色,否则为红色.

I am running the code below and it works. What I am trying to do is, in the results when Enabled properties is True I would like to the text 'True' foreground color to be green otherwise if 'false' color Red.

与 Password never expires 属性相同,如果结果为 'True' 我希望文本 'True' 前景色为红色,否则如果它的 'False' 颜色为绿色.

The same with the Password never expires properties, if the result is 'True' I would like to the text 'True' foreground color to be Red otherwise if its 'False' color green.

我正在尝试将 if-else 语句放入代码中,但没有成功.

I am trying to fit an if-else statement into the code but have not been successfull.

代码:

Get-ADUser "user name"  -Properties Enabled,LockedOut,DisplayName,GivenName, SurName,Mail,LastLogon, Created,passwordlastset,Passwordneverexpires,msDS-UserPasswordExpiryTimeComputed, Description, office, Canonicalname | `
        Select-Object Enabled, 
        @{Expression={$_.LockedOut};Label='Locked';}, 
        @{Expression={$_.DisplayName};Label='Display Name';},
        @{Expression ={$_.GivenName};Label='Name';}, `
        @{Expression ={$_.SurName}; Label='Last Name'; }, 
        Mail, 
        @{Expression ={[DateTime]::FromFileTime($_.LastLogon)}; Label='Last Logon';}, 
        @{Expression={$_.Created};Label='Date Created';}, 
        @{Expression={$_.passwordlastset};Label='PW Last Reset';}, 
        @{Expression={$_.Passwordneverexpires};Label='PW Never Expires';},
        @{Name="PW Exp Date";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}  ,
        Description, 
        Office,
        Canonicalname 
        Format-list

有什么建议吗?

推荐答案

这是一个可能的解决方案:

Here's a possible solution:

$Highlight = @{
    True = 'Red'
    False = 'Green'
}

$User = Get-ADUser "user name"  -Properties Enabled,LockedOut,DisplayName,GivenName,SurName,Mail,LastLogon,Created,passwordlastset,Passwordneverexpires,msDS-UserPasswordExpiryTimeComputed, Description, office, Canonicalname |
        Select-Object Enabled, 
        @{Expression={$_.LockedOut};Label='Locked';}, 
        @{Expression={$_.DisplayName};Label='Display Name';},
        @{Expression ={$_.GivenName};Label='Name';}, `
        @{Expression ={$_.SurName}; Label='Last Name'; }, 
        Mail, 
        @{Expression ={[DateTime]::FromFileTime($_.LastLogon)}; Label='Last Logon';}, 
        @{Expression={$_.Created};Label='Date Created';}, 
        @{Expression={$_.passwordlastset};Label='PW Last Reset';}, 
        @{Expression={$_.Passwordneverexpires};Label='PW Never Expires';},
        @{Name="PW Exp Date";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}},
        Description, 
        Office,
        Canonicalname | Format-list | Out-String

$User -split "`n" | ForEach-Object {
    $Output = $False
    If ($_ -match 'Enabled|PW Never Expires') {
        ForEach ($Entry in $Highlight.Keys){
            $Text = $_ -split $Entry
            If ($Text.count -gt 1) { 
                Write-Host $Text[0] -NoNewline
                Write-Host $Entry -ForegroundColor $Highlight.$Entry
                $Output = $true
                Break
            }
        }
    }

    If (-not $Output) { Write-Host $_ }
}

请注意,这应该特别适用于您使用 Format-List 获得的那种输出,但不是一个经过深思熟虑的突出显示功能,否则:例如,这将无法处理可能在同一行文本中多次出现.

Note that this should work for the sort of output you get with Format-List in particular but isn't a particularly well thought out highlighting function otherwise: e.g this won't handle words that might appear multiple times in the same line of text.

另外,仅供参考,以这种方式使用 PowerShell 通常不是最佳实践.您应该考虑编写输出到管道的 PowerShell 工具,而不是通过 Write-Host,这样您就可以在其他命令中操作结果,或将其转换(例如,转换为 CSV 等).

Also FYI working with PowerShell in this way isn't generally best practice. You should consider writing PowerShell tools that output to the pipeline and not via Write-Host, that way you can manipulate the results in other commands, or transform it (e.g to CSV etc.).

这篇关于Powershell - 某些结果的输出颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆