首页 > 解决方案 > 将 AD 信息移动到 excel 的脚本,有没有办法为描述值编写 if 函数?

问题描述

这里是 PowerShell 的新手,我正在尝试编写一个脚本来通过 powershell 将一些 AD 信息移动到 excel 中。我遇到的问题是,除了所有其他事情,我试图让 excel 中的一个字段成为描述字段。从那里我正在尝试编写一个 if 函数来根据它是否符合描述进行过滤。

get-aduser -Filter * -properties CN,PasswordNeverExpires,LastLogon,description| where {
$_.PasswordNeverExpires -eq "true" } | If ( $_.description -eq "super") {"SU"} Else {"Human"} | Select-Object CN, enabled,samAccountName,LastLogon, description | Export-Csv $path ```

No error message just doesn't display description field in excel.

标签: powershellactive-directory

解决方案


您可以计算-blockdescription中的值。select无需在管道中进行。以下是使用计算属性的方法。计算属性是定义为具有名称(“描述”)的哈希表和具有表达式(if/else)的代码块的属性:

get-aduser -Filter * -properties CN,PasswordNeverExpires,LastLogon,description| where {
$_.PasswordNeverExpires -eq "true" } | select CN, enabled,samAccountName,LastLogon,@{Name ="description";Expression={ If ( $_.description -eq "super") {"SU"} Else {"Human"}  }} | Export-Csv $path

推荐阅读