Unable to delete files on multiple computer

by syed_mahm at 2013-01-25 05:39:36

Hi All ,
I am very new in scripting need help to delete files on multile computers getting looping error

$Path = "C:\Temp\Scm"
$Daysback = "-60"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
$a = Get-Content "c:\Temp\srv.csv"
foreach ($a in $as){
Get-ChildItem $Path -Recurse | Where-Object { $.LastWriteTime -lt $DatetoDelete } #| Remove-Item}
delet from
$Path = "C:\Temp\fine"
$Daysback = "-30"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
$a = Get-Content "c:\temp\srv.csv"
foreach ($i in $a)
Get-ChildItem $Path -Recurse | Where-Object { $
.LastWriteTime -lt $DatetoDelete } #| Remove-Item
}
}

Error
PS C:\Temp> .\remove.ps1
Missing statement body in foreach loop.
At C:\Temp\remove.ps1:15 char:1
+ <<<< Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } #| Remove-Item
+ CategoryInfo : ParserError: (:slight_smile: [], ParseException
+ FullyQualifiedErrorId : MissingForeachStatement


my csv file has

Computername
Server1
Server2

Thanks in advance

GT
by Klaas at 2013-01-25 07:30:46
[quote="syed_mahm"]foreach ($a in $as){[/quote]You’re looping through $as, but there is no $as
[quote="syed_mahm"]Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } #| Remove-Item}[/quote] You don’t capture the output of Get-Childitem, not to a variable, not to the pipeline, so the files are send to Out-Default, which means they’re printed on your screen and that’s it.
[quote="syed_mahm"]delet from [/quote] I suppose this will give an error too. Why is this line here?
When you get your loop fixed, you will remove files only on the machine where you execute this script because you don’t use the servernames in the loop.

Maybe you should describe what you want to do in English first.
by syed_mahm at 2013-01-26 10:11:28
Klaas - All I know this script work fine when I run this script it delete any file older than 60 days and other one 30 day "SCM" and "fine" directory … now my goal is to delete same file from bunch of other cumputer remotley


$Path = "C:\Temp\Scm"
$Daysback = "-60"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse | Where-Object { $.LastWriteTime -lt $DatetoDelete } #| Remove-Item}
delet from
$Path = "C:\Temp\fine"
$Daysback = "-30"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse | Where-Object { $
.LastWriteTime -lt $DatetoDelete } #| Remove-Item
************
by DonJ at 2013-01-26 10:18:31
The easiest would be to use UNC paths and the remote computers administrative share, like \server\c$\folder\folder. No need to involve Remoting.
by syed_mahm at 2013-01-29 10:51:02
Thanks Don I use the UNC path and it work like charm