none
Looking for script help - Get-Content of directories from Get-ADuser

    Question

  • Hi all.
    I am in no means an expert in PowerShell but I can usually find something that works.  This one though has had me bashing my head against a wall for the past week.  
    I need to provide a report that exports the following AD User information into a CSV...

    SamAccountName, Name, Enabled, LastLogonDate, PasswordExpired, LastBadPasswordAttempt, Description, EmailAddress, HomeDirectory, Home Size (MB), ProfilePath, Profile Size (MB), DistinguishedName.

    So far I have the following which isn't working as Select-Object needs to have a pipe.
    There is also no Export-Csv.
    The calculation doesn't work either.  I have been using this calculation in the past except performing it in a Select-Object (Example Select-Object @{N='Size (MB)';E={"{0:N2}" -f ($_.sum / 1MB)}}).
    At the point when the HomeDirectory property is empty I guess the Size should be outputting as 0MB.

    I am having to export the Get-ADUser to a variable as when I was straight piping into a ForEach-Object, at random intervals my script would fail with Logon server errors.

    Import-Module ActiveDirectory
    
    $Users = Get-AdUser -Filter * -Properties * 
    
    ForEach ( $User in $Users ) {
    If (!($User.HomeDirectory -eq $null)) {
    $HomeLenght = Get-ChildItem ($User.HomeDirectory) -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -property length -sum -ErrorAction SilentlyContinue
    $HomeSum = "{0:N2}" -f ($HomeLength.sum / 1MB)
    }
    If (!($User.ProfileDirectory -eq $null)) {
    $ProfileLength = Get-ChildItem ($User.ProfilePath + ".v2") -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -property length -sum -ErrorAction SilentlyContinue
    $ProfileSum = "{0:N2}" -f ($ProfileLength.sum / 1MB)
    }
    Select-Object $User.SamAccountName, $User.Name, $User.Enabled, $User.LastLogonDate, $User.PasswordExpired, $User.LastBadPasswordAttempt, $User.Description, $User.EmailAddress, $User.HomeDirectory, @{N='Home Size (MB)';E={$HomeSum}}, $ProfileLength.ProfilePath, @{N='Profile Size (MB)';E={$ProfileSum}}, $User.DistinguishedName
    }
    Hope this makes sense.  

    Andrew


    • Edited by Andrew J Palmer Wednesday, July 31, 2013 1:23 PM Correcting typos
    Wednesday, July 31, 2013 1:21 PM

