需要从域向下一级的列表来编号到选择菜单以从中进行选择以执行下一部分?

问题描述 投票:0回答:1

我正在尝试为我的用户重置密码,但控制为一次在一家商店执行此操作。 我正在使用命令:

Get-ADOrganizationalUnit -Filter 'Name -like "store-*"' | FT Name, DistinguishedName -AutoSize

显示下一级的所有 OU,但我想从该列表 (1-20) 中选择我想要更改的商店。

我将使用代码仅重置该商店的密码标志:

$days = 42  #change date here according to GPO.
$users = Get-ADUser -Filter { enabled -eq $true } -Properties pwdLastSet |
    Select-Object samaccountname, @{n = 'pwdLastSet'; e = { [DateTime]::FromFileTime($_.pwdLastSet) } } |
    Where-Object { $_.pwdlastset -lt (Get-Date).AddDays(-$days) }

foreach ($user in $users) {
    $sam = $user.samaccountname

    $todouser = Get-ADUser $sam -Properties pwdLastSet, distinguishedname

    $todouser.pwdLastSet = 0
    Set-ADUser -Instance $todouser

    $todouser.pwdLastSet = -1
    Set-ADUser -Instance $todouser
}

我希望所有管理员都能够选择一家商店并处理所有用户,然后继续。我只是不想在同一天大规模更改公司的所有用户 - 哇,那将是一场灾难。

我只是找不到任何代码来获取第一行代码的输出并将其放入屏幕上的选择列表中。然后进行选择并用它来帮助选择第二部分中的过滤器。

windows powershell active-directory multiple-choice
1个回答
1
投票

您可以在 PowerShell 中使用的最简单的内置方法来提供选择是使用

OutGridView -PassThru
。然后您可以添加 2 个条件来确保:

  1. 用户做出了选择。
  2. 用户选择了仅一个组织单位。

然后,为了过滤用户,您可以包含

-SearchBase
-SearchScope
以定位该特定组织单位,并指定是否要在该层次结构中查找某一级别的用户或递归搜索。您还可以使用 Active Directory 过滤器查找过去 42 天内未设置密码的用户。

最后,

Set-ADUser
已经有了
-ChangePasswordAtLogon
标志,比手动设置
pwdLastSet
容易得多。

[array] $selection = Get-ADOrganizationalUnit -Filter 'Name -like "store-*"' |
    Select-Object Name, DistinguishedName |
    Out-GridView -PassThru -Title 'Pick an OrganizationalUnit'

if ($selection.Count -eq 0) {
    throw 'Please make a selection...'
}
elseif ($selection.Count -gt 1) {
    throw 'Please select a single Organizational Unit, multi-selection is not allowed...'
}

$days = 42  # change date here according to GPO.
$targetDate = (Get-Date).AddDays(-$days).ToFileTimeUtc()

$getADUserSplat = @{
    Filter      = "enabled -eq '$true' -and pwdLastSet -lt '$targetDate'"
    SearchBase  = $selection.DistinguishedName # Setting base here
    SearchScope = 'OneLevel' # Change to 'Subtree' to target all users in that hierarchy
}

$updatedUsers = Get-ADUser @getADUserSplat |
    Set-ADUser -ChangePasswordAtLogon $true -PassThru
© www.soinside.com 2019 - 2024. All rights reserved.