在Powershell脚本中更改输出颜色

时间:2013-10-18 14:47:10

标签: powershell active-directory powershell-module

我想修改我的ActiveDirectory查找脚本以输出颜色的某个结果。

脚本导入AD模块,然后提示您输入用户名并查看某些属性,例如姓名,电子邮件地址和员工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") 

2 个答案:

答案 0 :(得分:2)

Write-Host具有指定前景和背景颜色的参数:

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") 

答案 1 :(得分:1)

$result = 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 |
  out-string
write-host -foregroundcolor Red $result