Author Posts

September 9, 2015 at 12:56 pm

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.

September 9, 2015 at 1:54 pm

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

September 9, 2015 at 2:14 pm

Marc,

One way:

$User = Get-ADUser 'Joe' -Properties *
$User | Select ( $User.PropertyNames | Where { $User.$_ -is [datetime] } )

September 9, 2015 at 2:20 pm

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

September 9, 2015 at 2:25 pm

@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?

September 9, 2015 at 3:21 pm

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.

September 9, 2015 at 3:24 pm

@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.

September 9, 2015 at 4:09 pm

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] } )

September 9, 2015 at 4:27 pm

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.

September 10, 2015 at 6:46 am

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.