Solved

PowerShell, deleting files from multiple servers

Posted on 2016-11-03
12
26 Views
Last Modified: 2016-11-11
Hello experts. So I have this script below that deletes files from multiple servers > x days old. However, I am new to powershell and feel there is a better way to do this. For example I would like to use servers listed in a file. Below this script is the new one i am working on.
----------------------------------------------------------------------------------------------------------------------------------------------------
$limit = (Get-Date).AddDays(-15)
$path10 = "\\server10\C$\Users\dax\AppData\Local\Temp"
$path11 = "\\server11\C$\Users\dax\AppData\Local\Temp"
$path12 = "\\server12\C$\Users\dax\AppData\Local\Temp"
$path13 = "\\server13\C$\Users\dax\AppData\Local\Temp"
$path14 = "\\server14\C$\Users\dax\AppData\Local\Temp"

# Delete files older than the $limit.
Get-ChildItem -Path $path10 -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
Get-ChildItem -Path $path11 -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
Get-ChildItem -Path $path12 -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
Get-ChildItem -Path $path13 -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
Get-ChildItem -Path $path14 -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
----------------------------------------------------------------------------------------------------------------------------------------------------

here is the new script i am attempting
----------------------------------------------------------------------------------------------------------------------------------------------------
$Servers = Get-Content D:\Scripts\servers.txt
$limit = (Get-Date).AddDays(-15)
$path = "C$\Users\dax\AppData\Local\Temp"

Foreach ($Computer in $Computers)
{
    Invoke-Command -ComputerName $Servers {Get-ChildItem -Path $path10 -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force -WhatIf}
}

Can you please help me determine what i am doing wrong? Or let me know if there is a better way altogether to compete this task. Thank you in advanced.
0
Comment
Question by:jacobJL
  • 7
  • 5
12 Comments
 
LVL 14

Expert Comment

by:Jason Crawford
ID: 41872818
Try this:

Invoke-Command -ComputerName (Get-Content servers.txt) -ScriptBlock {Get-ChildItem c:\Users\dax\AppData\Local\Temp -Directory | Remove-Item -Force}

Open in new window

0
 
LVL 14

Expert Comment

by:Jason Crawford
ID: 41872822
The benefit of the method employed in the one-liner above is it will "fan" across all servers at the same time instead of one after the other the way you would see in a foreach loop.
0
 
LVL 14

Expert Comment

by:Jason Crawford
ID: 41872824
Hold up I forgot to specify the age limit.  Don't make my same mistake and just add that portion into the script block.
0
 

Author Comment

by:jacobJL
ID: 41872871
Jason thank you for quick response.

I am not sure what you mean by this "Hold up I forgot to specify the age limit.  Don't make my same mistake and just add that portion into the script block."
0
 
LVL 14

Expert Comment

by:Jason Crawford
ID: 41872877
Yea sorry I'm doing too many things at once.  Here is a much cleaner option (requires Powershell 3.0 or above):
Invoke-Command -ComputerName (Get-Content servers.txt) -ScriptBlock {
  $date = (Get-Date).AddDays(-3)
  Set-Location 'c:\Users\dax\AppData\Local\Temp'
  Get-ChildItem -Directory | Where-Object {$_.LastWriteTime -lt $date} | Remove-Item -Force
}

Open in new window

0
 

Author Comment

by:jacobJL
ID: 41872899
I see. I am getting an error.
"Cannot find path 'C:\Windows\system32\servers.txt' because it does not exist."
Not sure why its looking on c when i specify D:\Scripts\servers.txt

This is what i have
$Servers = Get-Content D:\Scripts\servers.txt

Invoke-Command -ComputerName (Get-Content servers.txt) -ScriptBlock {
  $date = (Get-Date).AddDays(-10)
  Set-Location 'c:\Users\dax\AppData\Local\Temp'
  Get-ChildItem -Directory | Where-Object {$_.LastWriteTime -lt $date} | Remove-Item -Force -WhatIf
}

Get-Content : Cannot find path 'C:\Windows\system32\servers.txt' because it does not exist.
At line:5 char:31
+ Invoke-Command -ComputerName (Get-Content servers.txt) -ScriptBlock {
+                               ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Windows\system32\servers.txt:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
 
Invoke-Command : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:5 char:30
+ Invoke-Command -ComputerName (Get-Content servers.txt) -ScriptBlock {
+                              ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Invoke-Command], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 14

Expert Comment

by:Jason Crawford
ID: 41872905
Ok try this:

Invoke-Command -ComputerName (Get-Content 'D:\Scripts\servers.txt') -ScriptBlock {
  $date = (Get-Date).AddDays(-3)
  Set-Location 'c:\Users\dax\AppData\Local\Temp'
  Get-ChildItem -Directory | Where-Object {$_.LastWriteTime -lt $date} | Remove-Item -Force
}

Open in new window

0
 

Author Comment

by:jacobJL
ID: 41872954
Hmm, no error but It's not removing anything.  is "Get-ChildItem -Directory" correct?

if i run:
 Invoke-Command -ComputerName (Get-Content 'D:\NCR\DAX\Scripts\servers.txt') -ScriptBlock {
  $date = (Get-Date).AddDays(-10)
  Set-Location 'c:\Users\dax\AppData\Local\Temp'
  Get-ChildItem -Directory}

it returns each sub-directory in that location on each server. But doesn't return the files in directory
0
 
LVL 14

Accepted Solution

by:
Jason Crawford earned 500 total points
ID: 41872960
My fault I didn't see the exclamation point in your script.  Ok last time hopefully :)

Invoke-Command -ComputerName (Get-Content 'D:\Scripts\servers.txt') -ScriptBlock {
  $date = (Get-Date).AddDays(-3)
  Set-Location 'c:\Users\dax\AppData\Local\Temp'
  Get-ChildItem -File | Where-Object {$_.LastWriteTime -lt $date} | Remove-Item -Force
}

Open in new window

0
 

Author Closing Comment

by:jacobJL
ID: 41872979
Bingo. Thank you so much! I learned a lot from this. I'll have to research -scriptblock now and include that in some of my other scripts. That's awesome. Really liking how powerful powershell is.
0
 
LVL 14

Expert Comment

by:Jason Crawford
ID: 41872993
1
 

Author Comment

by:jacobJL
ID: 41883925
Hey Jason if you are interested I opened another question in relation to this one.
https://www.experts-exchange.com/questions/28982543/PowerShell-deleting-files-using-last-modified-and-last-accessed.html
Thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now