Advertisement
Guest User

Untitled

a guest
Aug 31st, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.86 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3. Create Website in IIS from JSON configuration file.
  4.  
  5. .DESCRIPTION
  6. The script load the JSON configuration file.
  7. Create the application pool and configure it.
  8. Create the website and configure it.
  9.  
  10.  
  11. .NOTES
  12. File Name : IIS-Sitemanager.ps1
  13. Author : shadowmtl2000
  14. Requires : PowerShell V4 to be confirmed
  15. Tested on : Windows 2012 R2 IIS 8.5, Windows2008 R2 IIS 7.5
  16.  
  17. https://technet.microsoft.com/en-us/library/hh867899.aspx
  18. Need incremental IIS website ID.
  19. [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Inetmgr\Parameters] "IncrementalSiteIDCreation"=dword:00000000
  20.  
  21.  
  22. .SYNTAX
  23. C:\IIS-Sitemanager.ps1 -configpath ["C:\configfile.json"] -cleanup [$false | $true]
  24.  
  25. .CONFIGURATIONFILE
  26.  
  27.  
  28. 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.
  29.  
  30. Directory "\" need to be escape in a Json file
  31. <Optional> -> Mean that if not define initially it will use IIS default value.
  32.  
  33. site -> Definition of 1 website, multiple sites can be define in the Json.
  34.  
  35. site.name -> Name of the web site (eg. testsite.test.com)
  36. site.physical_path -> physical of the website (eg. D:\\Inetpub\\testsite.test.com)
  37. site.apppoolname -> Name of one the application pool define in site.app_pools.name
  38. site.binding -> Array of bindings
  39. site.binding.protocol -> "http" or "https"
  40. 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")
  41.  
  42. site.app_pools.name -> Name of the application pool (eg. testsite.test.com)
  43. site.app_pools.identityType -> Process model Identity. Values can be
  44. "NetworkService"
  45. or "LocalService"
  46. or "LocalSystem"
  47. or "ApplicationPoolIdentity"
  48. or "SpecificUser" the values app_pools.username and app_pools.password must be define.
  49.  
  50. site.app_pools.username -> Name of the user if "SpecificUser" has been defined for app_pools.identityType
  51. site.app_pools.password -> Password of the user if "SpecificUser" has been defined for app_pools.identityType
  52.  
  53. site.app_pools.managedRuntimeVersion -> Version of the runtime. Values can be "v2.0" or "v4.0" or "No Managed Code"
  54. site.app_pools.managedPipelineMode -> "Integrated" or "Classic"
  55.  
  56. site.app_pools.maxProcesses -> <Optional> Maximum Worker Processes; Value is a Integer
  57. site.app_pools.smpAffinitized -> <Optional> Processor Affinity Enable; Value is bool "True" or "False"
  58. site.app_pools.enable32BitAppOnWin64 -> <Optional> Enable 32-Bit Application; Value is bool "True" or "False"
  59.  
  60. site.app_pools.recycling -> <Optional> Set configuration for periodic restarts
  61. site.app_pools.recycling.periodicRestart -> <Optional> Set configuration for periodic restarts conditions, all sub parameter must be define.
  62. site.app_pools.recycling.periodicrestart.virtualmemory -> Virtual Memory Based Maximum; Values are Integer in KB, "0" = disable
  63. site.app_pools.recycling.periodicrestart.privatememory -> Private Memory Based Maximum; Values are Integer in KB, "0" = disable
  64. site.app_pools.periodicrestart.timeInterval -> Fixed Intervals; Regular time interval (in minutes) ; Values are Integer, "0" = disable
  65. site.app_pools.recycling.periodicrestart.requests -> Fixed Intervals; Fixed number of requests; Values are Integer, "0" = disable
  66.  
  67. site.web_apps -> Array of configuration for web application, if the website do not have web application the array can be empty "web_apps": []
  68. 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")
  69. site.web_apps.application_pool - > Name of one the application pool define in site.app_pools.name
  70. site.web_apps.physical_path -> Physical of the web application (eg. D:\\Inetpub\\testsite.test.com\\webapp2\\sub1)
  71.  
  72. site.logging -> Configure the built-in logging module
  73. site.logging.directory -> Configure the path of the logging directory (eg.D:\\IISLogFiles )
  74. site.logging.dontlog -> Enable the logging (be careful of the double negative); Value is bool "True" (Disable logging), "False" (Enable logging)
  75. site.logging.customfield -> Custom field definition like X_Forward_for is definition is only for IIS 8.5, the array can be empty "customfield": []
  76.  
  77. site.logging.customfield.logfieldName -> Name of the field in the log file
  78. site.logging.customfield.sourcename -> Name of the source
  79. site.logging.customfield.sourcetype -> Where the source name is located in the request; Value can be "RequestHeader" or "ResponseHeader" or "ServerVariable"
  80.  
  81. Example using all configuration:
  82.  
  83. [{
  84. "site": {
  85. "name": "dev-testsite.test.com",
  86. "physical_path": "D:\\Inetpub\\testsite.test.com",
  87. "apppoolname": "dev-testsite.test.com",
  88. "bindings": [{
  89. "protocol": "http",
  90. "BindingInformation": "*:80:dev-testsite.test.com"
  91. }],
  92. "app_pools": [{
  93. "name": "dev-testsite.test.com",
  94. "identityType": "NetworkService",
  95. "managedRuntimeVersion": "No Managed Code",
  96. "managedPipelineMode": "Integrated",
  97. "username": "",
  98. "password": "",
  99. "recycling": {
  100. "periodicrestart": {
  101. "virtualmemory": "0",
  102. "privatememory": "0",
  103. "timeInterval": "0",
  104. "requests": "0"
  105. }
  106. }
  107. },
  108. {
  109. "name": "webapp1",
  110. "identityType": "ApplicationPoolIdentity",
  111. "managedRuntimeVersion": "v4.0",
  112. "managedPipelineMode": "Integrated",
  113. "username": "",
  114. "password": ""
  115. }],
  116. "virtualDirectories": [{
  117. "virtual_path": "\\webapp2",
  118. "physical_path": "C:\\lsproot\\test"
  119. }],
  120. "web_apps": [{
  121. "virtual_path": "\\webapp2\\sub1",
  122. "application_pool": "dev-testsite.test.com",
  123. "physical_path": "C:\\lsproot"
  124. },
  125. {
  126. "virtual_path": "\\webapp2\\webapp3",
  127. "application_pool": "dev-testsite.test.com",
  128. "physical_path": "D:\\Inetpub\\testsite.test.com\\webapp2\\webapp3"
  129. }],
  130. "logging": {
  131. "directory": "%SystemDrive%\\inetpub\\logs\\LogFiles",
  132. "dontlog": "False",
  133. "customfield": [{
  134. "logfieldName": "X-FORWARDED-FOR",
  135. "sourcename": "X-FORWARDED-FOR",
  136. "sourcetype": "RequestHeader"
  137. },
  138. {
  139. "logfieldName": "X-FORWARDED-FOR2",
  140. "sourcename": "X-FORWARDED-FOR2",
  141. "sourcetype": "RequestHeader"
  142. }]
  143. }
  144. }
  145. }]
  146.  
  147. #>
  148.  
  149. #param([Parameter(Mandatory=$true)][string]$configpath,[string]$cleanup)
  150.  
  151. $configpath = "C:\data\powershell-script-repository\hgosselin\IIS\test.json"
  152. $cleanup = "true"
  153.  
  154. if($cleanup.length -eq 0){$cleanup = "false"}
  155.  
  156. #initialize collection
  157. $collection =$null
  158. $collection= (Get-Content $configpath) -join "`n" | ConvertFrom-Json
  159.  
  160. $Logfile = "C:\IIS-Sitemanager.log"
  161.  
  162. #Clear log file
  163. if(Test-Path $Logfile)
  164. {
  165. Remove-Item $Logfile
  166. }
  167. #Set $ErrorActionPreference to "Stop" so that any exception terminating or non-terminating stop the script
  168. $ErrorActionPreference ="stop"
  169.  
  170. #Loading module
  171. Import-Module WebAdministration
  172.  
  173. [System.Reflection.Assembly]::LoadFrom("C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll") | Out-Null #To fix problem with cmdlet get-webconfigurationporperty
  174.  
  175.  
  176.  
  177. Function WriteLog
  178. {
  179. Param ([string]$logstring)
  180. Add-content $Logfile -value $logstring
  181. }
  182.  
  183. function CreateDirectory
  184. {
  185. param([string]$folder)
  186.  
  187. if(Test-Path $folder)
  188. {
  189. WriteLog "$(get-date) - Directory $folder - already exist"
  190. }
  191. else
  192. {
  193. new-item -ItemType Directory $folder | Out-Null
  194. WriteLog "$(get-date) - Directory $folder - New folder created"
  195. }
  196. }
  197.  
  198. function Set-WritePermissionOn ($folder,$ApplicationPool)
  199. {
  200.  
  201. $item = Get-Item "IIS:\\AppPools\\$($ApplicationPool)"
  202. $accountIdentity = $null
  203.  
  204. if($item.processModel.identityType -eq "ApplicationPoolIdentity" -or $item.processModel.identityType -eq 4)
  205. {
  206. $accountIdentity = "IIS APPPOOL\$($item.Name)"
  207. }
  208. elseif ($item.processModel.identityType -eq "NetworkService" -or $item.processModel.identityType -eq 2)
  209. {
  210. $accountIdentity = "NT AUTHORITY\NETWORK SERVICE"
  211. }
  212. elseif ($item.processModel.identityType -eq "SpecificUser" -or $item.processModel.identityType -eq 3)
  213. {
  214. $accountIdentity = $item.processModel.userName
  215. }
  216. elseif($item.processModel.identityType -eq "LocalSystem")
  217. {
  218. $accountIdentity = "NT AUTHORITY\SYSTEM"
  219. }
  220.  
  221. #Prepare ACL and rule
  222. $acl = Get-Acl $folder
  223. $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($accountIdentity, "MODIFY", "ContainerInherit,ObjectInherit", "None", "ALLOW")
  224.  
  225. #Check if Permission is already set
  226. if(!(($acl.Access | ?{$_.IdentityReference -eq $accountIdentity}).FileSystemRights) -eq $rule.FileSystemRights)
  227. {
  228. WriteLog "$(get-date) - Logging - Applying WRITE permmission to $accountIdentity on $($folder)"
  229. #Add security rule to current ACL
  230. $acl.AddAccessRule($rule)
  231. Set-Acl $folder $acl
  232. }
  233. else
  234. {
  235. WriteLog "$(get-date) - Logging - Permission on $($config.site.logging.directory) is OK"
  236. }
  237. }
  238.  
  239. $starttime = get-date
  240. WriteLog "$(get-date) - ----------------------- Starting execution ------------------------------------------"
  241.  
  242. ForEach($config in $collection)
  243. {
  244. #Reload server manager config
  245. $servermanager = New-Object Microsoft.Web.Administration.ServerManager
  246. #--------------------------- Initialize validation variable ------------
  247. $CurrentPool = $null
  248. $CurrentSite = $null
  249. $currentWebapp =$null
  250. $Currentdontlog = $null
  251. $customfielddiff = $null
  252.  
  253. #--------------------------- WebAppPool --------------------------------
  254.  
  255. foreach($apppool in $config.site.app_pools)
  256. {
  257. # No apppool present then create with default config.
  258. if(!(Test-Path IIS:\AppPools\$($apppool.name)))
  259. {
  260. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Not found"
  261. New-WebAppPool -Name $apppool.name | Out-Null
  262. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Created"
  263. }
  264. else
  265. {
  266. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Already exist"
  267. }
  268.  
  269. #Already created or not applying proper configuration if needed.
  270. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Verifying configuration"
  271.  
  272. #Getting the current configuration of the application pool.
  273. $CurrentPool = Get-Item IIS:\AppPools\$($apppool.name)
  274.  
  275. #Configure identityType
  276. if($CurrentPool.processModel.identityType -ne $apppool.identityType)
  277. {
  278. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - identityType mismatch"
  279. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Changing identityType from $($CurrentPool.processModel.identityType) to $($apppool.identityType)"
  280. $CurrentPool.processModel.identityType = $apppool.identityType
  281. }
  282. else
  283. {
  284. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - identityType OK"
  285. }
  286.  
  287. #Configure username and password
  288. if($CurrentPool.ProcessModel.Username -ne $apppool.username)
  289. {
  290. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - username mismatch changing username from $($CurrentPool.ProcessModel.Username) to $($apppool.username)"
  291. $CurrentPool.ProcessModel.Username = $apppool.username
  292. $CurrentPool.ProcessModel.Password = $apppool.password
  293. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Changing password"
  294. }
  295. else
  296. {
  297. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - username OK"
  298. }
  299.  
  300. #configure managedRuntimeVersion (.Net version)
  301. if($CurrentPool.managedRuntimeVersion -ne $apppool.managedRuntimeVersion)
  302. {
  303. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - managedRuntimeVersion mismatch changing from $($CurrentPool.managedRuntimeVersion) to $($apppool.managedRuntimeVersion)"
  304. $CurrentPool.managedRuntimeVersion = $apppool.managedRuntimeVersion
  305. }
  306. else
  307. {
  308. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - managedRuntimeVersion is OK"
  309. }
  310.  
  311. #Configure the managedPipelineMode (Classic or Integrated)
  312. if($CurrentPool.managedPipelineMode -ne $apppool.managedPipelineMode)
  313. {
  314. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - managedPipelineMode mismatch changing from $($CurrentPool.managedPipelineMode) to $($apppool.managedPipelineMode)"
  315. $CurrentPool.managedPipelineMode = $apppool.managedPipelineMode
  316. }
  317. else
  318. {
  319. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - managedPipelineMode is OK"
  320. }
  321.  
  322. #Not mandatory field in configuration file, check if its present
  323. if(Get-Member -InputObject $apppool -Name "maxProcesses")
  324. {
  325. #configure Maximum workwer process
  326. if($CurrentPool.processModel.maxProcesses -ne $apppool.maxProcesses)
  327. {
  328. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - maxProcesses mismatch changing from $($CurrentPool.processModel.maxProcesses) to $($apppool.maxProcesses)"
  329.  
  330. $CurrentPool.processModel.maxProcesses = [int]$apppool.maxProcesses
  331. }
  332. else
  333. {
  334. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - maxProcesses is OK"
  335. }
  336. }
  337. else
  338. {
  339. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - maxProcesses is OK"
  340. }
  341.  
  342. #Configure the enable32BitAppOnWin64 true or false
  343. #Not mandatory field in configuration file, check if its present
  344. if(Get-Member -InputObject $apppool -Name "enable32BitAppOnWin64")
  345. {
  346. #configure Maximum workwer process
  347. if([string]$CurrentPool.enable32BitAppOnWin64 -ne $apppool.enable32BitAppOnWin64)
  348. {
  349. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - enable32BitAppOnWin64 mismatch changing from $($CurrentPool.enable32BitAppOnWin64) to $($apppool.enable32BitAppOnWin64)"
  350.  
  351. $CurrentPool.enable32BitAppOnWin64 = $apppool.enable32BitAppOnWin64
  352. }
  353. else
  354. {
  355. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - enable32BitAppOnWin64 is OK"
  356. }
  357. }
  358. else
  359. {
  360. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - enable32BitAppOnWin64 is OK"
  361. }
  362.  
  363. #Configure the cpu.smpAffinitized true or false
  364. #Not mandatory field in configuration file, check if its present
  365. if(Get-Member -InputObject $apppool -Name "smpAffinitized")
  366. {
  367. #configure Maximum workwer process
  368. if([string]$CurrentPool.cpu.smpAffinitized -ne $apppool.smpAffinitized)
  369. {
  370. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - smpAffinitized mismatch changing from $($CurrentPool.cpu.smpAffinitized) to $($apppool.smpAffinitized)"
  371.  
  372. $CurrentPool.cpu.smpAffinitized = $apppool.smpAffinitized
  373. }
  374. else
  375. {
  376. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - smpAffinitized is OK"
  377. }
  378. }
  379. else
  380. {
  381. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - smpAffinitized is OK"
  382. }
  383.  
  384. #configure the Recycling properties
  385. #Not mandatory field in configuration file, check if its present
  386. if(Get-Member -InputObject $apppool -Name "recycling")
  387. {
  388. #Periodic restart on virtual memory (in KB)
  389. if($CurrentPool.recycling.periodicRestart.memory -ne [int]$apppool.recycling.periodicrestart.virtualmemory)
  390. {
  391. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Recycling on virtual memory changing from $($CurrentPool.recycling.periodicRestart.memory) to $($apppool.recycling.periodicrestart.virtualmemory)"
  392. $CurrentPool.recycling.periodicRestart.memory = [int]$apppool.recycling.periodicrestart.virtualmemory
  393. }
  394. else{WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Recycling on vitual memory is OK"}
  395.  
  396. #Periodic restart on private memory (in KB)
  397. if($CurrentPool.recycling.periodicRestart.privateMemory -ne [int]$apppool.recycling.periodicrestart.privatememory)
  398. {
  399. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Recycling on private memory changing from $($CurrentPool.recycling.periodicRestart.privatememory) to $($apppool.recycling.periodicrestart.privatememory)"
  400. $CurrentPool.recycling.periodicRestart.privatememory = [int]$apppool.recycling.periodicrestart.privatememory
  401. }
  402. else{WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Recycling on private memory is OK"}
  403.  
  404. #Periodic restart on regular time interval (in minutes)
  405. #convert Json string to timespan variable
  406. $timeinterval = [timespan]::FromMinutes($apppool.recycling.periodicrestart.timeInterval)
  407. if($CurrentPool.recycling.periodicRestart.time -ne $timeinterval)
  408. {
  409. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Recycling on time interval changing from $($CurrentPool.recycling.periodicRestart.time) to $($apppool.recycling.periodicrestart.timeInterval)"
  410. $CurrentPool.recycling.periodicRestart.time = $apppool.recycling.periodicrestart.timeInterval
  411. }
  412. else{WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Recycling on time interval is OK"}
  413.  
  414. #Recycling on the number of requests
  415. if($CurrentPool.recycling.periodicRestart.requests -ne [int]$apppool.recycling.periodicrestart.requests)
  416. {
  417. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Recycling on requests changing from $($CurrentPool.recycling.periodicRestart.requests) to $($apppool.recycling.periodicrestart.requests)"
  418. $CurrentPool.recycling.periodicRestart.requests = [int]$apppool.recycling.periodicrestart.requests
  419. }
  420. else{WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Recycling on requests is OK"}
  421. }
  422.  
  423.  
  424. #Applying configuration
  425. $CurrentPool | Set-Item
  426. WriteLog "$(get-date) - ApplicationPool $($apppool.name) - Configuration applied"
  427. }
  428. #-------------------------- Physical path creation ----------------------------------------
  429.  
  430. #website physical path
  431. CreateDirectory $config.site.physical_path
  432. #webapplication physical path
  433. foreach($app in $config.site.web_apps)
  434. {
  435. CreateDirectory $app.physical_path
  436. }
  437.  
  438. #-------------------------- WebSite ----------------------------------------------
  439.  
  440. if(!(Test-Path IIS:\Sites\$($config.site.name)))
  441. {
  442. WriteLog "$(get-date) - Website $($config.site.name) - Not found"
  443.  
  444. #IIS7x accepted an psobject collection for binding
  445. #IIS8x dont accept a collection so need adding site with first binding then add.
  446. 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
  447.  
  448. WriteLog "$(get-date) - Website $($config.site.name) - Created"
  449.  
  450. #set remaining binding
  451. if ($config.site.bindings.Count -gt 1)
  452. {
  453. for($i=1; $i -lt $config.site.bindings.Count;$i++ )
  454. {
  455. new-ItemProperty IIS:\Sites\$($config.site.name) -Name bindings -Value @{protocol=$config.site.bindings.Get($i).protocol;bindingInformation=$config.site.bindings.Get($i).bindinginformation}
  456. WriteLog "$(get-date) - Binding $(($config.site.bindings).Get($i).hostheader) - Created "
  457. }
  458. }
  459. }
  460. #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.
  461. #http://support.microsoft.com/kb/890015
  462. #Site exist
  463. else
  464. {
  465. WriteLog "$(get-date) - Website $($config.site.name) - Already exist"
  466. #Check web site configuration
  467. }
  468.  
  469. #****************************** Start website configuration validation (double validation even if its been already created) *****************************************
  470. #****************************** Verifying website Application Pool association *********************************
  471. WriteLog "$(get-date) - Website $($config.site.name) - Verifying configuration"
  472. $CurrentSite = Get-Item IIS:\Sites\$($config.site.name)
  473.  
  474. if($CurrentSite.applicationPool -ne $config.site.apppoolname)
  475. {
  476. WriteLog "$(get-date) - Website $($config.site.name) - application pool mismatch changing from $($CurrentSite.applicationPool) to $($config.site.apppoolname)"
  477. Set-ItemProperty IIS:\Sites\$($config.site.name) -name ApplicationPool $($config.site.apppoolname)
  478. }
  479. else
  480. {
  481. WriteLog "$(get-date) - Website $($config.site.name) - application pool is OK"
  482. }
  483.  
  484. #***************************** Verifying website Physical Path association *********************************
  485.  
  486. if($CurrentSite.physicalPath -ne $config.site.physical_path)
  487. {
  488. WriteLog "$(get-date) - Website $($config.site.name) - physical path mismatch changing from $($CurrentSite.physicalPath) to $($config.site.physical_path)"
  489. Set-ItemProperty IIS:\Sites\$($config.site.name) -name physicalPath $($config.site.physical_path)
  490. }
  491. else
  492. {
  493. WriteLog "$(get-date) - Website $($config.site.name) - physical path is OK"
  494. }
  495.  
  496. #***************************** Verifying website Binding association *********************************
  497. $bindingdiff = $null
  498. $bindingdiff = Compare-Object -ReferenceObject $config.site.bindings -DifferenceObject $CurrentSite.bindings.Collection -Property protocol,BindingInformation
  499. if($bindingdiff -eq $null)
  500. {
  501. WriteLog "$(get-date) - Website $($config.site.name) - Binding is OK"
  502.  
  503. }
  504. else
  505. {
  506. #Not super cleaning removing ALL site bindings,stop state, then reapply the binding collection.
  507. #But I suppose that a faulty binding cause an outage on the website anyways.
  508.  
  509. Get-WebBinding -Name $config.site.name | Remove-WebBinding
  510.  
  511. #$config.site.bindings | %{New-WebBinding -Name $config.site.nane -Protocol $_.protocol -Port $_.port }
  512. new-ItemProperty IIS:\Sites\$($config.site.name) -Name bindings -Value $config.site.bindings
  513.  
  514. WriteLog "$(get-date) - Website $($config.site.name) - Binding mismatch"
  515. }
  516.  
  517.  
  518. #------------------------ Web Application -----------------------------------------
  519.  
  520. foreach($webapp in $config.site.web_apps)
  521. {
  522.  
  523. $servermanager = New-Object Microsoft.Web.Administration.ServerManager
  524. $currentWebapp= $servermanager.Sites["$($config.site.name)"].Applications["$($webapp.virtual_path -replace "\\","/")"]
  525.  
  526. #If webapplication dont exist
  527. if($currentWebapp -eq $null)
  528. {
  529. New-WebApplication -site $config.site.name -Name $webapp.virtual_path -PhysicalPath $webapp.physical_path -ApplicationPool $webapp.application_pool
  530. WriteLog "$(get-date) - WebApplication $($config.site.name)$($webapp.virtual_path) - Created"
  531. }
  532. else
  533. {
  534. WriteLog "$(get-date) - WebApplication $($config.site.name)$($webapp.virtual_path) - Already Exist"
  535. }
  536.  
  537. #****************************** Start WebApplication configuration validation (double validation even if its been already created) *****************************************
  538. WriteLog "$(get-date) - WebApplication $($config.site.name)$($webapp.virtual_path) - Verifying configuration"
  539. #check physical path
  540. if($currentWebapp.VirtualDirectories.physicalpath -ne $webapp.physical_path)
  541. {
  542. #if current config is not the same as config then change it.
  543. WriteLog "$(get-date) - WebApplication $($config.site.name)$($webapp.virtual_path) - physical path mismatch changing from $($currentWebapp.VirtualDirectories.physicalpath) to $($webapp.physical_path)"
  544. Set-ItemProperty IIS:\Sites\$($config.site.name)$($webapp.virtual_path) -name physicalPath $($webapp.physical_path)
  545. }
  546. else
  547. {
  548. WriteLog "$(get-date) - WebApplication $($config.site.name)$($webapp.virtual_path) - physical path is OK"
  549. }
  550.  
  551. #check if Application pool name is proper, the application pool config is already handle.
  552. if($currentWebapp.ApplicationPoolName -ne $webapp.application_pool)
  553. {
  554. WriteLog "$(get-date) - WebApplication $($config.site.name)$($webapp.virtual_path) - application pool mismatch changing from $($currentWebapp.ApplicationPoolName) to $($webapp.application_pool)"
  555. Set-ItemProperty IIS:\Sites\$($config.site.name)$($webapp.virtual_path) -Name applicationPool $($webapp.application_pool)
  556. }
  557. else
  558. {
  559. WriteLog "$(get-date) - WebApplication $($config.site.name)$($webapp.virtual_path) - application pool path is OK"
  560. }
  561. }#end webapp
  562.  
  563. #-------------------------- Logging -------------------------------------------------
  564.  
  565. #Set Enabled value
  566. $servermanager = New-Object Microsoft.Web.Administration.ServerManager
  567. $Currentdontlog = $servermanager.GetApplicationHostConfiguration().GetSection("system.webServer/httpLogging","$($config.site.name)").GetAttribute("dontlog").value.ToString()
  568. if($Currentdontlog -ne ($config.site.logging.dontlog))
  569. {
  570. WriteLog "$(get-date) - Logging $($config.site.name) - Dontlog mismatch changing from $($Currentdontlog) to $($config.site.logging.dontlog)"
  571. $servermanager.GetApplicationHostConfiguration().GetSection("system.webServer/httpLogging","$($config.site.name)").SetAttributeValue("dontlog","$($config.site.logging.dontlog)")
  572. $serverManager.CommitChanges()
  573. }
  574. else
  575. {
  576. WriteLog "$(get-date) - Logging $($config.site.name) - Dontlog is OK"
  577. }
  578.  
  579. #Checking if logging directory is present, if not create
  580. if(!(test-path $config.site.logging.directory))
  581. {
  582. New-Item -ItemType Directory $config.site.logging.directory | Out-Null
  583. WriteLog "$(get-date) - Logging folder $($config.site.logging.directory) - created" | Out-Null
  584. }
  585. else #Logging directory exist
  586. {
  587. WriteLog "$(get-date) - Logging folder $($config.site.logging.directory) - is OK"
  588. }
  589.  
  590. #Set write permission on the logging directory
  591. Set-WritePermissionOn $config.site.logging.directory $config.site.apppoolname
  592.  
  593. #Check if website logging module is properly configure
  594. #Is site logging directory is OK
  595. if($CurrentSite.logFile.directory -ne $config.site.logging.directory)
  596. {
  597. WriteLog "$(get-date) - Logging $($config.site.name) - Logging directory mismatch changing from $($CurrentSite.logFile.directory) to $($config.site.logging.directory)"
  598. Set-ItemProperty IIS:\Sites\$($config.site.name) -Name Logfile.directory -Value $($config.site.logging.directory)
  599. }
  600. else
  601. {
  602. WriteLog "$(get-date) - Logging of $($config.site.name) - Logging directory is OK"
  603. }
  604.  
  605. #If IIS 8.5 check if customs fields are configured
  606. if((get-itemproperty HKLM:\SOFTWARE\Microsoft\InetStp\).Setupstring -eq "IIS 8.5")
  607. {
  608. $filter = "system.applicationHost/sites/site"
  609. $customfielddiff = Compare-Object -ReferenceObject $config.site.logging.customfield -DifferenceObject $CurrentSite.logFile.customFields.Collection -Property logFieldName,sourceName,sourceType
  610.  
  611. #No diffence its OK
  612. if($customfielddiff -eq $null)
  613. {
  614. WriteLog "$(get-date) - Logging $($config.site.name) - Customfields is OK"
  615. }
  616. else
  617. {
  618. #if current site customfield is empty, then add the config
  619. if(($CurrentSite.logFile.customFields.Collection).count -eq 0)
  620. {
  621. #Adding collection
  622. WriteLog "$(get-date) - Logging $($config.site.name) - Customfields adding configuration"
  623. New-ItemProperty "IIS:\Sites\$($config.site.name)" -name logfile.customfields.collection -Value $config.site.logging.customfield -Force
  624. }
  625. else
  626. {
  627. #Difference detected removing custom field collection
  628. WriteLog "$(get-date) - Logging $($config.site.name) - Customfields mismatch deleting configuration"
  629. Remove-WebConfigurationProperty -Filter "$filter[@name='$($config.site.name)']/logfile/customfields" -Name "."
  630. #Adding collection
  631. WriteLog "$(get-date) - Logging $($config.site.name) - Customfields adding configuration"
  632. New-ItemProperty "IIS:\Sites\$($config.site.name)" -name logfile.customfields.collection -Value $config.site.logging.customfield
  633. }
  634. }
  635. }
  636.  
  637. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement