locked
Loop output into table instead of line by line RRS feed

  • Question

  • Hello All,

    I'm extremely new to powershell so please excuse my amateurish script =).  Basically we have multiple AD forests and I'm just trying to get a report of inactive users, computers, empty groups..... just some metrics.  However when I run my script it is not putting everything under 1 column.  It's basically doing what I'm telling it to do not what I mean it to do lol.

    $domains = "domain1.com", "domain2.com"...etc

    $(Foreach($domain in $domains)
    {
        Get-ADDomain -server $domain | select dnsroot,
            @{n='Total Users';e={(get-aduser -Filter * -server $domain ).count}},
            @{n='Inactive Users(180)';e={(Search-ADAccount -UsersOnly -AccountInactive -server $domain -TimeSpan (New-TimeSpan -days 180)).count}},
            @{n='Total Computers';e={(get-adcomputer -Filter * -server $domain ).count}},
            @{n='Inactive Computers(180)';e={(Search-ADAccount -ComputersOnly -AccountInactive -server $domain -TimeSpan (New-TimeSpan -days 180)).count}},
            @{n='Total Groups';e={(get-adgroup -Filter * -server $domain).count}} ,
            @{n='Empty Groups';e={(Get-ADGroup -Filter * -server $domain -Properties Members | where {-not $_.members}).count}}| ft -AutoSize
    })

    Output looks like this:

    dnsroot       Total Users Inactive Users(180) Total Computers Inactive Computers(180) Total Groups Empty Groups
    -------       ----------- ------------------- --------------- ----------------------- ------------ ------------
    domain1.com         934                 702            3291                    1554          187           34



    dnsroot         Total Users Inactive Users(180) Total Computers Inactive Computers(180) Total Groups Empty Groups
    -------         ----------- ------------------- --------------- ----------------------- ------------ ------------

    domain2.com        1094                 970            2830                    2350          392          150

    But I want it to look like this:

    dnsroot       Total Users Inactive Users(180) Total Computers Inactive Computers(180) Total Groups Empty Groups
    -------       ----------- ------------------- --------------- ----------------------- ------------ ------------
    InfoSolCo.Com         934                 702            3291                    1554          187           34
    firstamdata.net        1094                 970            2830                    2350          392          150

    Excuse the formatting (something happens when I copy and paste here), but hope you get my drift.  I know there are similar posts but while reading them, I can't figure how to apply it to what I'm trying to do.

    Thanks in advance!!!


    -C-

    Wednesday, May 18, 2016 6:06 PM

Answers

  • Hi,

    Move your Format-Table outside of the loop.


    • Proposed as answer by mukesh-kumar Thursday, May 19, 2016 8:55 AM
    • Marked as answer by Charlie1313 Friday, May 20, 2016 6:14 PM
    Wednesday, May 18, 2016 6:29 PM

All replies

  • Hi,

    Move your Format-Table outside of the loop.


    • Proposed as answer by mukesh-kumar Thursday, May 19, 2016 8:55 AM
    • Marked as answer by Charlie1313 Friday, May 20, 2016 6:14 PM
    Wednesday, May 18, 2016 6:29 PM
  • You've put it in the incorrect spot. Move it all the way outside.


    EDIT: As an aside, I'd generally recommend ForEach-Object loops for something like this.

    $domains = 'dc.domain1.com','dc.domain2.com'
    
    $domains | ForEach-Object {
    
        $currentDC = $_
    
        Get-ADDomain -Server $currentDC |
            Select DNSRoot,
                   @{N='Total Users';E={(Get-ADUser -Filter * -Server $currentDC).Count}},
                   @{N='Inactive Users(180)';E={(Search-ADAccount -UsersOnly -AccountInactive -Server $currentDC -TimeSpan (New-TimeSpan -Days 180)).Count}},
                   @{N='Total Computers';E={(Get-ADComputer -Filter * -Server $currentDC).Count}},
                   @{N='Inactive Computers(180)';E={(Search-ADAccount -ComputersOnly -AccountInactive -Server $currentDC -TimeSpan (New-TimeSpan -Days 180)).Count}},
                   @{N='Total Groups';E={(Get-ADGroup -Filter * -Server $currentDC).Count}},
                   @{N='Empty Groups';E={(Get-ADGroup -Filter * -Server $currentDC -Properties Members | Where {-not $_.members}).Count}}
    
    } | Format-Table -AutoSize


    Friday, May 20, 2016 6:00 PM
  • See my edit. This is why I recommend ForEach-Object loops.

    Friday, May 20, 2016 6:02 PM
  • THis will work:

    $resukts=Foreach($domain in $domains)
     {
         Get-ADDomain -server $domain | select dnsroot,
             @{n='Total Users';e={(get-aduser -Filter * -server $domain ).count}},
             @{n='Inactive Users(180)';e={(Search-ADAccount -UsersOnly -AccountInactive -server $domain -TimeSpan (New-TimeSpan -days 180)).count}},
             @{n='Total Computers';e={(get-adcomputer -Filter * -server $domain ).count}},
             @{n='Inactive Computers(180)';e={(Search-ADAccount -ComputersOnly -AccountInactive -server $domain -TimeSpan (New-TimeSpan -days 180)).count}},
             @{n='Total Groups';e={(get-adgroup -Filter * -server $domain).count}} ,
             @{n='Empty Groups';e={(Get-ADGroup -Filter * -server $domain -Properties Members | where {-not $_.members}).count}}
    
    
    } 
    
    $results | ft -autosize

    Using ForEach-Object would be more efficient.


    \_(ツ)_/

    Friday, May 20, 2016 6:07 PM
  • You're right....works great! Thanks!

    $(Foreach($domain in $domains)
    {
        Get-ADDomain -server $domain | select dnsroot,
            @{n='Total Users';e={(get-aduser -Filter * -server $domain ).count}},
            @{n='Inactive Users(180)';e={(Search-ADAccount -UsersOnly -AccountInactive -server $domain -TimeSpan (New-TimeSpan -days 180)).count}},
            @{n='Total Computers';e={(get-adcomputer -Filter * -server $domain ).count}},
            @{n='Inactive Computers(180)';e={(Search-ADAccount -ComputersOnly -AccountInactive -server $domain -TimeSpan (New-TimeSpan -days 180)).count}},
            @{n='Total Groups';e={(get-adgroup -Filter * -server $domain).count}} ,
            @{n='Empty Groups';e={(Get-ADGroup -Filter * -server $domain -Properties Members | where {-not $_.members}).count}}
    }) | ft -autosize


    -C-

    Friday, May 20, 2016 6:15 PM
  • Cheers, you're very welcome. Glad you got it working.

    I do recommend looking over the ForEach-Object version that I posted for any future adjustments. It'll make your life easier down the road.


    Friday, May 20, 2016 6:17 PM