PoshCode Archive  Artifact Content

Artifact 9ba56b39f23786efd4e3a818fb15f80af21ce2d0d0cac6a2d77cc7a865a398ec:

  • File Set-IISLogLocation.ps1 — part of check-in [c3d5a29648] at 2018-06-10 13:09:15 on branch trunk — This advanced function will allow you to set the IIS log location on a server or servers. You can specify a single site or perform the task on all sites. Also supports -whatif in the function. (user: Boe Prox size: 3846)

# encoding: ascii
# api: powershell
# title: Set-IISLogLocation
# description: This advanced function will allow you to set the IIS log location on a server or servers.  You can specify a single site or perform the task on all sites. Also supports -whatif in the function.
# version: 0.1
# type: function
# author: Boe Prox
# license: CC0
# function: Set-IISLogLocation
# x-poshcode-id: 2374
# x-archived: 2010-11-23T15:13:33
#
#
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  
    http://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"
                                    }                                   
                                }                             
                            }                                                                                                   
                        }
                    }                        
            }                
        }  
    }
}