Link to home
Start Free TrialLog in
Avatar of winsane
winsane

asked on

Scripting maintenance jobs

I need 4 scripts to run on a Windows 2003 network.
a. Find keywords in title of the files. For example, find all files with word "tmp" in the text of the filename.
b. Find files that are zero bytes.
c. Find files bigger than 10 megabytes.
d. Find duplicate files.

Where can I get these types of scripts? I do not yet know how to write something like this...
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

This is super simple in Powershell

a) get-ChildItem <PathToStartSearch> -rec | Where-Object{$_.name -match "tmp"} | foreach{$_.fullnname}
Example: get-ChildItem \\server1\c$ -rec | Where-Object{$_.name -match "tmp"} | foreach{$_.fullnname}

b) get-ChildItem <PathToStartSearch> -rec |Where-Object{(!$_.PSIsContainer) -and ($_.Length -eq 0)} | foreach{$_.fullname}
Example: get-ChildItem \\server1\c$ -rec |Where-Object{(!$_.PSIsContainer) -and ($_.Length -eq 0)} | foreach{$_.fullname}

c) get-ChildItem <PathToStartSearch> -rec |Where-Object{(!$_.PSIsContainer) -and ($_.Length -ge 10mb)} | foreach{$_.fullname}
Example: get-ChildItem \\server1\c$ -rec |Where-Object{(!$_.PSIsContainer) -and ($_.Length -ge 10mb)} | foreach{$_.fullname}

d) $files = @{}
   get-ChildItem c:\test -rec | Where-Object{(!$_.PSIsContainer)} | foreach{if(!($files."$_.Name")){$files."$_.Name" = $_.FullName}else{write-Host $_.FullName}}
Example:
$files = @{}
get-ChildItem c:\ -rec | Where-Object{(!$_.PSIsContainer)} | foreach{if(!($files."$_.Name")){$files."$_.Name" = $_.FullName}else{write-Host $_.FullName}}
Avatar of winsane
winsane

ASKER

If I run these scripts, will they also check mapped network drives on the local machine?
Or do they have to be run from the server?
They run locally, but the can be run against any server or local.

<PathToStartSearch> can be a local path or a UNC. These commands also do recursive checks.. if you do NOT want that... remove -rec
Avatar of winsane

ASKER

I got them to work by typing them into the PowerShell command line but how can I stick them into a file and run the script with the servername as an argument? I would like to schedule a task every Friday to do a keyword search on server3 to find tmp files..

For example: I could save it in a batch file which captures the output into a file and runs automatically at 6:00pm. Is there a way to save the script in a file and execute by entering "powershell myscript.ps" or executing the script with the least human interaction possible?

Thanks!
sure.... you can even set the script up as a schedule tasked if you wish.

cut/past into Get-ServerFileInfo.ps1
############################################
Param($server)
Write-Output "Files that Match tmp on $Server"
get-ChildItem \\$server\c$ -rec | Where-Object{$_.name -match "tmp"} | foreach{$_.fullnname}

Write-Output "Files that are 0 bytes on $Server"
get-ChildItem \\$server\c$ -rec |Where-Object{(!$_.PSIsContainer) -and ($_.Length -eq 0)} | foreach{$_.fullname}

Write-Output "Files that over 10mb on $Server"
get-ChildItem \\$server\c$ -rec |Where-Object{(!$_.PSIsContainer) -and ($_.Length -ge 10mb)} | foreach{$_.fullname}

Write-Output "Files that have duplicates on $Server"
$files = @{}
get-ChildItem \\$server\c$ -rec | Where-Object{(!$_.PSIsContainer)} | foreach{if(!($files."$_.Name")){$files."$_.Name" = $_.FullName}else{write-Host $_.FullName}}
########################################
Avatar of winsane

ASKER

Anyone know how I could launch a script from a C# windows app? I actually think that powershell will work out perfect, but I need a way of controlling the scripts from a program. If I could call the script from a menu that would be great.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of BSonPosh
BSonPosh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of winsane

ASKER

I have been trying to use these scripts on another pc and I downloaded powershell. Windows goes through the motions of installing but says that it can't install until all other versions of powershell are taken off. In Add/Remove programs, powershell is not listed.  How can I fix this issue so that I can run my powershell scripts?