Limit Cmdlet results based on definition type?

This topic contains 9 replies, has 3 voices, and was last updated by Profile photo of Curtis Smith Curtis Smith 1 year, 4 months ago.

  • Author
    Posts
  • #29569
    Profile photo of marc carter
    marc carter
    Member

    I'm looking for a more efficient means to limit results to specific definition types. For example, if I'm troubleshooting issues with an AD account and know that there's a property that contains a datetime definition type but I've forgotten the property name. I can pipe the get-aduser cmdlet to get-member and/or format-list *, figure out which properties I need, then continue on with my troubleshooting (more querying).

    Using psreadline I can use the asterisk (*) after a partial property name to grab any similarly named property. But I'm hoping someone has a better means of displaying only the properties containing the same definition types.

  • #29573
    Profile photo of Curtis Smith
    Curtis Smith
    Participant
    function Get-ADUserPropertybyType {
        [CmdletBinding()]
        Param (
            [string]$user,
            [string]$type
        )
        $ADUser = Get-ADUser $user -properties *
        $ADUserProperties = ($ADUser | Get-Member -MemberType Property).Name
        $ADUserProperties | ForEach-Object {
            If(($ADUser.$_).GetType().Name -eq $type) {
                "$_ : $($ADUser.$_)"
            }
        }
    }
    

    Get-ADUserPropertybyType -user account -type DateTime -ErrorAction SilentlyContinue

    Results:

    Created : 04/02/2008 10:54:14
    createTimeStamp : 04/02/2008 10:54:14
    LastBadPasswordAttempt : 08/27/2015 16:33:15
    LastLogonDate : 09/09/2015 08:01:28
    Modified : 09/09/2015 15:27:11
    modifyTimeStamp : 09/09/2015 15:27:11
    PasswordLastSet : 06/15/2015 10:01:02
    whenChanged : 09/09/2015 15:27:11
    whenCreated : 04/02/2008 10:54:14

  • #29577
    Profile photo of Tim Curwick
    Tim Curwick
    Participant

    Marc,

    One way:

    $User = Get-ADUser 'Joe' -Properties *
    $User | Select ( $User.PropertyNames | Where { $User.$_ -is [datetime] } )
    
  • #29579
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Some slight modifications so you can use wildcards in your parameter type and so the output shows the definition as well as outputing an Object that can be piped into other cmdlets.

    function Get-ADUserPropertybyType {
        [CmdletBinding()]
        Param (
            [string]$user,
            [string]$type
        )
        $ADUser = Get-ADUser $user -properties *
        $ADUserProperties = ($ADUser | Get-Member -MemberType Property).Name
        $ADUserProperties | ForEach-Object {
            If(($ADUser.$_).GetType().Name -like $type) {
                [PSCustomObject]@{
                    Property = $_
                    Value = $ADUser.$_
                    Definition = ($ADUser.$_).GetType().Name
                }
            }
        }
    }
    


    Get-ADUserPropertybyType -user account -type "*int*" -ErrorAction SilentlyContinue | Format-Table -AutoSize

    Results:

    Property Value Definition
    -------- ----- ----------
    accountExpires 0 Int64
    adminCount 0 Int32
    BadLogonCount 0 Int32
    badPasswordTime 130851847954664956 Int64
    badPwdCount 0 Int32
    codePage 0 Int32
    countryCode 840 Int32
    instanceType 4 Int32
    internetEncoding 1310720 Int32
    lastLogoff 0 Int64
    lastLogon 130863013952223728 Int64
    lastLogonTimestamp 130862772888337631 Int64
    lockoutTime 0 Int64
    logonCount 16699 Int32

  • #29580
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    @Tim: I looked at doing something like your approach, which is very nice BTW, but I did not see a "propertynames" property when looking at the returned AD User object by outputing the properties or when using get-member. How did you know "propertynames" was available, is it a common property?

  • #29582
    Profile photo of Tim Curwick
    Tim Curwick
    Participant

    Curtis,

    Some objects have such things, so it was worth looking for. I retrieved a user account to play with, and then typed "$User.prop" in ISE. Intellisense presented several options for completion, including PropertyNames, and it was just what was needed. (So mostly, I just got lucky.)

    I don't know why Get-Member doesn't list it as an option for this object. Perhaps someone else can enlighten us. The most I could get out of Get-Member with a force switch is the .get_PropertyNames() method that sits behind the PropertyNames property.

  • #29583
    Profile photo of marc carter
    marc carter
    Member

    @Curtis/Tim –
    Thank you both for the responses. Tim's solution is more in line with what I was looking for. PropertyNames is part of the psbase collection. Unfortunately there doesn't appear to be something like PropertyDefinition collection. (That would have been too easy)

    Ultimately, I'm more interested in a quick and dirty way to return only those properties that I'm interested in (based on type not name). If I'm troubleshooting something and I'm only looking for specific properties like datetime and I don't happen to remember all the properties associated with datetime for a given cmdlet, I don't want to go through step of identifying those properties. I was thinking this would be something that I would use regularly.

    Get-ChildItem would be another example.
    If I'm troubleshooting issues that revolve around recent changes to a file or folder and I want to list *all* properites that contain a datetime value I could execute the following and successfully list out all properties that contained a reference to datetime based on the property name.

    dir | fl *time*

    The same technique doesn't work for get-aduser as I'd miss properties like Created, LastLogonDate, Modified, etc.

  • #29585
    Profile photo of Tim Curwick
    Tim Curwick
    Participant

    Marc,

    What I posted the first time does filter based on object types of the properties, not the names.

    For files you would need to do something like this:

    $File = Get-ChildItem | Select-First 1
    $File | Select ( ( $File | Get-Member -MemberType Properties ).Name | Where { $File.$_ -is [datetime] } )
    
  • #29587
    Profile photo of marc carter
    marc carter
    Member

    Tim,
    Yes, I'm with you on that.
    Was hoping there might be a filter or another means to perform as one-liner. That probably sounds like nit-picking. Appreciate your response and Curtis. Likely the best option available without adding something to my profile.

  • #29636
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Ah, makes since, thanks Tim.

    @Marc: if you build a function and put it in a powershell module, then you can easily use it as a one liner any time you want.

You must be logged in to reply to this topic.