IIS 7 Log Files Auto Delete? - asp.net

Is there any feature in IIS 7 that automatically deletes the logs files older than a specified amt of days?
I am aware that this can be accomplished by writing a script(and run it weekly) or a windows service, but i was wondering if there is any inbuilt feature or something that does that.
Also, Currently we turned logging off as it is stacking up a large amount of space. Will that be a problem?

You can create a task that runs daily using Administrative Tools > Task Scheduler.
Set your task to run the following command:
forfiles /p "C:\inetpub\logs\LogFiles" /s /m *.* /c "cmd /c Del #path" /d -7
This command is for IIS7, and it deletes all the log files that are one week or older.
You can adjust the number of days by changing the /d arg value.

One line batch script:
forfiles /p C:\inetpub\logs /s /m *.log /d -14 /c "cmd /c del /q #file"
Modify the /d switch to change number of days a log file hangs around before deletion. The /s switch recurses subdirectories too.
Ref: http://debug.ga/iis-log-purging/

Similar solution but in powershell.
I've set a task to run powershell with the following line as an Argument..
dir D:\IISLogs |where { ((get-date)-$_.LastWriteTime).days -gt 15 }| remove-item -force
It removes all files in the D:\IISLOgs folder older than 15 days.

Another viable Powershell one-liner:
Get-ChildItem -Path c:\inetpub\logs\logfiles\w3svc*\*.log | where {$_.LastWriteTime -lt (get-date).AddDays(-180)} | Remove-Item -force
In case $_.LastWriteTime doesn't work, you can use $PSItem.LastWriteTime instead.
For more info and other suggestions to leverage the IIS LogFiles folder HDD space usage, I also suggest to read this blog post that I wrote on the topic.

Related

Delete dos folders 4 chars long starting with letter S

I am trying to find a command, which could be described like
rmdir s???
i.e. delete all folders in current dir starting with S, exactly 4 characters long. I've tried rmdir, del, erase, none of them works.
Any ideas please? Will it be different for empty and non-empty folders?
In MS-DOS 6.0 (per your question):
deltree/y s???
And in case you are not in MS-DOS but actually on the Windows command line:
for /f %i in ('dir /a:d /b s????') do rd /s /q %i
I've found something similar to the first answer (in case of empty folders just remove the /s switch)
for /d %n in (s???) do rd /s "%n"

Robocopy everything in subdirectory excluding the root files

How do I use robocopy so the root contents are not copied?
I already have root files stored elsewhere and I just would like to copy the sub directories and their contents while the source folder still containing root directory contents.
This is not possible with native robocopy switches as far as I can tell. You will need to use a script to enumerate the subdirectories and run robocopy against them.
Here is a sample PowerShell command that will accomplish what you want, copying everything from C:\temp\source\ to c:\temp\target\, excluding the files that are in c:\temp\source:
get-childitem c:\temp\source\* |?{$_.PsIsContainer} | %{robocopy $_.FullName c:\temp\target\$($_.Name) /S}
Credit to powershell ignore files in root but robocopy folders and their contents for the basics of this.
I don't have a reputation but the answer Mr. Hinkle gave solved 2 days of effort and searching. My challenge was moving the files that were > 1hour of age. This combination of powershell and robocopy appears to work. Below is my final code.
# Clear screen
cls
# Disconnect the drive if it exist - don't know where it is pointing to
If (Test-path p:) {
net use p: /delete
}
#Map the destination
net use p: \\server\dir1\dir2\dir3 password /USER:domain\user /P:no
get-childitem -path 'D:\dir1\dir2\' |
where-object {$_.LastWriteTime -lt (get-date).Addhours(-1)} |
?{$_.PsIsContainer} |
%{robocopy $_.FullName p:\$($_.Name) /S /MOVE /r:3 /W:1}
net use p: /delete

issuing a bash command in multiple directories from a unix shell

I need to execute a short script that just renames a few files (mv).
The script is in about 50 folders, each folder is named and currently I'm executing them from the shell and moving to the next folder using:
bash rename && cd ../folder01
Then pressing up and changing the last digit(s).
Is there a way to execute the script in all folders in one line?
Sure, use a for loop:
for f in folder*; do (cd "$f" && bash rename) ; done

Is it possible to create a symlink to the latest file in a directory?

I have a home directory in my unix box. I would like to setup a number or shortcuts in it to point to the latest file in another directory and the link will update if a newer file is created.
Is this possible?
So far I able to get the latest file:
ls -lrt | tail -n1
Thanks
[EDIT]
Perhaps I could even create a shell instead of a softlink which finds the latest file and returns it so I can open/grep/delete etc?
In bash, this will make a link to the latest file or directory in "target-directory" called "latest":
ln -s target-directory/`ls -rt target-directory | tail -n1` latest
And this will wait for a change in "target-directory" before returning:
inotifywait -e attrib target-directory

Powershell script to create folder, website and deploy using MSDeploy.exe

I would like to deploy a web application on Windows 2008 R2. I know the separate PowerShell commands to do various tasks. But I would like to put this into a nice PowerShell script.
I just need the syntax, can you please help me to do the following actions:
Test if C:\Inetpub\MyWebsite folder exists, if not, create it.
Test in IIS7 if MyWebsite exists, if not create it (I know how to Import-Module WebAdministration and call New-WebSite)
Now the complicated part. I deploy a Web site from a package prepared by Visual Studio 2010. VS supplies a .cmd file where I just need to execute it from a DOS prompt. This means I have to leave the PS Console, open a DOS Console to run that cmd file. Is it possible to run a .cmd file from within a PowerShell console ?
To answer your questions:
Import-Module WebAdministration
# Check for physical path
$sitePath = "c:\inetpub\MyWebsite"
if (-not (Test-Path -path $sitePath))
{
New-Item -Path $sitePath -type directory
}
# Check for site
$siteName = "MyWebSite"
$site = Get-WebSite | where { $_.Name -eq $siteName }
if($site -eq $null)
{
Write-Host "Creating site: $siteName"
# Put your New-WebSite code here
}
# Execute your .cmd here
c:\PathToScript\MakeMySite.cmd
You can run .cmd scripts from within PowerShell just fine.
I have also changed a little bit. Using the same Test syntax to test if a website exists or not:
if (-not (Test-Path -path IIS:\Sites\$SiteName))
{
New-WebSite -Name $SiteName ...etc...
}
Also for executing the *.cmd file, I lift some code from the web and saw that people use & to execute external command. Hope that you are OK:
& c:\PathToScript\MakeMySite.cmd arg1 arg2
Thank you very much for your help.
If you need to run the .cmd file as administrator, you can use the following code:
Start-Process -FilePath C:\PathToScript\MakeMySite.cmd -Verb RunAs -ArgumentList "/y"

Resources