0

Good morning,

Hopefully this will be a quick and easy one to answer.

I am trying to run a PS script and have it export to csv based on a list of IP addresses from a text file. At the moment, it will run but only produce one csv.

Code Revision 1

$computers = get-content "pathway.txt"
$source = "\\$computer\c$"

foreach ($computer in $computers) {
   
   Get-ChildItem -Path "\\$Source\c$" -Recurse -Force -ErrorAction SilentlyContinue |
   Select-Object Name,Extension,FullName,CreationTime,LastAccessTime,LastWriteTime,Length |
   
Export-CSV -Path "C:\path\$computer.csv" -NoTypeInformation
}

Edit

The script is now creating the individual server files as needed and I did change the source .txt file to list the servers by HostName rather than IP. The issue now is that no data is populating in the .csv files. It will create them but nothing populates. I have tried different source file paths to see if maybe its due to folder permissions or just empty but nothing seems to populate in the files.

The $computer file lists a number of server IP addresses so the script should run against each IP and then write out to a separate csv file with the results, naming the csv file the individual IP address accordingly.

Does anyone see any errors in the script that I provided, that would prevent it from writing out to a separate csv with each run? I feel like it has something to do with the foreach loop but I cannot seem to isolate where I am going wrong.

Also, I cannot use any third-party software as this is a closed network with very strict FW rules so I am left with powershell (which is okay). And yes this will be a very long run for each of the servers but I am okay with that.

Edit I did forget to mention that when I run the script, I get an error indicating that the export-csv path is too long which doesn't make any sense unless it is trying to write all of the IP addresses to a single name.

"Export-CSV : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters. At line:14 char:1

TIA

2
  • $output = "C:\path\$computer.csv" creates a static (incorrect) path outside your loop which will never change inside your loop. In other words, you will need to bring this statement inside the loop, but also note that you have two different $source variables defined in your script. Commented Jan 5, 2023 at 17:35
  • Okay, i think we are getting somewhere. $computers = get-content "pathway.txt" $source = "\\$computer\c$" foreach ($computer in $computers) { Get-ChildItem -Path "\\$Source\c$" -Recurse -Force -ErrorAction SilentlyContinue | Select-Object Name,Extension,FullName,CreationTime,LastAccessTime,LastWriteTime,Length | Export-CSV -Path "C:\path\$computer.csv" -NoTypeInformation } The changes did create individual .csv files but it looks like there was no data that populated. I did get rid of the $output as it didnt seem needed. Does the updated code look better? Commented Jan 5, 2023 at 17:45

2 Answers 2

1

Running the script against C: Drive of each computer is strongly not advisable that too with Recurse option. But for your understanding, this is how you should pass the values to the variables. I haven't tested this code.

$computer = get-content "pathway.txt"
foreach ($Source in $computer) {
   
   Get-ChildItem -Path "\\$Source\c$" -Recurse -Force -ErrorAction SilentlyContinue |
   Select-Object Name,Extension,FullName,CreationTime,LastAccessTime,LastWriteTime,Length | Export-Csv -Path "C:\Path\$source.csv" -NoTypeInformation
}

$computer will hold the whole content and foreach will loop the content and $source will get one IP at a time. I also suggest instead of IP's you can have hostname so that your output file have servername.csv for each server.

Sign up to request clarification or add additional context in comments.

1 Comment

I did go ahead and create a new source file with the HostNames listed (I agree they are easier to sort through). The only reason I started with the IP's is so I could confirm there shouldnt be any DNS issues. Cause......you know, DNS. I am also working on modifying the script to only look at certain folders on the C:\ so that the return file is not 28k lines long.
0

In hopes that this helps someone else. I have finally got the script to run and create the individual .csv files for each server hostname.

$servers = Get-Content "path"

Foreach ($server in $servers)
{
Get-ChildItem -Path "\\$server\c$" -Recurse -Force -ErrorAction SilentlyContinue |

Select-Object Name,Extension,FullName,CreationTime,LastAccessTime,LastWriteTime,Length |

Export-CSV -Path "path\$server.csv" -NoTypeInformation
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.