【问题标题】:Powershell color outputPowershell 颜色输出
【发布时间】:2014-08-28 23:56:03
【问题描述】:

-问题1-

脚本:

$ADInfo = (Get-ADUser $ntaccount1 -Properties *)

Write-Host -NoNewLine -ForegroundColor Gray "Enabled                ";
Write-Host -NoNewLine ": ";
if ($ADInfo.Enabled -eq "False") {'Write-Host -ForegroundColor Gray $ADInfo.Enabled'} ELSE {'Write-Host -ForegroundColor Red $ADinfo.Enabled'}; #If False=gray if True=red

输出:

Enabled: False

我正在努力做到这一点,所以如果 $ADInfo.Enabled 等于 False,则为一种颜色。如果它是真的,那就是另一个。我无法让它发挥作用。

-问题2-

我正在尝试以与问题 1 相同的格式获取此脚本,但是,我没有得到相同的输出。下面粘贴的内容 100% 有效。它会导致 AD 的到期日期。如果我试图把它变成问题 1,我会得到一些随机日期 12/31/1600 7:00:00 PM 。我希望它与问题 1 相同,因此我可以将输出日期设置为我选择的任何颜色。

Get-ADUser -identity usernamehere -properties msDS-UserPasswordExpiryTimeComputed | format-list @{ Name = "Expiration Date";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}};

【问题讨论】:

    标签: powershell colors output


    【解决方案1】:

    这个怎么样?

    $ADInfo = (Get-ADUser jdoe -Properties *)
    
    Write-Host "Full Name                 : " $ADinfo.Name
    Write-Host "User ID                   : " $ADinfo.SamAccountName
    Write-Host "Email                     : " $ADinfo.mail
    Write-Host "Enabled                   : " $ADInfo.Enabled
    Write-Host "Locked Out                :  " -NoNewline; if ($ADInfo) {Write-Host -ForegroundColor Green $ADInfo.LockedOut} ELSE {Write-Host -ForegroundColor Red $ADinfo.LockedOut}
    Write-Host "Expiration Date           :  " -NoNewline; Write-Host ([datetime]::FromFileTime($ADInfo."msDS-UserPasswordExpiryTimeComputed"))
    Write-Host "Password Last Set         : " $ADinfo.PasswordLastSet
    Write-Host "Last Bad Password Attempt : " $ADinfo.LastBadPasswordAttempt
    Write-Host "Account Creation Date     : " $ADinfo.whenCreated
    Write-Host "Last change               : " $ADinfo.whenChanged
    Write-Host "Employee ID               : " $ADinfo.EmployeeID
    Write-Host "Account Description       : " $ADinfo.Description
    

    【讨论】:

    • 嘿,我编辑了我的 OP。 if 语句不起作用(颜色每次都变为绿色),并且“到期日期”位代码给出了 12/30/1600 7:00:00 PM 的通用日期。我在 OP 中的那行很好,但你说的方式不行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 2019-07-26
    • 2017-11-12
    • 2020-08-31
    • 2021-05-02
    相关资源
    最近更新 更多