In my previous post, I showed you how to query for the location of IIS log files using ADSI and connecting to remote systems. In this post, I will show you some code that will allow you to set the log location for each website in IIS or all of the depending on your preference.
This script came about from a forum post in the Windows PowerShell forum in which a user wanted to change the IIS log locations. Basically you can specify 1 or more computers to run the script advanced function against. Also is the capability to specify a website as opposed to the default of all websites to change the location. The last parameter you need to provide is the new location of where the logs will go. Using this along with my Get-IISLogLocation will allow you to easily locate and change the log locations.
PS C:\Users\boe> Get-IISLogLocation -computer dc1
Server WebSite LogLocation
—— ——- ———–
dc1 {Default Web Site} {C:\temp}
PS C:\Users\boe> Set-IISLogLocation -computer dc1 -website “Default Web Site” -logdir “D:\logs”
Default Web Site: Log location set to D:\logs
PS C:\Users\boe> Get-IISLogLocation -computer dc1
Server WebSite LogLocation
—— ——- ———–
dc1 {Default Web Site} {D:\logs}
This script also supports the use of –whatif and –confirm
You can also find this script out on the Script Repository and out at PoshCode.
Function Set-IISLogLocation { <# .SYNOPSIS This command will allow you to set the IIS log location on a server or multiple servers. .DESCRIPTION This command will allow you to set the IIS log location on a server or multiple servers. .PARAMETER computer Name of computer to set log location on .PARAMETER logdir Location to set IIS logs to write to .PARAMETER website Name of website to change the log location. .NOTES Name: Set-IISLogLocation Author: Boe Prox DateCreated: 20Aug2010 .LINK https://boeprox.wordpress.com .EXAMPLE Set-IISLogLocation -computer <server> -logdir "D:\logs" Description ----------- This command will change the IIS log locations for each website on the server. .EXAMPLE Set-IISLogLocation -computer <server> -logdir "D:\logs" -website "Default Web Site" Description ----------- This command will change the IIS log locations for only the Default Web Site on a server. #> [cmdletbinding( SupportsShouldProcess = $True, DefaultParameterSetName = 'default', ConfirmImpact = 'low' )] param( [Parameter( Mandatory = $False, ParameterSetName = '', ValueFromPipeline = $True)] [string]$computer, [Parameter( Mandatory = $False, ParameterSetName = '', ValueFromPipeline = $False)] [string]$logdir, [Parameter( Mandatory = $False, ParameterSetName = 'site', ValueFromPipeline = $False)] [string]$website ) Process { ForEach ($c in $Computer) { If (Test-Connection -comp $c -count 1) { $sites = [adsi]"IIS://$c/W3SVC" $children = $sites.children ForEach ($child in $children) { Switch ($pscmdlet.ParameterSetName) { "default" { If ($child.KeyType -eq "IIsWebServer") { If ($pscmdlet.ShouldProcess($($child.servercomment))) { $child.Put("LogFileDirectory",$logdir) $child.SetInfo() Write-Host -fore Green "$($child.servercomment): Log location set to $logdir" } } } "site" { If ($child.KeyType -eq "IIsWebServer" -AND $child.servercomment -eq $website) { If ($pscmdlet.ShouldProcess($($child.servercomment))) { $child.Put("LogFileDirectory",$logdir) $child.SetInfo() Write-Host -fore Green "$($child.servercomment): Log location set to $logdir" } } } } } } } } }