Solved

PowerShell, deleting files from multiple servers

Posted on 2016-11-03
12
47 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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
 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

825 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