Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .SYNOPSIS
- Create Website in IIS from JSON configuration file.
- .DESCRIPTION
- The script load the JSON configuration file.
- Create the application pool and configure it.
- Create the website and configure it.
- .NOTES
- File Name : IIS-Sitemanager.ps1
- Author : shadowmtl2000
- Requires : PowerShell V4 to be confirmed
- Tested on : Windows 2012 R2 IIS 8.5, Windows2008 R2 IIS 7.5
- https://technet.microsoft.com/en-us/library/hh867899.aspx
- Need incremental IIS website ID.
- [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Inetmgr\Parameters] "IncrementalSiteIDCreation"=dword:00000000
- .SYNTAX
- C:\IIS-Sitemanager.ps1 -configpath ["C:\configfile.json"] -cleanup [$false | $true]
- .CONFIGURATIONFILE
- This is the definition of the Json the values name follows the IIS configuration, so this documentation will not explain the purpose is the configuration.
- Directory "\" need to be escape in a Json file
- <Optional> -> Mean that if not define initially it will use IIS default value.
- site -> Definition of 1 website, multiple sites can be define in the Json.
- site.name -> Name of the web site (eg. testsite.test.com)
- site.physical_path -> physical of the website (eg. D:\\Inetpub\\testsite.test.com)
- site.apppoolname -> Name of one the application pool define in site.app_pools.name
- site.binding -> Array of bindings
- site.binding.protocol -> "http" or "https"
- site.binding.protocol.BindingInformation -> "interface address or *:port:hostheader" (eg."*:80:dev-testsite.test.com" or "192.168.1.25:8080:dev-testsite.test.com")
- site.app_pools.name -> Name of the application pool (eg. testsite.test.com)
- site.app_pools.identityType -> Process model Identity. Values can be
- "NetworkService"
- or "LocalService"
- or "LocalSystem"
- or "ApplicationPoolIdentity"
- or "SpecificUser" the values app_pools.username and app_pools.password must be define.
- site.app_pools.username -> Name of the user if "SpecificUser" has been defined for app_pools.identityType
- site.app_pools.password -> Password of the user if "SpecificUser" has been defined for app_pools.identityType
- site.app_pools.managedRuntimeVersion -> Version of the runtime. Values can be "v2.0" or "v4.0" or "No Managed Code"
- site.app_pools.managedPipelineMode -> "Integrated" or "Classic"
- site.app_pools.maxProcesses -> <Optional> Maximum Worker Processes; Value is a Integer
- site.app_pools.smpAffinitized -> <Optional> Processor Affinity Enable; Value is bool "True" or "False"
- site.app_pools.enable32BitAppOnWin64 -> <Optional> Enable 32-Bit Application; Value is bool "True" or "False"
- site.app_pools.recycling -> <Optional> Set configuration for periodic restarts
- site.app_pools.recycling.periodicRestart -> <Optional> Set configuration for periodic restarts conditions, all sub parameter must be define.
- site.app_pools.recycling.periodicrestart.virtualmemory -> Virtual Memory Based Maximum; Values are Integer in KB, "0" = disable
- site.app_pools.recycling.periodicrestart.privatememory -> Private Memory Based Maximum; Values are Integer in KB, "0" = disable
- site.app_pools.periodicrestart.timeInterval -> Fixed Intervals; Regular time interval (in minutes) ; Values are Integer, "0" = disable
- site.app_pools.recycling.periodicrestart.requests -> Fixed Intervals; Fixed number of requests; Values are Integer, "0" = disable
- site.web_apps -> Array of configuration for web application, if the website do not have web application the array can be empty "web_apps": []
- site.web_apps.virtual_path -> Path under the web site where the web application is (eg. dev-testsite.test.com/webapp2/sub1 -> "virtual_path": "\\webapp2\\sub1")
- site.web_apps.application_pool - > Name of one the application pool define in site.app_pools.name
- site.web_apps.physical_path -> Physical of the web application (eg. D:\\Inetpub\\testsite.test.com\\webapp2\\sub1)
- site.logging -> Configure the built-in logging module
- site.logging.directory -> Configure the path of the logging directory (eg.D:\\IISLogFiles )
- site.logging.dontlog -> Enable the logging (be careful of the double negative); Value is bool "True" (Disable logging), "False" (Enable logging)
- site.logging.customfield -> Custom field definition like X_Forward_for is definition is only for IIS 8.5, the array can be empty "customfield": []
- site.logging.customfield.logfieldName -> Name of the field in the log file
- site.logging.customfield.sourcename -> Name of the source
- site.logging.customfield.sourcetype -> Where the source name is located in the request; Value can be "RequestHeader" or "ResponseHeader" or "ServerVariable"
- Example using all configuration:
- [{
- "site": {
- "name": "dev-testsite.test.com",
- "physical_path": "D:\\Inetpub\\testsite.test.com",
- "apppoolname": "dev-testsite.test.com",
- "bindings": [{
- "protocol": "http",
- "BindingInformation": "*:80:dev-testsite.test.com"
- }],
- "app_pools": [{
- "name": "dev-testsite.test.com",
- "identityType": "NetworkService",
- "managedRuntimeVersion": "No Managed Code",
- "managedPipelineMode": "Integrated",
- "username": "",
- "password": "",
- "recycling": {
- "periodicrestart": {
- "virtualmemory": "0",
- "privatememory": "0",
- "timeInterval": "0",
- "requests": "0"
- }
- }
- },
- {
- "name": "webapp1",
- "identityType": "ApplicationPoolIdentity",
- "managedRuntimeVersion": "v4.0",
- "managedPipelineMode": "Integrated",
- "username": "",
- "password": ""
- }],
- "virtualDirectories": [{
- "virtual_path": "\\webapp2",
- "physical_path": "C:\\lsproot\\test"
- }],
- "web_apps": [{
- "virtual_path": "\\webapp2\\sub1",
- "application_pool": "dev-testsite.test.com",
- "physical_path": "C:\\lsproot"
- },
- {
- "virtual_path": "\\webapp2\\webapp3",
- "application_pool": "dev-testsite.test.com",
- "physical_path": "D:\\Inetpub\\testsite.test.com\\webapp2\\webapp3"
- }],
- "logging": {
- "directory": "%SystemDrive%\\inetpub\\logs\\LogFiles",
- "dontlog": "False",
- "customfield": [{
- "logfieldName": "X-FORWARDED-FOR",
- "sourcename": "X-FORWARDED-FOR",
- "sourcetype": "RequestHeader"
- },
- {
- "logfieldName": "X-FORWARDED-FOR2",
- "sourcename": "X-FORWARDED-FOR2",
- "sourcetype": "RequestHeader"
- }]
- }
- }
- }]
- #>
- #param([Parameter(Mandatory=$true)][string]$configpath,[string]$cleanup)
- $configpath = "C:\data\powershell-script-repository\hgosselin\IIS\test.json"
- $cleanup = "true"
- if($cleanup.length -eq 0){$cleanup = "false"}
- #initialize collection
- $collection =$null
- $collection= (Get-Content $configpath) -join "`n" | ConvertFrom-Json
- $Logfile = "C:\IIS-Sitemanager.log"
- #Clear log file
- if(Test-Path $Logfile)
- {
- Remove-Item $Logfile
- }
- #Set $ErrorActionPreference to "Stop" so that any exception terminating or non-terminating stop the script
- $ErrorActionPreference ="stop"
- #Loading module
- Import-Module WebAdministration
- [System.Reflection.Assembly]::LoadFrom("C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll") | Out-Null #To fix problem with cmdlet get-webconfigurationporperty
- Function WriteLog
- {
- Param ([string]$logstring)
- Add-content $Logfile -value $logstring
- }
- function CreateDirectory
- {
- param([string]$folder)
- if(Test-Path $folder)
- {
- WriteLog "$(get-date) - Directory $folder - already exist"
- }
- else
- {
- new-item -ItemType Directory $folder | Out-Null
- WriteLog "$(get-date) - Directory $folder - New folder created"
- }
- }
- function Set-WritePermissionOn ($folder,$ApplicationPool)
- {
- $item = Get-Item "IIS:\\AppPools\\$($ApplicationPool)"
- $accountIdentity = $null
- if($item.processModel.identityType -eq "ApplicationPoolIdentity" -or $item.processModel.identityType -eq 4)
- {
- $accountIdentity = "IIS APPPOOL\$($item.Name)"
- }
- elseif ($item.processModel.identityType -eq "NetworkService" -or $item.processModel.identityType -eq 2)
- {
- $accountIdentity = "NT AUTHORITY\NETWORK SERVICE"
- }
- elseif ($item.processModel.identityType -eq "SpecificUser" -or $item.processModel.identityType -eq 3)
- {
- $accountIdentity = $item.processModel.userName
- }
- elseif($item.processModel.identityType -eq "LocalSystem")
- {
- $accountIdentity = "NT AUTHORITY\SYSTEM"
- }
- #Prepare ACL and rule
- $acl = Get-Acl $folder
- $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($accountIdentity, "MODIFY", "ContainerInherit,ObjectInherit", "None", "ALLOW")
- #Check if Permission is already set
- if(!(($acl.Access | ?{$_.IdentityReference -eq $accountIdentity}).FileSystemRights) -eq $rule.FileSystemRights)
- {
- WriteLog "$(get-date) - Logging - Applying WRITE permmission to $accountIdentity on $($folder)"
- #Add security rule to current ACL
- $acl.AddAccessRule($rule)
- Set-Acl $folder $acl
- }
- else
- {
- WriteLog "$(get-date) - Logging - Permission on $($config.site.logging.directory) is OK"
- }
- }
- $starttime = get-date
- WriteLog "$(get-date) - ----------------------- Starting execution ------------------------------------------"
- ForEach($config in $collection)
- {
- #Reload server manager config
- $servermanager = New-Object Microsoft.Web.Administration.ServerManager
- #--------------------------- Initialize validation variable ------------
- $CurrentPool = $null
- $CurrentSite = $null
- $currentWebapp =$null
- $Currentdontlog = $null
- $customfielddiff = $null
- #--------------------------- WebAppPool --------------------------------
- foreach($apppool in $config.site.app_pools)
- {
- # No apppool present then create with default config.
- if(!(Test-Path IIS:\AppPools\$($apppool.name)))
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Not found"
- New-WebAppPool -Name $apppool.name | Out-Null
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Created"
- }
- else
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Already exist"
- }
- #Already created or not applying proper configuration if needed.
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Verifying configuration"
- #Getting the current configuration of the application pool.
- $CurrentPool = Get-Item IIS:\AppPools\$($apppool.name)
- #Configure identityType
- if($CurrentPool.processModel.identityType -ne $apppool.identityType)
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - identityType mismatch"
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Changing identityType from $($CurrentPool.processModel.identityType) to $($apppool.identityType)"
- $CurrentPool.processModel.identityType = $apppool.identityType
- }
- else
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - identityType OK"
- }
- #Configure username and password
- if($CurrentPool.ProcessModel.Username -ne $apppool.username)
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - username mismatch changing username from $($CurrentPool.ProcessModel.Username) to $($apppool.username)"
- $CurrentPool.ProcessModel.Username = $apppool.username
- $CurrentPool.ProcessModel.Password = $apppool.password
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Changing password"
- }
- else
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - username OK"
- }
- #configure managedRuntimeVersion (.Net version)
- if($CurrentPool.managedRuntimeVersion -ne $apppool.managedRuntimeVersion)
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - managedRuntimeVersion mismatch changing from $($CurrentPool.managedRuntimeVersion) to $($apppool.managedRuntimeVersion)"
- $CurrentPool.managedRuntimeVersion = $apppool.managedRuntimeVersion
- }
- else
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - managedRuntimeVersion is OK"
- }
- #Configure the managedPipelineMode (Classic or Integrated)
- if($CurrentPool.managedPipelineMode -ne $apppool.managedPipelineMode)
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - managedPipelineMode mismatch changing from $($CurrentPool.managedPipelineMode) to $($apppool.managedPipelineMode)"
- $CurrentPool.managedPipelineMode = $apppool.managedPipelineMode
- }
- else
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - managedPipelineMode is OK"
- }
- #Not mandatory field in configuration file, check if its present
- if(Get-Member -InputObject $apppool -Name "maxProcesses")
- {
- #configure Maximum workwer process
- if($CurrentPool.processModel.maxProcesses -ne $apppool.maxProcesses)
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - maxProcesses mismatch changing from $($CurrentPool.processModel.maxProcesses) to $($apppool.maxProcesses)"
- $CurrentPool.processModel.maxProcesses = [int]$apppool.maxProcesses
- }
- else
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - maxProcesses is OK"
- }
- }
- else
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - maxProcesses is OK"
- }
- #Configure the enable32BitAppOnWin64 true or false
- #Not mandatory field in configuration file, check if its present
- if(Get-Member -InputObject $apppool -Name "enable32BitAppOnWin64")
- {
- #configure Maximum workwer process
- if([string]$CurrentPool.enable32BitAppOnWin64 -ne $apppool.enable32BitAppOnWin64)
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - enable32BitAppOnWin64 mismatch changing from $($CurrentPool.enable32BitAppOnWin64) to $($apppool.enable32BitAppOnWin64)"
- $CurrentPool.enable32BitAppOnWin64 = $apppool.enable32BitAppOnWin64
- }
- else
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - enable32BitAppOnWin64 is OK"
- }
- }
- else
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - enable32BitAppOnWin64 is OK"
- }
- #Configure the cpu.smpAffinitized true or false
- #Not mandatory field in configuration file, check if its present
- if(Get-Member -InputObject $apppool -Name "smpAffinitized")
- {
- #configure Maximum workwer process
- if([string]$CurrentPool.cpu.smpAffinitized -ne $apppool.smpAffinitized)
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - smpAffinitized mismatch changing from $($CurrentPool.cpu.smpAffinitized) to $($apppool.smpAffinitized)"
- $CurrentPool.cpu.smpAffinitized = $apppool.smpAffinitized
- }
- else
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - smpAffinitized is OK"
- }
- }
- else
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - smpAffinitized is OK"
- }
- #configure the Recycling properties
- #Not mandatory field in configuration file, check if its present
- if(Get-Member -InputObject $apppool -Name "recycling")
- {
- #Periodic restart on virtual memory (in KB)
- if($CurrentPool.recycling.periodicRestart.memory -ne [int]$apppool.recycling.periodicrestart.virtualmemory)
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Recycling on virtual memory changing from $($CurrentPool.recycling.periodicRestart.memory) to $($apppool.recycling.periodicrestart.virtualmemory)"
- $CurrentPool.recycling.periodicRestart.memory = [int]$apppool.recycling.periodicrestart.virtualmemory
- }
- else{WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Recycling on vitual memory is OK"}
- #Periodic restart on private memory (in KB)
- if($CurrentPool.recycling.periodicRestart.privateMemory -ne [int]$apppool.recycling.periodicrestart.privatememory)
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Recycling on private memory changing from $($CurrentPool.recycling.periodicRestart.privatememory) to $($apppool.recycling.periodicrestart.privatememory)"
- $CurrentPool.recycling.periodicRestart.privatememory = [int]$apppool.recycling.periodicrestart.privatememory
- }
- else{WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Recycling on private memory is OK"}
- #Periodic restart on regular time interval (in minutes)
- #convert Json string to timespan variable
- $timeinterval = [timespan]::FromMinutes($apppool.recycling.periodicrestart.timeInterval)
- if($CurrentPool.recycling.periodicRestart.time -ne $timeinterval)
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Recycling on time interval changing from $($CurrentPool.recycling.periodicRestart.time) to $($apppool.recycling.periodicrestart.timeInterval)"
- $CurrentPool.recycling.periodicRestart.time = $apppool.recycling.periodicrestart.timeInterval
- }
- else{WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Recycling on time interval is OK"}
- #Recycling on the number of requests
- if($CurrentPool.recycling.periodicRestart.requests -ne [int]$apppool.recycling.periodicrestart.requests)
- {
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Recycling on requests changing from $($CurrentPool.recycling.periodicRestart.requests) to $($apppool.recycling.periodicrestart.requests)"
- $CurrentPool.recycling.periodicRestart.requests = [int]$apppool.recycling.periodicrestart.requests
- }
- else{WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Recycling on requests is OK"}
- }
- #Applying configuration
- $CurrentPool | Set-Item
- WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Configuration applied"
- }
- #-------------------------- Physical path creation ----------------------------------------
- #website physical path
- CreateDirectory $config.site.physical_path
- #webapplication physical path
- foreach($app in $config.site.web_apps)
- {
- CreateDirectory $app.physical_path
- }
- #-------------------------- WebSite ----------------------------------------------
- if(!(Test-Path IIS:\Sites\$($config.site.name)))
- {
- WriteLog "$(get-date) - Website $($config.site.name) - Not found"
- #IIS7x accepted an psobject collection for binding
- #IIS8x dont accept a collection so need adding site with first binding then add.
- New-Item IIS:\Sites\$($config.site.name) -ItemType site -PhysicalPath $config.site.physical_path -Bindings @{protocol=$config.site.bindings.Get(0).protocol;bindingInformation=$config.site.bindings.Get(0).bindinginformation} -ApplicationPool $config.site.apppoolname
- WriteLog "$(get-date) - Website $($config.site.name) - Created"
- #set remaining binding
- if ($config.site.bindings.Count -gt 1)
- {
- for($i=1; $i -lt $config.site.bindings.Count;$i++ )
- {
- new-ItemProperty IIS:\Sites\$($config.site.name) -Name bindings -Value @{protocol=$config.site.bindings.Get($i).protocol;bindingInformation=$config.site.bindings.Get($i).bindinginformation}
- WriteLog "$(get-date) - Binding $(($config.site.bindings).Get($i).hostheader) - Created "
- }
- }
- }
- #In case the new site is not starting, saying that it is use by another process in IIS, it mean that another process is using the specified port.
- #http://support.microsoft.com/kb/890015
- #Site exist
- else
- {
- WriteLog "$(get-date) - Website $($config.site.name) - Already exist"
- #Check web site configuration
- }
- #****************************** Start website configuration validation (double validation even if its been already created) *****************************************
- #****************************** Verifying website Application Pool association *********************************
- WriteLog "$(get-date) - Website $($config.site.name) - Verifying configuration"
- $CurrentSite = Get-Item IIS:\Sites\$($config.site.name)
- if($CurrentSite.applicationPool -ne $config.site.apppoolname)
- {
- WriteLog "$(get-date) - Website $($config.site.name) - application pool mismatch changing from $($CurrentSite.applicationPool) to $($config.site.apppoolname)"
- Set-ItemProperty IIS:\Sites\$($config.site.name) -name ApplicationPool $($config.site.apppoolname)
- }
- else
- {
- WriteLog "$(get-date) - Website $($config.site.name) - application pool is OK"
- }
- #***************************** Verifying website Physical Path association *********************************
- if($CurrentSite.physicalPath -ne $config.site.physical_path)
- {
- WriteLog "$(get-date) - Website $($config.site.name) - physical path mismatch changing from $($CurrentSite.physicalPath) to $($config.site.physical_path)"
- Set-ItemProperty IIS:\Sites\$($config.site.name) -name physicalPath $($config.site.physical_path)
- }
- else
- {
- WriteLog "$(get-date) - Website $($config.site.name) - physical path is OK"
- }
- #***************************** Verifying website Binding association *********************************
- $bindingdiff = $null
- $bindingdiff = Compare-Object -ReferenceObject $config.site.bindings -DifferenceObject $CurrentSite.bindings.Collection -Property protocol,BindingInformation
- if($bindingdiff -eq $null)
- {
- WriteLog "$(get-date) - Website $($config.site.name) - Binding is OK"
- }
- else
- {
- #Not super cleaning removing ALL site bindings,stop state, then reapply the binding collection.
- #But I suppose that a faulty binding cause an outage on the website anyways.
- Get-WebBinding -Name $config.site.name | Remove-WebBinding
- #$config.site.bindings | %{New-WebBinding -Name $config.site.nane -Protocol $_.protocol -Port $_.port }
- new-ItemProperty IIS:\Sites\$($config.site.name) -Name bindings -Value $config.site.bindings
- WriteLog "$(get-date) - Website $($config.site.name) - Binding mismatch"
- }
- #------------------------ Web Application -----------------------------------------
- foreach($webapp in $config.site.web_apps)
- {
- $servermanager = New-Object Microsoft.Web.Administration.ServerManager
- $currentWebapp= $servermanager.Sites["$($config.site.name)"].Applications["$($webapp.virtual_path -replace "\\","/")"]
- #If webapplication dont exist
- if($currentWebapp -eq $null)
- {
- New-WebApplication -site $config.site.name -Name $webapp.virtual_path -PhysicalPath $webapp.physical_path -ApplicationPool $webapp.application_pool
- WriteLog "$(get-date) - WebApplication $($config.site.name)$($webapp.virtual_path) - Created"
- }
- else
- {
- WriteLog "$(get-date) - WebApplication $($config.site.name)$($webapp.virtual_path) - Already Exist"
- }
- #****************************** Start WebApplication configuration validation (double validation even if its been already created) *****************************************
- WriteLog "$(get-date) - WebApplication $($config.site.name)$($webapp.virtual_path) - Verifying configuration"
- #check physical path
- if($currentWebapp.VirtualDirectories.physicalpath -ne $webapp.physical_path)
- {
- #if current config is not the same as config then change it.
- WriteLog "$(get-date) - WebApplication $($config.site.name)$($webapp.virtual_path) - physical path mismatch changing from $($currentWebapp.VirtualDirectories.physicalpath) to $($webapp.physical_path)"
- Set-ItemProperty IIS:\Sites\$($config.site.name)$($webapp.virtual_path) -name physicalPath $($webapp.physical_path)
- }
- else
- {
- WriteLog "$(get-date) - WebApplication $($config.site.name)$($webapp.virtual_path) - physical path is OK"
- }
- #check if Application pool name is proper, the application pool config is already handle.
- if($currentWebapp.ApplicationPoolName -ne $webapp.application_pool)
- {
- WriteLog "$(get-date) - WebApplication $($config.site.name)$($webapp.virtual_path) - application pool mismatch changing from $($currentWebapp.ApplicationPoolName) to $($webapp.application_pool)"
- Set-ItemProperty IIS:\Sites\$($config.site.name)$($webapp.virtual_path) -Name applicationPool $($webapp.application_pool)
- }
- else
- {
- WriteLog "$(get-date) - WebApplication $($config.site.name)$($webapp.virtual_path) - application pool path is OK"
- }
- }#end webapp
- #-------------------------- Logging -------------------------------------------------
- #Set Enabled value
- $servermanager = New-Object Microsoft.Web.Administration.ServerManager
- $Currentdontlog = $servermanager.GetApplicationHostConfiguration().GetSection("system.webServer/httpLogging","$($config.site.name)").GetAttribute("dontlog").value.ToString()
- if($Currentdontlog -ne ($config.site.logging.dontlog))
- {
- WriteLog "$(get-date) - Logging $($config.site.name) - Dontlog mismatch changing from $($Currentdontlog) to $($config.site.logging.dontlog)"
- $servermanager.GetApplicationHostConfiguration().GetSection("system.webServer/httpLogging","$($config.site.name)").SetAttributeValue("dontlog","$($config.site.logging.dontlog)")
- $serverManager.CommitChanges()
- }
- else
- {
- WriteLog "$(get-date) - Logging $($config.site.name) - Dontlog is OK"
- }
- #Checking if logging directory is present, if not create
- if(!(test-path $config.site.logging.directory))
- {
- New-Item -ItemType Directory $config.site.logging.directory | Out-Null
- WriteLog "$(get-date) - Logging folder $($config.site.logging.directory) - created" | Out-Null
- }
- else #Logging directory exist
- {
- WriteLog "$(get-date) - Logging folder $($config.site.logging.directory) - is OK"
- }
- #Set write permission on the logging directory
- Set-WritePermissionOn $config.site.logging.directory $config.site.apppoolname
- #Check if website logging module is properly configure
- #Is site logging directory is OK
- if($CurrentSite.logFile.directory -ne $config.site.logging.directory)
- {
- WriteLog "$(get-date) - Logging $($config.site.name) - Logging directory mismatch changing from $($CurrentSite.logFile.directory) to $($config.site.logging.directory)"
- Set-ItemProperty IIS:\Sites\$($config.site.name) -Name Logfile.directory -Value $($config.site.logging.directory)
- }
- else
- {
- WriteLog "$(get-date) - Logging of $($config.site.name) - Logging directory is OK"
- }
- #If IIS 8.5 check if customs fields are configured
- if((get-itemproperty HKLM:\SOFTWARE\Microsoft\InetStp\).Setupstring -eq "IIS 8.5")
- {
- $filter = "system.applicationHost/sites/site"
- $customfielddiff = Compare-Object -ReferenceObject $config.site.logging.customfield -DifferenceObject $CurrentSite.logFile.customFields.Collection -Property logFieldName,sourceName,sourceType
- #No diffence its OK
- if($customfielddiff -eq $null)
- {
- WriteLog "$(get-date) - Logging $($config.site.name) - Customfields is OK"
- }
- else
- {
- #if current site customfield is empty, then add the config
- if(($CurrentSite.logFile.customFields.Collection).count -eq 0)
- {
- #Adding collection
- WriteLog "$(get-date) - Logging $($config.site.name) - Customfields adding configuration"
- New-ItemProperty "IIS:\Sites\$($config.site.name)" -name logfile.customfields.collection -Value $config.site.logging.customfield -Force
- }
- else
- {
- #Difference detected removing custom field collection
- WriteLog "$(get-date) - Logging $($config.site.name) - Customfields mismatch deleting configuration"
- Remove-WebConfigurationProperty -Filter "$filter[@name='$($config.site.name)']/logfile/customfields" -Name "."
- #Adding collection
- WriteLog "$(get-date) - Logging $($config.site.name) - Customfields adding configuration"
- New-ItemProperty "IIS:\Sites\$($config.site.name)" -name logfile.customfields.collection -Value $config.site.logging.customfield
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement