Webmonger
Webmonger

Reputation: 593

Powershell command to set IIS logging settings

I'm creating a powershell script so I can create website hosting with a single command using the IIS Powershell Management Console.

I have the commands I need to create the IIS Site and add bindings for the domain names etc...

The one piece of the puzzle I'm missing though is how to change the default Logging directory from %SystemDrive%\inetpub\logs\LogFiles to my own folder that's not on the boot drive of the server.

After extensive searching I expected to find a command along the lines of the following pseudo powershell

New-ItemProperty IIS:\Sites\MyNewSite -name logging -value @{format='W3C';directory='d:\sites\site\logs';encoding=UTF-8}

Please could you show me with an example how you change the logging folder in the IIS Powershell Management Console

Thanks in advance

Upvotes: 9

Views: 38701

Answers (6)

Prof Von Lemongargle
Prof Von Lemongargle

Reputation: 3768

If you host multiple sites on a single server and want them to all log to the same log file, the process is quite different. It took some digging to find clues here and here, so I thought I would leave a description behind for anyone else with this need.

The following two statements will combine logs for all of your websites into a folder e:\log\w3svc.

Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter 'system.applicationHost/log' -name CentralLogFileMode -Value 'CentralW3C'
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter 'system.applicationHost/log' -name centralW3CLogFile.directory -value 'e:\log'

Upvotes: 1

JoeGasper
JoeGasper

Reputation: 141

While testing the answer from this thread, toggling options via IIS Manager and PowerShell, I stumbled on something that has been hidden to me. In IIS Manager, choosing Configuration Editor and making a change, allows the IIS Manager to generate and display the script for the change in C#, JavaScript, AppCmd.exe and PowerShell. Just click the Generate Script option.

[Auto Generated Scripts for IIS configuration changes]

Upvotes: 13

Paul Hopkinson
Paul Hopkinson

Reputation: 441

For changing an individual web site's logFile configuration, the original post was nearly correct. Instead of New-ItemProperty, use Set-ItemProperty, like so...

Set-ItemProperty "IIS:\Sites\$SiteName" -name logFile -value @{directory=$LogPath}

For changing the server-wide default settings, see Andy Schneider's answer.

For more information about the options available, see this IIS.net article.

Upvotes: 8

Ted Elliott
Ted Elliott

Reputation: 3503

This works as well, using the WebAdministration Module

Import-Module WebAdministration
$site = gi IIS:\Sites\MyNewSite
$site.Logging.format='W3C'
$site.Logging.directory='d:\sites\site\logs'
$site.Logging.encoding=UTF-8
$site | set-item

Upvotes: 5

Webmonger
Webmonger

Reputation: 593

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
$iis = new-object Microsoft.Web.Administration.ServerManager
$web = $iis.Sites["test"]
#set new logpath, must be existing
$web.LogFile.Directory = "F:\Logfiles\"
$iis.CommitChanges()

Upvotes: 2

Andy Schneider
Andy Schneider

Reputation: 8684

Import-Module WebAdministration
Set-WebConfigurationProperty "/system.applicationHost/sites/siteDefaults" -name logfile.directory -value $logdir

Upvotes: 20

Related Questions