none
IIS - how to change default log file location for ALL websites on a server

    Question

  • We all know from the abundance of articles on the net how to change the log file location of an existing IIS website, you can simply use this

    Set-ItemProperty 'IIS:\Sites\Default Web Site' -name logFile.directory -value 'D:\IISLogs'

    My question is, how do I change this setting in IIS (and not the individual website), so whenever  a new website is created, it already has my custom log file location?

    thanks!

    Thursday, May 10, 2012 12:21 PM

Answers

  • the last lines a see are these
     
    $xml = [xml](Get-Content
    C:\Windows\System32\inetsrv\config\applicationHost.config)
    #set the new directory, default is %SystemDrive%\inetpub\logs\LogFiles
    $xml.configuration."system.applicationHost".log.centralW3CLogFile.directory
    = "E:\Logs"
     
    so you've modified the log directory value, but then what? its still just an
    xml object that hasn’t been saved back out..
     
    $xml.save('C:\Windows\System32\inetsrv\config\applicationHost.config')
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Robert Little Thursday, May 10, 2012 4:30 PM
    Thursday, May 10, 2012 3:29 PM
  • Ah, I see what you're saying, fixed it. I also noticed that I was setting the configuration for individual web sites, not the entire server, all websites on the server.

    In the code below, I made jrich's change as well as my enhancements. This affects all SITE formats (IIS, NCSA, W3C) as well as server formats (binary and W3C). Using my code as an example, you could open the XML file and change just about everything in there such as the logging schedule, max log file size, etc.

    I'll remove the incorrect code from above, and post the corrected code in this post

    #################
    ## install IIS ##
    #################
    Import-Module servermanager
    Add-WindowsFeature web-server
    ###########################
    ## set log file location ##
    ###########################
    #create new directory if needed
    $e_drive_exist = Test-Path -Path "E:"
    if ($e_drive_exist -eq $false)
    {
    	[console]::ForegroundColor = "red"
    	read-host "E drive does not exist. Please manually set log directory. Press ENTER to exit script."
    	[console]::ResetColor()
    	exit
    }
    $folder_exist = Test-Path -Path E:\Logs
    if ($folder_exist -eq $false)
    {
    	#create it
    	New-Item -Path E:\Logs -ItemType directory
    }
    #load the existing XLM configuration file
    $xml = [xml](Get-Content C:\Windows\System32\inetsrv\config\applicationHost.config)
    #set server wide defaults
    $xml.configuration."system.applicationHost".sites.siteDefaults.logfile.directory = "E:\Logs"
    $xml.configuration."system.applicationHost".sites.siteDefaults.tracefailedrequestslogging.directory = "E:\Logs"
    #set individual site defaults
    $xml.configuration."system.applicationHost".log.centralW3CLogFile.directory = "E:\Logs"
    $xml.configuration."system.applicationHost".log.centralBinaryLogFile.directory = "E:\Logs"
    #save changes
    $xml.save('C:\Windows\System32\inetsrv\config\applicationHost.config')

    Pastebin: http://pastebin.com/44UgTKfE

    edit: I added this to the top since it won't work for Windows Server 2003.

    #Detect OS
    $os = Get-WMIObject Win32_OperatingSystem
    $temp = $os.Caption
    if ($temp.Contains("2003") -eq $true)
    {
    	[console]::ForegroundColor = "red"
    	read-host "Cannot install IIS to Windows Server 2003 using this script. Please install and configure manually. Press ENTER to exit script."
    	[console]::ResetColor()
    	exit
    }

    Thursday, May 10, 2012 4:28 PM

