Network Folder delete

  • Follow


Our AV solution makes temp files on all our servers in the Windows directory. 
Im looking for a script I can run locally that reads from a serverlist.txt -> 
goes out and deletes all folders and their contenets in the following 
directory:   C:\WINDOWS\SoftwareDistribution\ScanFile - except for today.

I've searched high and low and seen pieces, such as reading over the 
network, searching for files based on date, deleting files (not folders), but 
nothing quite like my request. Any scripting help would of course greatly be 
appreciated! Thanks!
0
Reply Utf 1/13/2010 10:00:01 PM

Let me add the following process below - perhaps someone can translate it to 
powershell? Thanks

$a = Get-Content C:\Scripts\serverlist.txt
foreach $a
Net Use T$ \\a$\c$\WINDOWS\SoftwareDistribution\ScanFile
dir | sort-object LastWriteTime
last write directory = Z$
delete eveerything but Z$
net use t$ /delete

(contine looping through servers until complete)
0
Reply Utf 1/14/2010 4:25:01 PM

Nothing like replying to your own thread - ok Im closer - I think -- anyone?
-instead of delete - lets just try try to count the files in the remote 
share ? anyone?

(this fails)

$server = "c:\scripts\serverlist2.txt" 
foreach {set-location -path \\$server\system32\ | (dir).count}
0
Reply Utf 1/14/2010 10:01:01 PM

I think this will get the job done.  This is assuming that you are only 
wanting to delete folders in the ScanFile folder.  

"Ian_1" wrote:

> Let me add the following process below - perhaps someone can translate it to 
> powershell? Thanks
> 
> $a = Get-Content C:\Scripts\serverlist.txt
> foreach $a
> Net Use T$ \\a$\c$\WINDOWS\SoftwareDistribution\ScanFile
> dir | sort-object LastWriteTime
> last write directory = Z$
> delete eveerything but Z$
> net use t$ /delete
> 
> (contine looping through servers until complete)

$servers = Get-Content c:\scripts\serverlist.txt

foreach($server in $servers)
{
	$folders = gci "\\$server\c$\windows\softwaredistribution\scanfile" | where 
{$_.psiscontainer -eq $true}
	foreach ($folder in $folders)
	{
		$lastwrite = $folder.lastwritetime.date
		$today = (Get-Date).date
		
		If ($lastwrite -lt $today)
		{
			#$folder | Remove-Item
			$folder
		}
	}
}
0
Reply Utf 1/15/2010 1:00:01 AM


"mayben" wrote:

> I think this will get the job done.  This is assuming that you are only 
> wanting to delete folders in the ScanFile folder.  
> 
> "Ian_1" wrote:
> 
> > Let me add the following process below - perhaps someone can translate it to 
> > powershell? Thanks
> > 
> > $a = Get-Content C:\Scripts\serverlist.txt
> > foreach $a
> > Net Use T$ \\a$\c$\WINDOWS\SoftwareDistribution\ScanFile
> > dir | sort-object LastWriteTime
> > last write directory = Z$
> > delete eveerything but Z$
> > net use t$ /delete
> > 
> > (contine looping through servers until complete)
> 
> $servers = Get-Content c:\scripts\serverlist.txt
> 
> foreach($server in $servers)
> {
> 	$folders = gci "\\$server\c$\windows\softwaredistribution\scanfile" | where 
> {$_.psiscontainer -eq $true}
> 	foreach ($folder in $folders)
> 	{
> 		$lastwrite = $folder.lastwritetime.date
> 		$today = (Get-Date).date
> 		
> 		If ($lastwrite -lt $today)
> 		{
> 			#$folder | Remove-Item
> 			$folder
> 		}
> 	}
> }

Whoops.  To get it to delete just uncommend #$folder | Remove-Item
0
Reply Utf 1/15/2010 1:10:01 AM

I think your close, Im getting the following error though:

PS C:\scripts> C:\scripts\del_files.ps1

