Westez
asked on
IIS log management Powershell script?
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\w3s vc[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.
Start at \\webserver1\logfile - loop through each folder.
File path c:\logfiles\domain.org\w3s
If the files date isn't equal to today date
then move the file \\webserver1\ domain.org\w3svc[X] to \\archiveserver\domain.org
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.
ASKER
Yes, sorry for not being clear.
do u mind having it as vb script?
file date you mean creation time or last modified time?
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_' }
ASKER
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?
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?
ASKER
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\w3s vc17\*.* \\archiveserver\weblogs\do main.org\w 3svc17
weblogs is a share sitting on the archive server.
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\w3s
weblogs is a share sitting on the archive server.
ASKER
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?
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?
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?
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?
i'm not big DOS fan but i can post you a vb script in no time, whatever you feel comfortable with.
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_' }
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Let me get back to you tomorrow on this, and many thanks for your help.
ASKER
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.
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_'?