【问题标题】:PowerShell - Select only the value (true/false) instead of the complete object itselfPowerShell - 仅选择值 (true/false) 而不是整个对象本身
【发布时间】:2021-10-28 01:56:09
【问题描述】:

// 大家好

我目前正在开发和巩固我的 PowerShell 技能,需要帮助我编写脚本。

我只想检索对象的结果/值“真/假”,而不是值“真/假”结果后跟的描述。

我的代码会更清楚:

$moreadlock = get-aduser -identity fferman -properties * | select lastbadpasswordattempt, lastlogondate, lockedout, passwordexpired, passwordlastset | format-list

    $sAMAccountName = Read-Host "Username"
        if ( $null -ne ([ADSISearcher] "(sAMAccountName=$sAMAccountName)").FindOne() -eq $true ) {
Write-Host "Exist."
Write-Host ""
Write-Host "Additional information about the selected user : "    
$moreadlock

并且在这个级别,我想实现一个条件: 如果 "lockedout" 的值为 true,建议在 AD 中解除对帐户的阻止

        if ( $lockedout -eq $true ) {
Write-Host "Account locked."

在询问用户类似的事情之后:

$qunlock = Read-Host "Unlock account ?"
    if ( $qunlock -eq $true ) {
    Unlock-AdAccount }

问题在于使用命令:

$test=get-aduser -identity user -properties * | Select-Object -Property lockedout
$test

这会返回我:

lockedout
---------
    False

而不是值本身的结果,“假”。

希望你能帮助我,提前谢谢你,

最好的问候。

【问题讨论】:

  • (get-aduser -identity user -properties *).lockedout(称为member enumeration
  • 而不是Select-Object -Property lockedoutSelect-Object -ExpandProperty lockedout
  • 正如@Olaf 的评论,不要使用 -Properties *。在您真正只需要一个属性的情况下请求 ALL 属性是浪费且耗时的。

标签: powershell active-directory admin script


【解决方案1】:

我已经在我的环境中测试过。

请使用以下命令获取 AD 用户的 LockedOut 值:

Get-ADUser -Identity "labadmin" -properties * | Select-Object -ExpandProperty lockedout

【讨论】:

    【解决方案2】:

    这是我选择实施的解决方案。

    Write-Host "Is the account blocked : " -ForegroundColor gray -NoNewline
    (get-aduser -identity $ADuser -properties *).lockedout
    

    *(get me$ADuser 账户的所有属性).选择这个属性的值

    Is the account blocked - example

    【讨论】:

      猜你喜欢
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      • 1970-01-01
      相关资源
      最近更新 更多