locked
Script to find files on multiple servers. RRS feed

  • Question

  • Hi all,

    I am trying to find and locate 3 files from 20 servers and export to CSV that will list out the servername and the files' location in those 20 server. The only command i know is below and i tried Get-Content but failed. Can anyone help please..

    My command:

    Get-ChildItem -include "kaguya.txt","redsun.txt","heroes.txt" -Recurse

    Tuesday, November 21, 2017 2:00 PM

Answers

  • Because the Export is inside the For loop, so you'll only have 1 record. Either add the -append switch, or try the following

    get-content c:\servers.txt | Foreach {
      Get-ChildItem -Path "\\$_\c$" -include "kaguya.txt","redsuns.txt","heroes.txt" -Recurse -ErrorAction silentlycontinue
    } | export-csv c:\Scripts\results.csv



    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Marked as answer by Bremen6 Wednesday, November 22, 2017 2:27 AM
    Tuesday, November 21, 2017 3:51 PM

All replies

  • Get-Content reads the contents of a file, Get-ChildItem is the correct on but you are missing the path to search.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Tuesday, November 21, 2017 2:12 PM
  • I'm trying to search on all drives in the servers.
    Tuesday, November 21, 2017 2:14 PM
  • If you run locally on the machine you can do something like

    Get-PsDrive -p "FileSystem" | Foreach {
      Get-ChildItem $_ -include kaguya.txt,redsun.txt,heroes.txt
    }

    Since these cmdlets do not have a computer name property, you'll need to use Invoke-Command if you want to run remotely against all machines, which means PSRemoting needs to be enabled. Otherwise, you'll have to run on each server, or need to utilize UNC paths. 


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''


    • Edited by clayman2 Tuesday, November 21, 2017 2:46 PM typo
    Tuesday, November 21, 2017 2:45 PM
  • I manage to run the script below and it works fine if i have 1 server in the servers.txt, but fails if i list more than 1 server in the servers.txt . Im planning put 20 server names in the servers.txt file..I'm not familiar Invoke-Command and PSremoting...

    $server=get-content c:\servers.txt
    Foreach ($srv in $server) {Get-ChildItem -Path "\\$srv\c$" -include "kaguya.txt","redsuns.txt","heroes.txt"         -Recurse -ErrorAction silentlycontinue | export-csv c:\Scripts\results.csv}

    Tuesday, November 21, 2017 3:03 PM
  • What failure do you get when you add more than one server? Are you listing the server names on their own line in the servers.txt file?

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Tuesday, November 21, 2017 3:26 PM
  • If i list more than 1 server, the CSV file will give no results (no content/empty result in the csv file). If i list 1 server, it will produce the results. I am listing the server names 1 server per row as below in the servers.txt file

    Server01
    Server02
    Server03


    Tuesday, November 21, 2017 3:36 PM
  • Because the Export is inside the For loop, so you'll only have 1 record. Either add the -append switch, or try the following

    get-content c:\servers.txt | Foreach {
      Get-ChildItem -Path "\\$_\c$" -include "kaguya.txt","redsuns.txt","heroes.txt" -Recurse -ErrorAction silentlycontinue
    } | export-csv c:\Scripts\results.csv



    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Marked as answer by Bremen6 Wednesday, November 22, 2017 2:27 AM
    Tuesday, November 21, 2017 3:51 PM