Powershell文本颜色

Powershell文本颜色,powershell,Powershell,我有一个小的Powershell脚本,运行该脚本可以向HR显示已终止用户帐户的状态。如果LastLogonDate超过ModifiedDate的时间,是否有办法将颜色从默认更改为红色 Get-ADUser $User1 -Properties Name, Enabled, UserPrincipalName, LastLogonDate, Modified | Select Name, Enabled, UserPrincipalName, LastLogonDate, Modified 我不

我有一个小的Powershell脚本,运行该脚本可以向HR显示已终止用户帐户的状态。如果LastLogonDate超过ModifiedDate的时间,是否有办法将颜色从默认更改为红色

Get-ADUser $User1 -Properties Name, Enabled, UserPrincipalName, LastLogonDate, Modified | Select Name, Enabled, UserPrincipalName, LastLogonDate, Modified

我不喜欢这个代码,所以如果有更好的方法,我想知道更多。

问题是您正在使用一个表

您可以将整个表更改为红色,或者只添加红色警告


$table=Get-ADUser $User1 -Properties Name, Enabled, UserPrincipalName, LastLogonDate, Modified | Select Name, Enabled, UserPrincipalName, LastLogonDate, Modified


$orginal=[console]::ForegroundColor 

if (  $table.LastLogonDate -gt   $table.Modified) {
    Write-Host -ForegroundColor Red "Warning! - Put info here!"
}


我会用红色写一个警告


$table=Get-ADUser $User1 -Properties Name, Enabled, UserPrincipalName, LastLogonDate, Modified | Select Name, Enabled, UserPrincipalName, LastLogonDate, Modified


$orginal=[console]::ForegroundColor 

if (  $table.LastLogonDate -gt   $table.Modified) {
    Write-Host -ForegroundColor Red "Warning! - Put info here!"
}


如果您想更改整个控制台的颜色并将其更改回原来的颜色,这是一个选项

$table=Get-ADUser $User1 -Properties Name, Enabled, UserPrincipalName, LastLogonDate, Modified | Select Name, Enabled, UserPrincipalName, LastLogonDate, Modified


$orginal=[console]::ForegroundColor 

if (  $table.LastLogonDate -gt   $table.Modified) {
    Write-Host -ForegroundColor Yellow "Warning! - Put info here!"
    [console]::ForegroundColor = "Red"
    $table
    [console]::ForegroundColor = $orginal
} else {
    $table
}

第一个选项对我不起作用,但第二个选项运行。这是漫长一天的结束。当我再次有一些自由的大脑周期时,我会设法解决它。但是谢谢你的建议。别担心。。。两个都要工作。意识到丢失了什么。又是德普·坦克斯。