Powershell not prompting for additional input after Out-GridView has been selected

76 views Asked by At

Below the Get-ADUser, $NewPassword, Set-ADAccountPassword are not working or not even prompting after making a selection in the gridview.

Import-Module ActiveDirectory
$x = Read-Host -Prompt "Enter Search Query for Name"
Get-ADUser -Filter "SamAccountName -like '*$x*'" -Properties DisplayName | 
$choice = (Get-ADUser -Filter "SamAccountName -like '$x'" -Properties DisplayName | Select-Object Name, DistinguishedName | Out-GridView -PassThru)
if($choice) { # if the user selected an item from the DGV and pressed `OK`
    $choice.DistinguishedName
    Get-ADUser -Identity $choice.DistinguishedName -Properties * | Select-Object SamAccountName, Enabled, LockedOut, AccountLockoutTime, BadLogonCount | Out-Host
    $NewPassword = Read-Host -Prompt "Enter New Password for $choice.DistinguishedName"
    Set-ADAccountPassword -Server *REDACTED* -Identity "$choice.DistinguishedName" -NewPassword $NewPassword -Reset
}
else { # user clicked `Cancel` or closed the DGV
    exit
}

Below the same Get-ADUser, Write-Host, and Unlock-ADAccount are not working after selecting an item from the Grid-View to unlock the account.

Import-Module ActiveDirectory
$ous = Search-ADAccount -LockedOut -UsersOnly 
$choice = $ous | Select-Object Name, DistinguishedName | Out-GridView -PassThru
if($choice) { # if the user selected an item from the DGV and pressed `OK`
    $choice.DistinguishedName
    Get-ADUser -Identity $choice.DistinguishedName -Properties * | Select-Object SamAccountName, Enabled, LockedOut, AccountLockoutTime, BadLogonCount | Out-Host
    Write-Host "Unlocking $choice.DistinguishedName"
    Unlock-ADAccount -Server *REDACTED* -Identity $choice.DistinguishedName
}
else { # user clicked `Cancel` or closed the DGV
    exit
}
0

There are 0 answers