[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

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...
0
winsane
Asked:
winsane
  • 4
  • 4
1 Solution
 
BSonPoshCommented:
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}}
0
 
winsaneAuthor Commented:
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?
0
 
BSonPoshCommented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
winsaneAuthor Commented:
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!
0
 
BSonPoshCommented:
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}}
########################################
0
 
winsaneAuthor Commented:
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!
0
 
BSonPoshCommented:
LOL... your IN LUCK!

Powershell is C# Friendly.. so much so that there is an entire namespace. FYI, This is exactly how the Exchange tools were written.

Bad news is.. I cant help much here.. I can just point you in the write direction
Start here
http://blogs.msdn.com/powershell/archive/2006/04/25/583250.aspx


Windows PowerShell Programmer's Guide
http://msdn2.microsoft.com/en-us/library/ms714674.aspx

You can also find a ton of links on my Search
http://bsonposh.com/modules/wordpress/?page_id=13
0
 
winsaneAuthor Commented:
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?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now