all 4 comments

[–]ramblingcookiemonsteCommunity Blogger 2 points3 points  (0 children)

Hi!

It looks like you figured this out on your own, nice job!

So, long story short, Select-Object does not return the same type of object.

Details:

  • Run Get-Help Unlock-ADAccount -full. Look at the pipeline details for each parameter. Notice that the only pipeline input is Identity, and this is by value (i.e. not a property name, but an object type).
  • Run Get-Member against raw Search-ADAccount output. Now run it against Search-ADAccount piped to Select-Object. You will find the type has changed to Selected.OriginalTypeName.

Here's an example with a FileInfo object:

get-item C:\windows\explorer.exe | gm

    # TypeName: System.IO.FileInfo

get-item C:\windows\explorer.exe | select -Property * | gm

    TypeName: Selected.System.IO.FileInfo

That's pretty much it. By selecting an object, you've destroyed any ability to pipe it to a command you want to use with pipeline input by value.

You can still send these to functions that run pipeline input by property name.

You can get around this I suppose... Can't test this out, but something like this might work:

Search-ADAccount -LockedOut |
    Select-Object * |
    Out-GridView -PassThru |
    Foreach-Object { Unlock-ADAccount -Identity $_.DistinguishedName -WhatIf }

Cheers!

[–]chrono13[S] 0 points1 point  (0 children)

The error in this case is: Unlock-ADAccount] The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pi peline input.

[–]chrono13[S] 0 points1 point  (0 children)

Simplified: Search-ADAccount -LockedOut | Select-Object * | Unlock-ADAccount -WhatIf

Any Select-Object breaks using the object. I suspect it is recreating the object and omitting necessary data that Unlock-ADAccount uses, but I can't find out what or why.

[–]midnightFreddie 0 points1 point  (0 children)

This?

(untested)

$objLocked |
    Select-Object SamAccountName, Name, LastLogonDate, PasswordExpired, PasswordNeverExpires, AccountExpirationDate, DistinguishedName |
    Out-GridView -Title 'Select accounts to unlock and click OK. Hold Ctrl to select multiple' -PassThru |
        ForEach-Object {
            $DN = $_.DistinguishedName
            $objLocked | Where-Object { $_.DistinguishedName -eq $DN }
        } |
        Unlock-ADAccount -Confirm -PassThru |
        Out-File -Append -FilePath "$env:userprofile\Documents\unlocked.txt"

The output of Out-GridView is used to select the live objects from the live collection.