File search - multiple remote servers - specific file types?


This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Istvan Szarka Istvan Szarka 2 years, 10 months ago.

  • Author
  • #16011
    Profile photo of Jake Sully
    Jake Sully

    I'm trying to find out how to write a script that will search a list of servers in a text file for specific file types, then report into a CSV file with attributes that are: size, last access date, full path, file name (full path – if that includes the name that's cool), and owner.

    Can anyone help me as I'm new and not sure at all how to do this.

  • #16014
    Profile photo of Dave Wyatt
    Dave Wyatt

    That type of thing would be very slow over the network. Ideally, you'd have PSRemoting enabled on all of the servers, and could have them run a local search via Invoke-Command. Even then, though, you're using a "push" model for this inventory collection; any target computers that are not responding at the time that the script is executed would be missed.

    Personally, I would prefer not to have to script the delivery of this at all. If you have any sort of management software in your environment (such as SCCM), it should be able to facilitate the inventory in a more reliable way. You might still be writing the same client-side script, but without the fragile push model of deployment.

  • #16047
    Profile photo of Istvan Szarka
    Istvan Szarka


    I've written a one-liner as an example. It gets the computer names from a text file, then gets the files with the desired extensions on the system drive on each computer, gets the properties you need and exports it to a csv.
    I'm a beginner in PowerShell too, so I'm sure my example isn't the shortest and smartest, but it does what you described. Invoke-Command puts a few extra columns into the csv file, but everything you need is there too.

    Invoke-Command -ComputerName (Get-Content C:\computers.txt) -ScriptBlock {dir c:\ -Recurse -File |
    where {$_.Extension -eq ".txt" -or $_.Extension -eq ".ps1" -or $_.Extension -eq ".docx"} |
    select Length,LastAccessTime,Name,@{n='Path';e={$_.fullname}},@{n='Owner';e={$_.getaccesscontrol().owner}}} |
    Export-Csv C:\files.csv -NoTypeInformation


    If I use $env:SystemDrive instead of just c:\, the command generates no output at all. Do you know why?
    If this push method isn't very reliable, isn't it possible to use the custom script DSC resource (sorry, I don't know its exact name) and a pull server?
    Then, the servers would pull the script and could append the results onto a csv file in a file share. What about that?

You must be logged in to reply to this topic.