Answers

  • I'd try it using new-object, also work on your formatting, it makes troubleshooting a lot easier:

    Import-Module ActiveDirectory
    
    $Users = Get-AdUser -Filter * -Properties * 
    
    ForEach ( $User in $Users ) 
        {
            If (!($User.HomeDirectory)) 
                {
                    $HomeLength = Get-ChildItem ($User.HomeDirectory) -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -property length -sum
                    $HomeSum = "{0:N2}" -f ($HomeLength.sum / 1MB)
                }
            If (!($User.ProfileDirectory)) 
                {
                    $ProfileLength = Get-ChildItem ($User.ProfilePath + ".v2") -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -property length -sum
                    $ProfileSum = "{0:N2}" -f ($ProfileLength.sum / 1MB)
                }
            New-Object -TypeName PSObject -Property @{
                    SAMAccountname = $User.SamAccountName
                    Name = $User.Name
                    'Enabled?' = $User.Enabled
                    LasLogonDate = $User.LastLogonDate
                    'PasswordExpired' = $User.PasswordExpired
                    LastBadPassword = $User.LastBadPasswordAttempt
                    Description = $User.Description
                    Email = $User.EmailAddress
                    HomeDir = $User.HomeDirectory
                    'Home Size (MB)' = $HomeSum
                    ProfilePath = $ProfileLength.ProfilePath
                    'Profile Size (MB)' = $ProfileSum
                    DM = $User.DistinguishedName
                }
        }


    Hope that helps! Jason

    Wednesday, July 31, 2013 1:41 PM
  • If you're using powershell v3 you can make an ordered hash table by adding [ordered] in front of the hash opening , @{symbol.  Otherwise you'll need to use select to set the order.

    As for exporting to csv the way I do it is pipe the script outp[ut to a csv instead of adding it to the script itself.

    .\myscript.ps1 | export-csv .\mycsv.csv


    Hope that helps! Jason

    Wednesday, July 31, 2013 2:49 PM

All replies

  • Hi Andrew,

    You have a typo when you're setting the $HomeLength variable. You've got it named $HomeLenght.


    Don't retire TechNet!

    Wednesday, July 31, 2013 1:35 PM
  • I'd try it using new-object, also work on your formatting, it makes troubleshooting a lot easier:

    Import-Module ActiveDirectory
    
    $Users = Get-AdUser -Filter * -Properties * 
    
    ForEach ( $User in $Users ) 
        {
            If (!($User.HomeDirectory)) 
                {
                    $HomeLength = Get-ChildItem ($User.HomeDirectory) -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -property length -sum
                    $HomeSum = "{0:N2}" -f ($HomeLength.sum / 1MB)
                }
            If (!($User.ProfileDirectory)) 
                {
                    $ProfileLength = Get-ChildItem ($User.ProfilePath + ".v2") -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -property length -sum
                    $ProfileSum = "{0:N2}" -f ($ProfileLength.sum / 1MB)
                }
            New-Object -TypeName PSObject -Property @{
                    SAMAccountname = $User.SamAccountName
                    Name = $User.Name
                    'Enabled?' = $User.Enabled
                    LasLogonDate = $User.LastLogonDate
                    'PasswordExpired' = $User.PasswordExpired
                    LastBadPassword = $User.LastBadPasswordAttempt
                    Description = $User.Description
                    Email = $User.EmailAddress
                    HomeDir = $User.HomeDirectory
                    'Home Size (MB)' = $HomeSum
                    ProfilePath = $ProfileLength.ProfilePath
                    'Profile Size (MB)' = $ProfileSum
                    DM = $User.DistinguishedName
                }
        }


    Hope that helps! Jason

    Wednesday, July 31, 2013 1:41 PM
  • Thanks for the typo highlight.

    Wow Jason that's pretty amazing. I've ran it and it indeed works.  With testing I don't need to provide testing to see if the paths exist. It will always work out the sum is 0 anyway which is lucky. Two questions though.
    The output is shown in a different order that is shown in the script is there any way to make sure it outputs in the order specified in the script?
    How would I export the output to a CSV? I tried adding Export-Csv to each of the two last script blocks but got errors.

    Wednesday, July 31, 2013 2:08 PM
  • If you're using powershell v3 you can make an ordered hash table by adding [ordered] in front of the hash opening , @{symbol.  Otherwise you'll need to use select to set the order.

    As for exporting to csv the way I do it is pipe the script outp[ut to a csv instead of adding it to the script itself.

    .\myscript.ps1 | export-csv .\mycsv.csv


    Hope that helps! Jason

    Wednesday, July 31, 2013 2:49 PM
  • If you aren't familiar with using New-object you may want to go out and get a book for learning PowerShell.  Don Jones has an amazing book called, Learn PowerShell in a Month of Lunches, and Ed Wilson, the Scripting guy, has a book out called Powershell step by step.  Both should cover the topic really well and being able to use things like new-object are pretty important to using powershell well.

    Hope that helps! Jason

    Wednesday, July 31, 2013 2:53 PM
  • Cheers Jason. Working as intended. 

    I have read a few items the scripting guy puts out. I read about new-object on one of his aritcles after you wrote your script above.  PowerShell is one of them things right now that I use every so often but not enough to warrent reading and learning it fully.

    Wednesday, July 31, 2013 3:25 PM
  • *Edit* - Forget this. Update at the bottom :)

    Strange issue. This is working and works fine when outputting to the console however when exporting it to a CSV the

    'Profile Size (MB)' = $ProfileSum

    Is showing a value of 0.00.
    I changed it to...

    'Profile Size (MB)' = $ProfileLength.sum

    To see if I can get the normal byte count but this is also empty so I presume that $ProfileLength is effectively empty. It works fine though if I just run the New-Object.ps1 script from a console.
    I've checked my spelling of the variables and even got someone else to look this over to see if they can see any mistakes but were both at a loss.  Any ideas?

    Here's the two scripts.

    New-Object.PS1

    Import-Module ActiveDirectory
    
    $Users = Get-AdUser -Filter * -Properties *
    
    ForEach ( $User in $Users ) 
        {
            $HomeLength = Get-ChildItem ($User.HomeDirectory) -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -property length -sum
            $ProfileLength = Get-ChildItem ($User.ProfilePath + ".V2") -Recurse -Force -ErrorAction SilentlyContinue | Measure-Object -property length -sum
    
            New-Object -TypeName PSObject -Property @{
                    SAMAccountname = $User.SamAccountName
                    Name = $User.Name
                    Enabled = $User.Enabled
                    LastLogonDate = $User.LastLogonDate
                    PasswordExpired = $User.PasswordExpired
                    LastBadPassword = $User.LastBadPasswordAttempt
                    Description = $User.Description
                    Email = $User.EmailAddress
                    HomeDir = $User.HomeDirectory
                    'HomeSize(MB)' = "{0:N2}" -f ($HomeLength.sum / 1MB)
                    'ProfilePath(MB)' = "{0:N2}" -f ($ProfileLength.sum / 1MB)
                    ProfileSize = $ProfileLength.sum
                    DistinguishedName = $User.DistinguishedName
             }
        }


    Get-UserData.PS1

    .\New-Object.ps1 | Select-Object SAMAccountname, Name, Enabled, LastLogonDate, PasswordExpired, LastBadPassword, Description, Email, ProfilePath, 'ProfilePath(MB)', HomeDir, 'HomeSize(MB)', DistinguishedName | Export-Csv "ADUsers - $((Get-Date).ToString('MMMM yyyy')).csv" -NoTypeInformation

    Hope you can help!

    Cheers,

    Andrew

    *Edit*

    Never mind, I think I have the issue sorted now but can't test it properly.  The issue I think is down to permissions.  Testing from console from my account I can see profiles but not some users home folders. The script that outputs to CSV is running on a scheduled task from another user/service account which can see home data but not profile data!  I need to get the script to have permissions to read profile folders.

    Sorry about that :(


    Wednesday, July 31, 2013 4:49 PM