locked
Get list of inactive devices RRS feed

  • Question

  • Hello,

    I am not asking for a single piece of code (well, at least not at this moment). Rather im asking for logic how to approach my goal.

    Ive share with many folders (4000+), each folder represent name of the device (server1, server2...).

    Each folder contains files (one by day) in this format:  yyyy-mm-dd-devicename.log.

    My goal is to check for new devices, if current devices are still reporting (if theyre not creating files for more than 7 days, then mark them as removed).

    So, what comes to my head..
    1. create CSV with current folder structure (device names)
    2. each week compare this CSV with current folder structure  (this might get me new devices as new foldernames will pop out)
    3. for not reporting devices, i should grab log files where $_.name match date between current date and date(-7)..  not very well explained but i think u know what i mean...   If search result count -eq 0 than  device is not reporting.

    Maybe there is another way to approach this (without need of CSV).

    Maybe I should just get folderstructure.
    1. new devices = folder, where creationdate -lt week
    2. not reporting devices = where last log file creation date -lt get-date.adddays(-7)  (something like that)..
    .. but this doesnt give me info, when the folder (devicename) is deleted by coleague (no info about removed devices). Still should need to have this info somewhere (CSV).

    Iam also a bit concerned about speed ... if use of get-childitem on 4000+ folders, each already contain around 300+ files .. is efficient.

    I would appreciate any advice. 

    Again, not asking for script, just the logic which way i should go.

    Wednesday, November 1, 2017 9:15 AM

Answers

  • Hi,

    >> .. but this doesnt give me info, when the folder (devicename) is deleted by coleague (no info about removed devices). Still should need to have this info somewhere (CSV).
    I agree with you. And based on my research, it might be convenient to export a CSV for comparison. In this case, new folder means new device, removed folder means removed device. Then make further comparisons to other folders.

    In addition, I wrote a demo script for your reference, hope it is helpful to you:
    $thisWeek = (Get-Date).ToString('yyyy-MM-dd')
    $lastWeek = (Get-Date).AddDays(-7).ToString('yyyy-MM-dd')
    Get-ChildItem -Path 'D:\Folder' -Directory | Export-Csv -Path "D:\Folder\$thisWeek.csv" -NoTypeInformation # for next week comparison
    $lastWeekData = Import-Csv -Path "D:\Folder\$lastWeek.csv"
    $thisWeekData = Import-Csv -Path "D:\Folder\$thisWeek.csv"
    $difference = Compare-Object -ReferenceObject $lastWeekData.FullName -DifferenceObject $thisWeekData.FullName -IncludeEqual
    $newDevices = $difference | Where-Object {$_.SideIndicator -eq '=>'} | Select-Object -ExpandProperty InputObject # new device
    $removedDevices = $difference | Where-Object {$_.SideIndicator -eq '<='} | Select-Object -ExpandProperty InputObject # removed device
    $otherDevices = $difference | Where-Object {$_.SideIndicator -eq '=='} | Select-Object -ExpandProperty InputObject
    foreach ($otherDevice in $otherDevices)
    {
        $logs = Get-ChildItem -Path $otherDevice | Where-Object {$_.Name -like '*.log' -and $_.CreationTime -gt $lastWeek}
        if ($logs -eq $null)
        {
            $otherDevice # mark them as removed
        }
    }

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Mekac Thursday, November 2, 2017 6:30 AM
    Thursday, November 2, 2017 5:27 AM

All replies

  • Hi,

    your second way (without csv) sounds doable at first sight and is appreciated because you don't get in trouble if you change something or loose the csv.

    I would say, if you only run it once per week, you can deal with some lacks of efficency. Also, you won't get around Get-Childitem, I guess. Otherwise, you should know how your paths are constructed and look with Test-Path for a limited number of paths. But that can get quite difficult.

    Wednesday, November 1, 2017 11:19 AM
  • Hi,

    >> .. but this doesnt give me info, when the folder (devicename) is deleted by coleague (no info about removed devices). Still should need to have this info somewhere (CSV).
    I agree with you. And based on my research, it might be convenient to export a CSV for comparison. In this case, new folder means new device, removed folder means removed device. Then make further comparisons to other folders.

    In addition, I wrote a demo script for your reference, hope it is helpful to you:
    $thisWeek = (Get-Date).ToString('yyyy-MM-dd')
    $lastWeek = (Get-Date).AddDays(-7).ToString('yyyy-MM-dd')
    Get-ChildItem -Path 'D:\Folder' -Directory | Export-Csv -Path "D:\Folder\$thisWeek.csv" -NoTypeInformation # for next week comparison
    $lastWeekData = Import-Csv -Path "D:\Folder\$lastWeek.csv"
    $thisWeekData = Import-Csv -Path "D:\Folder\$thisWeek.csv"
    $difference = Compare-Object -ReferenceObject $lastWeekData.FullName -DifferenceObject $thisWeekData.FullName -IncludeEqual
    $newDevices = $difference | Where-Object {$_.SideIndicator -eq '=>'} | Select-Object -ExpandProperty InputObject # new device
    $removedDevices = $difference | Where-Object {$_.SideIndicator -eq '<='} | Select-Object -ExpandProperty InputObject # removed device
    $otherDevices = $difference | Where-Object {$_.SideIndicator -eq '=='} | Select-Object -ExpandProperty InputObject
    foreach ($otherDevice in $otherDevices)
    {
        $logs = Get-ChildItem -Path $otherDevice | Where-Object {$_.Name -like '*.log' -and $_.CreationTime -gt $lastWeek}
        if ($logs -eq $null)
        {
            $otherDevice # mark them as removed
        }
    }

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Mekac Thursday, November 2, 2017 6:30 AM
    Thursday, November 2, 2017 5:27 AM