cmdlet where at command pipeline position 2
Supply values for the following parameters:
FilterScript:
Where-Object : Cannot bind parameter 'FilterScript'. Cannot convert value "" 
to type "System.Management.Automation.Scri
ptBlock". Error: "Invalid cast from 'System.String' to 
'System.Management.Automation.ScriptBlock'."
At C:\scripts\del_files.ps1:4 char:77
+ {$folders = gci "\\$server\c$\windows\softwaredistribution\scanfile" | 
where  <<<<
$_.psiscontainer -eq $true
Remove-Item : Cannot bind argument to parameter 'Path' because it is null.
At C:\scripts\del_files.ps1:10 char:23
+ {$folder | Remove-Item <<<<

cmdlet where at command pipeline position 2
Supply values for the following parameters:
FilterScript:
Where-Object : Cannot bind parameter 'FilterScript'. Cannot convert value "" 
to type "System.Management.Automation.Scri
ptBlock". Error: "Invalid cast from 'System.String' to 
'System.Management.Automation.ScriptBlock'."
At C:\scripts\del_files.ps1:4 char:77
+ {$folders = gci "\\$server\c$\windows\softwaredistribution\scanfile" | 
where  <<<<
$_.psiscontainer -eq $true
Remove-Item : Cannot bind argument to parameter 'Path' because it is null.
At C:\scripts\del_files.ps1:10 char:23
+ {$folder | Remove-Item <<<<

cmdlet where at command pipeline position 2
Supply values for the following parameters:
FilterScript:
Where-Object : Cannot bind parameter 'FilterScript'. Cannot convert value "" 
to type "System.Management.Automation.Scri
ptBlock". Error: "Invalid cast from 'System.String' to 
'System.Management.Automation.ScriptBlock'."
At C:\scripts\del_files.ps1:4 char:77
+ {$folders = gci "\\$server\c$\windows\softwaredistribution\scanfile" | 
where  <<<<
$_.psiscontainer -eq $true
Remove-Item : Cannot bind argument to parameter 'Path' because it is null.
At C:\scripts\del_files.ps1:10 char:23
+ {$folder | Remove-Item <<<<
0
Reply Utf 1/15/2010 2:41:01 AM

Karl the script works great but just for counting. So if I wanted to change 
it to list the contaning directories, or sort and display the newest only, or 
best yet, delete everything- the commands at the end, I must be not using the 
correct format and I have replaced .count with everything from .dir alias, 
dir | where { $_.MshIsContainer }, dir | sort-object LastWriteTime,  del *, 
etc.  Nothing seems to work for those options. Am I using the incorrect 
process for this? Thanks
0
Reply Utf 1/15/2010 3:27:01 AM

That's odd because this script as it is works fine in the test I'm doing.

"Ian_1" wrote:

> I think your close, Im getting the following error though:
> 
> PS C:\scripts> C:\scripts\del_files.ps1
> 
> cmdlet where at command pipeline position 2
> Supply values for the following parameters:
> FilterScript:
> Where-Object : Cannot bind parameter 'FilterScript'. Cannot convert value "" 
> to type "System.Management.Automation.Scri
> ptBlock". Error: "Invalid cast from 'System.String' to 
> 'System.Management.Automation.ScriptBlock'."
> At C:\scripts\del_files.ps1:4 char:77
> + {$folders = gci "\\$server\c$\windows\softwaredistribution\scanfile" | 
> where  <<<<
> $_.psiscontainer -eq $true
> Remove-Item : Cannot bind argument to parameter 'Path' because it is null.
> At C:\scripts\del_files.ps1:10 char:23
> + {$folder | Remove-Item <<<<
> 
> cmdlet where at command pipeline position 2
> Supply values for the following parameters:
> FilterScript:
> Where-Object : Cannot bind parameter 'FilterScript'. Cannot convert value "" 
> to type "System.Management.Automation.Scri
> ptBlock". Error: "Invalid cast from 'System.String' to 
> 'System.Management.Automation.ScriptBlock'."
> At C:\scripts\del_files.ps1:4 char:77
> + {$folders = gci "\\$server\c$\windows\softwaredistribution\scanfile" | 
> where  <<<<
> $_.psiscontainer -eq $true
> Remove-Item : Cannot bind argument to parameter 'Path' because it is null.
> At C:\scripts\del_files.ps1:10 char:23
> + {$folder | Remove-Item <<<<
> 
> cmdlet where at command pipeline position 2
> Supply values for the following parameters:
> FilterScript:
> Where-Object : Cannot bind parameter 'FilterScript'. Cannot convert value "" 
> to type "System.Management.Automation.Scri
> ptBlock". Error: "Invalid cast from 'System.String' to 
> 'System.Management.Automation.ScriptBlock'."
> At C:\scripts\del_files.ps1:4 char:77
> + {$folders = gci "\\$server\c$\windows\softwaredistribution\scanfile" | 
> where  <<<<
> $_.psiscontainer -eq $true
> Remove-Item : Cannot bind argument to parameter 'Path' because it is null.
> At C:\scripts\del_files.ps1:10 char:23
> + {$folder | Remove-Item <<<<
0
Reply Utf 1/15/2010 1:55:04 PM

Is your server text file CSV or some other format? the script is stopping 
like its prompting for something. I press enter, or even try a server name 
and it errors out.
0
Reply Utf 1/15/2010 3:10:01 PM

This script stays local to the location where I run it "C:\scrips" - it 
doesn't go to the remote server C$ location.

filter Files {
if (!$_.psIsContainer) 
{
	$_
}

Get-Content "c:\scripts\serverlist2.txt" | foreach {$FilesToDelete = 
@(Get-ChildItem 
"\\$_\c$\Windows\SoftwareDistribution\ScanFile\*" |Files |Sort-Object 
LastWriteTime 
-Descending)}

$FilesToDelete[1..($FilesToDelete.count -1)] |Remove-Item -WhatIf

}

---------------------------------------------------------
0
Reply Utf 1/18/2010 3:43:01 PM

I am having a simlar issue:
I took 
this<http://www.networknet.nl/apps/wp/published/powershell-delete-files-older-than-x-days> and tweaked it.

$Now = Get-Date
$Days = “30”
$TargetFolder = “C:\Test”
$LastWrite = $Now.AddDays(-$days)
$Files = get-childitem $TargetFolder -recurse | Where {$_.LastWriteTime -le 
“$LastWrite”} 

foreach ($File in $Files)
{write-host “Deleting File $File.FullName” -foregroundcolor “Red” ; 
Remove-Item $File.FullName -recurse -force } 

Anyhow this works great if there are files it can delete.  If there are no 
files it gives
"
PS C:\Utilities\PS\Delete.old.files> 
C:\Utilities\PS\Delete.old.files\DeleteFiles30PlusDays.ps1
Deleting File .FullName
Remove-Item : Cannot bind argument to parameter 'Path' because it is null.
At C:\Utilities\PS\Delete.old.files\DeleteFiles30PlusDays.ps1:8 char:80
+ {write-host “Deleting File $File.FullName” -foregroundcolor “Red” ; 
Remove-Item <<<<  $File.FullName -recurse -force } 
    + CategoryInfo          : InvalidData: (:) [Remove-Item], 
ParameterBindingValidationException
    + FullyQualifiedErrorId : 
ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RemoveItemCommand
 "

I think the issue is that is trying to delete null.  (No logic to detect 
empty directory.)
0
Reply Utf 3/25/2010 5:03:02 PM

10 Replies
2944 Views

(page loaded in 0.195 seconds)

6/6/2014 8:11:21 PM


Reply: