更改Powershell脚本中的输出颜色

更改Powershell脚本中的输出颜色,powershell,active-directory,powershell-module,Powershell,Active Directory,Powershell Module,我想修改ActiveDirectory查找脚本,以彩色输出特定结果 该脚本导入广告模块,然后提示您输入用户名并查看某些属性,如姓名、电子邮件地址和员工ID 有几个属性的颜色我想根据输出改变 例如,如果'LockedOut'或'PasswordExpired'为'True',我希望这些特定结果的文本颜色为红色 可能吗?非常感谢您的帮助 这是剧本 Import-Module ActiveDirectory do{ $username = (read-host "Please Enter U

我想修改ActiveDirectory查找脚本,以彩色输出特定结果

该脚本导入广告模块,然后提示您输入用户名并查看某些属性,如姓名、电子邮件地址和员工ID

有几个属性的颜色我想根据输出改变

例如,如果'LockedOut'或'PasswordExpired'为'True',我希望这些特定结果的文本颜色为红色

可能吗?非常感谢您的帮助

这是剧本

Import-Module ActiveDirectory

do{ 
   $username = (read-host "Please Enter Username to Lookup")

   get-aduser $username  -properties Created, Name, EmployeeID, EmailAddress, Enabled, LockedOut, LastBadPasswordAttempt, PasswordExpired, AccountExpires, LastLogonDate, Modified, LogonCount, HomeDirectory, Office, TelephoneNumber | Format-List Created, Modified, LogonCount, Name, EmailAddress, EmployeeID, Enabled, LockedOut, PasswordExpired, LastLogonDate, LastBadPasswordAttempt, HomeDirectory, Office, TelephoneNumber

   $response = Read-Host "Enter 'Y' to check another user, any other key to exit"
   Clear-Host
}
while ($response -eq "Y") 

写入主机具有用于指定前景和背景颜色的参数:

Import-Module ActiveDirectory
[string[]]$getADProps=echo Created, Name, EmployeeID, EmailAddress, Enabled, LockedOut, LastBadPasswordAttempt, PasswordExpired, AccountExpires, LastLogonDate, Modified, LogonCount, HomeDirectory, Office, TelephoneNumber
[string[]]$flProps=echo Created, Modified, LogonCount, Name, EmailAddress, EmployeeID, Enabled, LockedOut, PasswordExpired, LastLogonDate, LastBadPasswordAttempt, HomeDirectory, Office, TelephoneNumber
do{ 
    $username = (read-host "Please Enter Username to Lookup")
    $adUser=Get-ADUser $username  -properties $getADProps  
    if ($adUser.'LockedOut' -or $adUser.'PasswordExpired'){
        $adUser | Format-List $flProps  | Out-String | Write-Host -ForegroundColor Red
    }
    else{
        $adUser | Format-List $flProps
    }
    $response = Read-Host "Enter 'Y' to check another user, any other key to exit"
    Clear-Host
}while ($response -eq "Y") 

写入主机具有用于指定前景和背景颜色的参数:

Import-Module ActiveDirectory
[string[]]$getADProps=echo Created, Name, EmployeeID, EmailAddress, Enabled, LockedOut, LastBadPasswordAttempt, PasswordExpired, AccountExpires, LastLogonDate, Modified, LogonCount, HomeDirectory, Office, TelephoneNumber
[string[]]$flProps=echo Created, Modified, LogonCount, Name, EmailAddress, EmployeeID, Enabled, LockedOut, PasswordExpired, LastLogonDate, LastBadPasswordAttempt, HomeDirectory, Office, TelephoneNumber
do{ 
    $username = (read-host "Please Enter Username to Lookup")
    $adUser=Get-ADUser $username  -properties $getADProps  
    if ($adUser.'LockedOut' -or $adUser.'PasswordExpired'){
        $adUser | Format-List $flProps  | Out-String | Write-Host -ForegroundColor Red
    }
    else{
        $adUser | Format-List $flProps
    }
    $response = Read-Host "Enter 'Y' to check another user, any other key to exit"
    Clear-Host
}while ($response -eq "Y") 

写入主机具有用于指定前景和背景颜色的参数:

Import-Module ActiveDirectory
[string[]]$getADProps=echo Created, Name, EmployeeID, EmailAddress, Enabled, LockedOut, LastBadPasswordAttempt, PasswordExpired, AccountExpires, LastLogonDate, Modified, LogonCount, HomeDirectory, Office, TelephoneNumber
[string[]]$flProps=echo Created, Modified, LogonCount, Name, EmailAddress, EmployeeID, Enabled, LockedOut, PasswordExpired, LastLogonDate, LastBadPasswordAttempt, HomeDirectory, Office, TelephoneNumber
do{ 
    $username = (read-host "Please Enter Username to Lookup")
    $adUser=Get-ADUser $username  -properties $getADProps  
    if ($adUser.'LockedOut' -or $adUser.'PasswordExpired'){
        $adUser | Format-List $flProps  | Out-String | Write-Host -ForegroundColor Red
    }
    else{
        $adUser | Format-List $flProps
    }
    $response = Read-Host "Enter 'Y' to check another user, any other key to exit"
    Clear-Host
}while ($response -eq "Y") 

写入主机具有用于指定前景和背景颜色的参数:

Import-Module ActiveDirectory
[string[]]$getADProps=echo Created, Name, EmployeeID, EmailAddress, Enabled, LockedOut, LastBadPasswordAttempt, PasswordExpired, AccountExpires, LastLogonDate, Modified, LogonCount, HomeDirectory, Office, TelephoneNumber
[string[]]$flProps=echo Created, Modified, LogonCount, Name, EmailAddress, EmployeeID, Enabled, LockedOut, PasswordExpired, LastLogonDate, LastBadPasswordAttempt, HomeDirectory, Office, TelephoneNumber
do{ 
    $username = (read-host "Please Enter Username to Lookup")
    $adUser=Get-ADUser $username  -properties $getADProps  
    if ($adUser.'LockedOut' -or $adUser.'PasswordExpired'){
        $adUser | Format-List $flProps  | Out-String | Write-Host -ForegroundColor Red
    }
    else{
        $adUser | Format-List $flProps
    }
    $response = Read-Host "Enter 'Y' to check another user, any other key to exit"
    Clear-Host
}while ($response -eq "Y") 

嗨,比尔,谢谢你的帮助,快到了,但基本上我只希望状态是某种颜色。例如,当PS返回结果时,我想更改单个项目的颜色;如果'True'为绿色,如果'False'为红色,我希望'Enabled'为绿色。为了使它更清晰、更简洁,您需要构建自己的输出,并将write host-nonewline和write host-foregroundcolor结合起来。例如:
write host-nonewline”帐户已禁用:“
然后在下一行
write host-foregroundcolor红色“True”
。您将无法使用format table,因为它将以当前控制台颜色输出对象。嗨,比尔,谢谢您的帮助,它就快到了,但基本上我只希望状态是某种颜色。例如,当PS返回结果时,我想更改单个项目的颜色;如果'True'为绿色,如果'False'为红色,我希望'Enabled'为绿色。为了使它更清晰、更简洁,您需要构建自己的输出,并将write host-nonewline和write host-foregroundcolor结合起来。例如:
write host-nonewline”帐户已禁用:“
然后在下一行
write host-foregroundcolor红色“True”
。您将无法使用format table,因为它将以当前控制台颜色输出对象。嗨,比尔,谢谢您的帮助,它就快到了,但基本上我只希望状态是某种颜色。例如,当PS返回结果时,我想更改单个项目的颜色;如果'True'为绿色,如果'False'为红色,我希望'Enabled'为绿色。为了使它更清晰、更简洁,您需要构建自己的输出,并将write host-nonewline和write host-foregroundcolor结合起来。例如:
write host-nonewline”帐户已禁用:“
然后在下一行
write host-foregroundcolor红色“True”
。您将无法使用format table,因为它将以当前控制台颜色输出对象。嗨,比尔,谢谢您的帮助,它就快到了,但基本上我只希望状态是某种颜色。例如,当PS返回结果时,我想更改单个项目的颜色;如果'True'为绿色,如果'False'为红色,我希望'Enabled'为绿色。为了使它更清晰、更简洁,您需要构建自己的输出,并将write host-nonewline和write host-foregroundcolor结合起来。例如:
write host-nonewline”帐户已禁用:“
然后在下一行
write host-foregroundcolor红色“True”
。你将无法使用format table,因为它将以当前控制台颜色输出对象。谢谢你,德克,很遗憾,我在使用你的答案时收到了错误。格式列表:无法将System.Management.Automation.PSObject转换为以下类型之一{System.String,System.Management.Automation.ScriptBlock}。在第11行,char:30+$adUser |格式列表您好,詹姆斯,是的,我看到您正在使用PowerShell v2。我刚刚在v3上测试过。我已经做了一些修改,使其在v2上也能正常工作。谢谢你,德克,不幸的是,我在使用你的答案时收到了错误。格式列表:无法将System.Management.Automation.PSObject转换为以下类型之一{System.String,System.Management.Automation.ScriptBlock}。在第11行,char:30+$adUser |格式列表您好,詹姆斯,是的,我看到您正在使用PowerShell v2。我刚刚在v3上测试过。我已经做了一些修改,使其在v2上也能正常工作。谢谢你,德克,不幸的是,我在使用你的答案时收到了错误。格式列表:无法将System.Management.Automation.PSObject转换为以下类型之一{System.String,System.Management.Automation.ScriptBlock}。在第11行,char:30+$adUser |格式列表您好,詹姆斯,是的,我看到您正在使用PowerShell v2。我刚刚在v3上测试过。我已经做了一些修改,使其在v2上也能正常工作。谢谢你,德克,不幸的是,我在使用你的答案时收到了错误。格式列表:无法将System.Management.Automation.PSObject转换为以下类型之一{System.String,System.Management.Automation.ScriptBlock}。在第11行,char:30+$adUser |格式列表您好,詹姆斯,是的,我看到您正在使用PowerShell v2。我刚刚在v3上测试过。我已经做了一些更改,使其在v2上也能工作。