0

I wrote a script to delete temporary files/folders older than 90 days on remote servers. The server.txt file is loaded with Get-Content, and I use 'net use' to map to the IPC$ share. I'm worried that I'm not using Best Practices to delete the old temp files. Here is the meat of my script:

net use \\$server\IPC$ /user:$Authname $pw /persistent:yes
Get-ChildItem -Path "\\$($server)\C$\Temp" -Recurse | Where-Object {!$_.PSIsContainer -and $_.LastAccessTime -lt $cutoffdate} | Remove-Item -Recurse
(Get-ChildItem -Path "\\$($server)\C$\Temp" -recurse | Where-Object {$_.PSIsContainer -eq $True}) | Where-Object {$_.GetFiles().Count -eq 0} | Remove-Item -Recurse
net use \\$Server\IPC$ /delete

The first gci deletes old files, the second deletes empty folders.

The reason I'm concerned is that in my initial tests, it's taking about a half hour to delete approximately 4 gb off of one server. And I work in a big shop; my script needs to be run against about 10,000 servers. At that rate my script won't be done for more than six months, and I was hoping to run it on a quarterly basis.

Am I doing something the hard way?

3
  • 3
    If at all possible I would run the script in a remote session on each server. Then you could run it as jobs for each server, and run them all at the same time. Commented Sep 24, 2015 at 18:19
  • If you update to ps 4 you can use the -file and -container switches of GCI which filter much much faster than piping the objects to where, even with the remaining where being necessary. Also on the first GCI I don't believe -recurse would be necessary for remove-item. Commented Sep 24, 2015 at 18:22
  • Is this pretty much exactly what the powershell job abstraction exists for? Commented Sep 24, 2015 at 19:29

1 Answer 1

2
  1. get a list of your servers
  2. cycle through the list and use invoke-command -computername
  3. your command will be executed on the remote server rather than pulling all the data across the network which is very slow
Sign up to request clarification or add additional context in comments.

2 Comments

I agree, from experience, all those extra calls across the network are almost certainly your bottleneck.
also, invoke-command can accept multiple computer names and will parallelize the task for you

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.