Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

IIS log management Powershell script?

Posted on 2010-08-25
14
Medium Priority
?
1,494 Views
Last Modified: 2012-05-10
Would somebody help me out with a Powershell script to manage IIS log files?  What I want to do.

Start at \\webserver1\logfile -  loop through each folder.
File path c:\logfiles\domain.org\w3svc[X]

If the files date isn't equal to today date
then move the file \\webserver1\ domain.org\w3svc[X] to \\archiveserver\domain.org\w3svc[X] and
then rename the file u_ex100713.log to w1u_ex100713.log
else do nothing

if the files been moved and renamed then write or append the filename to \\webserver1\tracking.txt
file.  


0
Comment
Question by:Westez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33529050
do u mean to check all log files dates under \\webserver1\logfile\domain.org\w3svc[X].
and if the file date isn't equal to today date then move to the archive server?

>>then rename the file u_ex100713.log to w1u_ex100713.log
is this an example of a log file?
so the renaming should be just the prefix of file name?
from 'u_' to 'w1u_'?
0
 

Author Comment

by:Westez
ID: 33530757
Yes, sorry for not being clear.
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33530775
do u mind having it as vb script?
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33530803
file date you mean creation time or last modified time?
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33531083
here you go in powershell
foreach ($i in gci \\webserver1\logfile\domain.org\w3svc[X]){
	if(!$i.psiscontainer){
		if ((Get-Date $i.CreationTime).ToShortDateString() -eq (Get-Date).ToShortDateString()){
			move-Item $i.FullName \\archiveserver\domain.org\w3svc[X]
		}
	}
}

gci \\archiveserver\domain.org\w3svc[X] | rename-item -newname { $_.name -replace 'u_','w1u_' }

Open in new window

0
 

Author Comment

by:Westez
ID: 33533510
Thanks for your help.

The w3svc[X] - that X is a number.  So my file structure looks like this.  
domain.org\w3svc7
domain.org\w3svc22
domain.org\w3svc23

We currently host 41domains.  Is it possible to treat the w3svc[X] as a variable so one script would loop through each domains log folder and move the files?  

0
 

Author Comment

by:Westez
ID: 33534243
Do we have to tell it where to move the file too?

At the moment it doesn't work.
I'm trying to make it work with a single domain.  And I can move the files with this cmd in a DOS shell on the web server where the log files are stored.  Of course the cmd moves everything, so the date thing comes back to bite me, LOL.

move c:\logfiles\domain.org\w3svc17\*.* \\archiveserver\weblogs\domain.org\w3svc17

weblogs is a share sitting on the archive server.
0
 

Author Comment

by:Westez
ID: 33534376
My bad, you are telling it where to move the file to.  It doesn't throw any error msg's it just don't move the files.  
One thing I never figured out in DOS was the date thing. If the files creation date is not equal to todays date then move the file, or delete the file, etc.  Could you help me out with an example using DOS?
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33535323
it doesn't move the files cause the script looking for folder:
w3svc[X]
which as u said doesn't exists cause the [X] reflect different numbers.
so basically it should look for all subfolders which start with 'w3svc'.
is that correct?
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33535332
i'm not big DOS fan but i can post you a vb script in no time, whatever you feel comfortable with.
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 33535636
check now
foreach ($it in gci \\meirpc\temp){
	if($it.psiscontainer -and $it.Name.ToLower().StartsWith("w3svc")){
		foreach ($i in gci $it.FullName){
			if(!$i.psiscontainer){
				if ((Get-Date $i.CreationTime).ToShortDateString() -eq (Get-Date).ToShortDateString()){
					Move-Item $i.FullName \\meirpc\temp\output
				}
			}
		}
	}
}

gci \\meirpc\temp\output | rename-item -newname { $_.name -replace 'u_','w1u_' }

Open in new window

0
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 2000 total points
ID: 33536005
replace $source_folder and $dest_folder to your source and destination folders.

i assume that in your case:
$source_folder = \\webserver1\logfile\domain.org
$dest_folder = \\archiveserver\weblogs\domain.org
cls
$source_folder = "\\meirpc\temp\w3svc"
$dest_folder = "\\meirpc\temp\output\w3svc"

foreach ($it in gci $source_folder){
	if($it.psiscontainer -and $it.Name.ToLower().StartsWith("w3svc")){
		foreach ($i in gci $it.FullName){
			if(!$i.psiscontainer){
				if ((Get-Date $i.CreationTime).ToShortDateString() -eq (Get-Date).ToShortDateString()){
					$dest = Join-Path $dest_folder $it.Name
					if (!(Test-Path -path $dest)){
						New-Item $dest -type directory
					}
					move-Item $i.FullName $dest 
					$destfile = Join-Path $dest $i.Name
					$filename = $destfile -replace 'u_','w1u_'
					Rename-Item -path $destfile -NewName $filename
				}
			}
		}
	}
}

Open in new window

0
 

Author Comment

by:Westez
ID: 33537161
Let me get back to you tomorrow on this, and many thanks for your help.  
0
 

Author Closing Comment

by:Westez
ID: 33560217
Sedqwick - I'm having problems getting the script to run. Most likely something I'm doing. But I've another problem that demands attention, we're a small shop and we all wear many hats.  Maybe we can pick back up on it in a day or two. Thanks for taking the time to help us out here though, it's certainly enough to get us started.
0

Featured Post

Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Six Sigma Control Plans
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

609 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