All replies

  • Your question would be better asked in the IIS forum:

    http://social.technet.microsoft.com/Forums/en-US/iises/threads


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Thursday, May 10, 2012 12:33 PM
  • I was literally in the process of searhcing technet forums for the IIS forum to cross post. Thanks Teddy :) You guys here know so much, figured I'd start where I am most comfortable
    Thursday, May 10, 2012 12:48 PM
  • Actually, that won't work. For one it's all in Spanish. All my spanish is centered around asking for booze, bathrooms, and complimenting women, lol. Also, it won't let me post there. One more odd thing, if you start at the 'forums home' area and search for IIS or Internet Information Server, no results are there. I find that really odd.
    Thursday, May 10, 2012 12:55 PM
  • got it, http://iis.net is the official forums. Odd it's a different 'external' site.
    Thursday, May 10, 2012 12:58 PM
  • there are a few external MS websites, mostly because they started up before
    the forums became popular (people dislike newsgroups for some reason?!)
     
    as far as an answer, if its possible, and im not sure it is, its tucked some
    where in the metabase..
     
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, May 10, 2012 1:05 PM
  • got it guys.

    The config files are in “%WinDir%\System32\Inetsrv\Config”

    the XML is in <centralW3CLogFile> element of the <log> element of the applicationHost.config XML file.

    New problem though.

    when I run this

    $xml = [xml](Get-Content C:\Windows\System32\inetsrv\config\applicationHost.config)
    $xml

    It doesn't return the contents of the XML file, all it returns is:

    xml                                                         #comment                                                    configuration                                              
    ---                                                         --------                                                    -------------                                              
    version="1.0" encoding="UTF-8"                              ...                                                         configuration                                              

    I was hoping I could do something like:

    $xml.log.centralw3clogfile = "e:\logs"

    I suppose I could always open up the file as text, do a search/replace. I'd rather work with the XML natively though.

    Thursday, May 10, 2012 1:49 PM
  • sure it does :)
     
    its xml tho, so it doesn’t just spit it out, you can see it shows the node,
    configuration so then do..
     
    $xml.configuration
     
    and keep on going...
     
    it might be easier to just take it in as a string and do a replace.. im not
    expert in xml tho, so maybe its easy enough with xml..
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, May 10, 2012 2:23 PM
  • Got it. I just wasn't drilling down using he right element names, doh! Thanks! Final code below

    edit: removed code because it wasn't correct

    Thursday, May 10, 2012 2:53 PM
  • if you are going to bother doing that why not just push them all to a DB? :)
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, May 10, 2012 2:55 PM
  • Hey Robert,

    Could you re post your code. It got cut off right at the good part.

    Us dummies would like to see the rest for our future endevours.

    Olddog

    Thursday, May 10, 2012 3:00 PM
  • I think he just forgot to save the file :)

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 10, 2012 3:02 PM
  • edit: removed code because it wasn't correct

    Thursday, May 10, 2012 3:22 PM
  • the last lines a see are these
     
    $xml = [xml](Get-Content
    C:\Windows\System32\inetsrv\config\applicationHost.config)
    #set the new directory, default is %SystemDrive%\inetpub\logs\LogFiles
    $xml.configuration."system.applicationHost".log.centralW3CLogFile.directory
    = "E:\Logs"
     
    so you've modified the log directory value, but then what? its still just an
    xml object that hasn’t been saved back out..
     
    $xml.save('C:\Windows\System32\inetsrv\config\applicationHost.config')
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Robert Little Thursday, May 10, 2012 4:30 PM
    Thursday, May 10, 2012 3:29 PM
  • Ah, I see what you're saying, fixed it. I also noticed that I was setting the configuration for individual web sites, not the entire server, all websites on the server.

    In the code below, I made jrich's change as well as my enhancements. This affects all SITE formats (IIS, NCSA, W3C) as well as server formats (binary and W3C). Using my code as an example, you could open the XML file and change just about everything in there such as the logging schedule, max log file size, etc.

    I'll remove the incorrect code from above, and post the corrected code in this post

    #################
    ## install IIS ##
    #################
    Import-Module servermanager
    Add-WindowsFeature web-server
    ###########################
    ## set log file location ##
    ###########################
    #create new directory if needed
    $e_drive_exist = Test-Path -Path "E:"
    if ($e_drive_exist -eq $false)
    {
    	[console]::ForegroundColor = "red"
    	read-host "E drive does not exist. Please manually set log directory. Press ENTER to exit script."
    	[console]::ResetColor()
    	exit
    }
    $folder_exist = Test-Path -Path E:\Logs
    if ($folder_exist -eq $false)
    {
    	#create it
    	New-Item -Path E:\Logs -ItemType directory
    }
    #load the existing XLM configuration file
    $xml = [xml](Get-Content C:\Windows\System32\inetsrv\config\applicationHost.config)
    #set server wide defaults
    $xml.configuration."system.applicationHost".sites.siteDefaults.logfile.directory = "E:\Logs"
    $xml.configuration."system.applicationHost".sites.siteDefaults.tracefailedrequestslogging.directory = "E:\Logs"
    #set individual site defaults
    $xml.configuration."system.applicationHost".log.centralW3CLogFile.directory = "E:\Logs"
    $xml.configuration."system.applicationHost".log.centralBinaryLogFile.directory = "E:\Logs"
    #save changes
    $xml.save('C:\Windows\System32\inetsrv\config\applicationHost.config')

    Pastebin: http://pastebin.com/44UgTKfE

    edit: I added this to the top since it won't work for Windows Server 2003.

    #Detect OS
    $os = Get-WMIObject Win32_OperatingSystem
    $temp = $os.Caption
    if ($temp.Contains("2003") -eq $true)
    {
    	[console]::ForegroundColor = "red"
    	read-host "Cannot install IIS to Windows Server 2003 using this script. Please install and configure manually. Press ENTER to exit script."
    	[console]::ResetColor()
    	exit
    }

    Thursday, May 10, 2012 4:28 PM
  • if you are going to bother doing that why not just push them all to a DB? :)


    Justin Rich

    What do you mean push them to a db?

    Thursday, May 10, 2012 4:31 PM
  • IIS has the ability to log to a DB rather than a text file. if you want to
    assure that your logs are all in the same location (usually for security
    reasons) why not make them centrally located (easier to clean and maintain
    also) by putting them in a SQL DB
     
    that way you can generate reports off of it pretty easily to show site usage
    or failures or whatever it is you want
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, May 10, 2012 6:32 PM
  • so this is the site default, which means any NEW site will use these
    settings, any current site will continue to use whatever it is its using,
    you'd probably want to change the default site as you have and also loop
    through any site that is on the server and modify it as well.
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, May 10, 2012 6:34 PM
  • Nice, I dind't know that you could use a DB for logs. In my situation, the web team handles this (I'm just server support) so it's not really my decision. I'm just implementing this task for new server builds. My first powershell script was the big one I use to roll out a VM template and make a bunch of changes. This is just something we're adding to that now, a default IIS install for Win2k8 R2
    Friday, May 18, 2012 1:40 PM