• I appreciate any assistance you all can give.

    0
  • SysOps Forum Team

    Just to let you know I am kind for new (newbie) to PowerShell scripting.  I have put together this script below to retrieve data from Active Directory (Name, OperatingSystem, IPV4, Last Login, & calculate and output Days Since Last Logon).  That being said, it does exactly what I initially built it to do.  Since I originally wrote this we have grown a bit and I need to make a couple of changes that I am not sure how to complete.

    First I would like to make the "Retrieve AD Data" into a function so that I apply requests for multiple locations (i.e. San Diego, San Antonio, Charlotte)

    Second I would like to group each location by

    - (i.e. San Diego’s – SD02, SD03, SD04)

    - (i.e. San Antonio’s – SA01, SA03, SA04, SA05)

    - (i.e. Charlotte’s – CL01, CL02)

    So the output that I would like is

    A. One report that outputs each: (preferred)

    - Grouped by: San Diego, San Antonio, & Charlotte
    - Grouped by: Server Groupings
    - Sorted by: Days Since Last Logon

    OR

    B. Separate reports for each (may be easier)
    - Grouped by: Server Groupings
    - Sorted by: Days Since Last Logon

    Attempt to Resolve
    I did make an attempt to user Group-Object (probably incorrectly) and while it did group them, it ended up adding a lot of extraneous information to the right of the “Name” column (pushing it all the way to the right) and excluding all previous columns to the right that was previously there (Shown below).

    Current Script

    ###########################################################
    
    # Path to localhost system
    
    $LH="C:TempSvrUpTime"
    
    # Define Systems to be checked (by location)
    
    Get_ADComputer -Sever us.contoso.com -Filter {(Name -Like 'SD02*') -or (Name -Like 'SD03*') -or (Name -Like 'SD04*')} |
    
    Where-Object {($_.Name -notlike '*NAPP*') -and ($_.Name -notlike '*SBU*')} |
    
    # Retrieve AD Data (name, OS, IP, Last Boot)  (want to turn this into a function)
    
    Select-Object Name,OperatingSystem,IPv4Address,@{Name="Last Log On";  Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp).ToString("MM/DD/YYYY")}} @{Name="Days Since Last Logon"; Expr={ ([timespan]((Get-Date) - ([DateTime]$_.LastLogonTimestamp).AddYears(1600))).Days +1; }} |
    
    Sort-Object "Days Since Last Logon" - Descending |
    
    # Export CVS   (added to each process after function call?)
    
    Export-CVS $LHSanDiego-Stats.csv -NoTypeInformation
    
    # Import to view out
    
    Import-Csv -Path $LHSanDiego-Stats.csv | Format-Table
    
    ###########################################################
    
    

    Attempt to use Group-Object:

    Values                                                              Count                    Group                                                                                   Name
    
    ------                                                                   -----                       -----                                                                                        ----
    
    System.Collections.ArrayList                     1                              System.Collections.ObjectModel.Collection`1[... SA03B19-ACAS-01
    
    System.Collections.ArrayList                     1                              System.Collections.ObjectModel.Collection`1[... SA03B19-SCCM-01
    
    System.Collections.ArrayList                     1                              System.Collections.ObjectModel.Collection`1[... SA03B19-ORCH-01
    
    System.Collections.ArrayList                     1                              System.Collections.ObjectModel.Collection`1[... SA03B19-DoCo-01
    
    System.Collections.ArrayList                     1                              System.Collections.ObjectModel.Collection`1[... SA03B19-DoCo-02
    
    System.Collections.ArrayList                     1                              System.Collections.ObjectModel.Collection`1[... SA04B20-ACAS-01
    
    System.Collections.ArrayList                     1                              System.Collections.ObjectModel.Collection`1[... SA04B20-SCCM-01
    
    System.Collections.ArrayList                     1                              System.Collections.ObjectModel.Collection`1[... SA04B20-ORCH-01
    
    System.Collections.ArrayList                     1                              System.Collections.ObjectModel.Collection`1[... SA04B20-DoCo-01
    
    System.Collections.ArrayList                     1                              System.Collections.ObjectModel.Collection`1[... SA04B20-DoCo-02
    
    System.Collections.ArrayList                     1                              System.Collections.ObjectModel.Collection`1[... SA02B19-DoCo-01
    0
  • 0
  • M Fix became a registered member 1 month, 1 week ago

    0