getting password age in readable columns

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #6170

    by Peeps3240 at 2012-08-30 10:28:50

    $filter = '(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=65536)(!userAccountControl:1.2.840.113556.1.4.803:=2))'

    $root = New-Object System.DirectoryServices.DirectoryEntry("LDAP://DC=[i]Domain[/i];DC=COM")
    $searcher = New-Object System.DirectoryServices.DirectorySearcher $filter
    $SearchRoot = $root.defaultNamingContext
    $searcher.SearchRoot = "LDAP://CN=Users,$SearchRoot"
    $searcher.SearchScope = 'SubTree'
    $searcher.SizeLimit = 0
    $searcher.PageSize = 1000
    $searcher.FindAll() | Foreach-Object {
    $account = $_.GetDirectoryEntry()
    $pwdset = [datetime]::fromfiletime($_.properties.item("pwdLastSet")[0])
    $age = (New-TimeSpan $pwdset).Days
    $mail = $_.properties.item("mail")
    $info = 1 | Select-Object Name, Email, Age, LastSet
    $info.Name = $account.SamAccountName[0]
    $info.Email = $mail
    $info.Age = $age
    $info.LastSet = $pwdset
    $info
    }

    so this code will give me output such as this

    kreki {Irma.Krek@XXXX... 161 3/22/2012 8:04:5...
    eslamlooym {} 3095 3/10/2004 9:52:1...
    Haydt {} 2908 9/13/2004 11:50:...
    winfax {} 4011 9/5/2001 2:01:28 PM
    djcermak {} 4209 2/20/2001 10:42:...
    morrisonr {} 4145 4/25/2001 9:21:0...
    smithe {} 3317 8/1/2003 11:51:4...
    worthingtond {} 4145 4/25/2001 9:09:1...
    randolphw {} 4145 4/25/2001 9:12:3...
    hinesk {} 4216 2/13/2001 9:51:1...

    my problem is the email field is truncated and being new to shell scripting, as in just started yesterday, i have no idea how to widen the column for the output. i tried the wide-format but that didnt work as it only expanded the first two colunms.

    any help will be greatly appreciated as i think i will be checking things out here a lot

    by poshoholic at 2012-08-30 11:19:33

    If you want tabular format and you want to make sure you see all information, you should read this blog post:

    http://poshoholic.com/2010/11/11/powershell-quick-tip-creating-wide-tables-with-powershell/

    You should also look at the help for Format-List and Format-Table. Each of these allows you to identify the parameters you want to see. For Format-Table, make sure you read about the -AutoSize parameter. That is helpful in some cases, and I use it in the blog article I just referenced. Format-List is also useful when you want to see many or all parameters but not necessarily in a table. Out-GridView would be another great option for you so that you can look at the results in a grid where space is less of an issue. One of these should give you a good nudge in the right direction. After you've looked at a few of these, if you're still not getting the output you want reply back with details about how you want it to appear and I may be able to help you get that output.

    by Peeps3240 at 2012-08-30 11:53:21

    Thanks for the heads up Kirk,

    I guess my biggest issue is where the |Format-Table -Property* -Autosize' goes.

    i tried it like this:

    c:\scripts\password.ps1 |Format-Table -Property* -Autosize' |Out-String -Width 4096' |Out-file C:\scripts\passwordexpireswide.txt

    but that just hangs in powershell.

    generally this isnt a big issue as there are only a small handful of people i have to email right now, but down the road it will be a much higher number

    by poshoholic at 2012-08-30 12:04:08

    You're welcome. 🙂

    If you are going to use that command on one line, you need to remove the backticks (line continuance characters). Also make sure your spacing is correct (you need a space between -Property and * for Format-List). Like this:

    [script=powershell]C:\scripts\password.ps1 | Format-Table -Property * -Autosize | Out-String -Width 4096 | Out-File C]

    by Peeps3240 at 2012-08-31 08:43:27

    many many thanks, that works perfectly

You must be logged in to reply to this topic.