Created
September 25, 2014 18:55
-
-
Save FrankDeGroot/8237b8bc4b2777cc8336 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| param ( | |
| [switch]$Create, | |
| [switch]$Drop | |
| ) | |
| if ($Create) { Create } | |
| if ($Drop) { Drop } | |
| Write-Host "Laden modulen voor IIS en SQL Server..." | |
| Import-Module WebAdministration | |
| Push-Location | |
| Import-Module sqlps -DisableNameChecking | |
| Pop-Location | |
| Add-PSSnapin WDeploySnapin3.0 | |
| $parameters = Join-Path $PSScriptRoot 'Parameters.ps1' | |
| Invoke-Expression ". $parameters" | |
| Function Create() { | |
| CreateCerts $certs | |
| $sites | ForEach-Object { | |
| CreateAppPool $_ | |
| CreateSite $_ | |
| CreateSqlLogin $_ | |
| DeploySite $_ | |
| SetLog4Net $_ | |
| SetConfig $_ | |
| } | |
| CreateJob | |
| } | |
| Function Drop() { | |
| DropCerts $certs | |
| $sites | ForEach-Object { | |
| DropSite $_ | |
| DropAppPool $_ | |
| } | |
| DropJob | |
| } | |
| Function CreateCerts($certs) { | |
| Push-Location IIS:\SslBindings | |
| $certs | ForEach-Object { | |
| $name = SslBindingName $_ | |
| if (!(Test-Path $name)) { | |
| Write-Host "Creating SSL entry for certificate '$($_.friendlyName)' for IP '$($_.ip)', port '$($_.port)' and host '$($_.host)'." | |
| FindCert $_.friendlyName | New-Item $name | Out-Null | |
| } | |
| } | |
| Pop-Location | |
| } | |
| Function DropCerts($certs) { | |
| Push-Location IIS:\SslBindings | |
| $certs | ForEach-Object { | |
| $name = SslBindingName $_ | |
| if (Test-Path $name) { | |
| Write-Host "Removing SSL for IP '$($_.ip)', port '$($_.port)' and host '$($_.host)'." | |
| Remove-Item $name | |
| } | |
| } | |
| Pop-Location | |
| } | |
| Function CreateAppPool($site) { | |
| Push-Location IIS:\AppPools\ | |
| if (!(Test-Path $site.appPool -pathType container)) { | |
| Write-Host "Creating App Pool '$($site.appPool)'." | |
| $appPool = New-Item $site.appPool | |
| if ($site.user) { | |
| $appPool | Set-ItemProperty -Name processModel -value @{userName=$site.user;password=$site.password;identitytype=3} | |
| } | |
| } | |
| Pop-Location | |
| } | |
| Function DropAppPool($site) { | |
| Push-Location IIS:\AppPools\ | |
| if (Test-Path $site.appPool -pathType container) { | |
| Write-Host "Removing App Pool '$($site.appPool)'." | |
| Remove-Item $site.appPool -Recurse | |
| } | |
| Pop-Location | |
| } | |
| Function CreateSite($site) { | |
| Push-Location IIS:\Sites\ | |
| if (!(Test-Path $site.name -pathType container)) { | |
| $dir = GetWebAppDir $site | |
| if(!(Test-Path $dir -PathType container)) { | |
| Write-Host "Creating Website directory '$dir'." | |
| New-Item $dir -ItemType directory | Out-Null | |
| } | |
| Write-Host "Creating Website '$($site.name)'." | |
| $binding = @{ | |
| protocol = $site.scheme; | |
| bindingInformation = "$($site.ip):$($site.port):$($site.host)" | |
| } | |
| $iisApp = New-Item $site.name -bindings $binding -physicalPath $dir | |
| $iisApp | Set-ItemProperty -Name 'applicationPool' -Value $site.appPool | |
| } | |
| Pop-Location | |
| } | |
| Function DropSite($site) { | |
| Push-Location IIS:\Sites\ | |
| $dir = GetWebAppDir $site | |
| if (Test-Path $dir -PathType container) { | |
| StopAppPool $site | |
| Write-Host "Removing Website directory '$dir'." | |
| Remove-Item $dir -Recurse | |
| } | |
| if (Test-Path $site.name -pathType container) { | |
| Write-Host "Removing Website '$($site.name)'." | |
| Remove-Item $site.name -Recurse | |
| } | |
| Pop-Location | |
| } | |
| Function DeploySite($site) { | |
| $packageDir = Join-Path $packagesDir "$($site.package)_Package\" | |
| #Write-Host "Using packages in '$packageDir'." | |
| $package = Join-Path $packageDir "$($site.package).zip" | |
| Restore-WDPackage -Package $package -Parameters (GetParameters $packageDir $site) | |
| } | |
| Function SetLog4Net($site) { | |
| $dir = GetWebAppDir $site | |
| $path = Join-Path $dir 'log4net.config' | |
| if (Test-Path $path) { | |
| $config = [xml](Get-Content $path) | |
| $ado = $config.log4net.appender | Where { $_.name -eq 'AdoNetAppender' } | Select -First 1 | |
| $connString = $ado.connectionString | |
| $connString.value = $connString.value -ireplace 'data source=[^;]+', "data source=$sqlServerInstance" | |
| $config.Save($path) | |
| } | |
| } | |
| Function SetConfig($site) { | |
| $dir = GetWebAppDir $site | |
| $path = Join-Path $dir 'Web.config' | |
| $hostFqdn = [System.Net.Dns]::GetHostName() | |
| $config = [xml](Get-Content $path) | |
| $ssrsUrl = $config.configuration.appSettings.add | Where { $_.key -eq 'reportServerUrl' } | Select -First 1 | |
| if ($ssrsUrl) { | |
| $ssrsUrl.value = $reportServerUrl | |
| } | |
| $moUrl = $config.configuration.appSettings.add | Where { $_.key -eq 'MOUrl' } | Select -First 1 | |
| if ($moUrl) { | |
| $moSite = $sites | Where { $_.name -eq 'MO' } | Select -First 1 | |
| if ($moSite.host) { | |
| $moUrl.value = "http://$($moSite.host)/" | |
| } else { | |
| $moUrl.value = "http://$($hostFqdn):$($moSite.port)/" | |
| } | |
| } | |
| $svcUrl = $config.configuration['system.serviceModel'].client.endpoint | Where { $_.contract -eq 'AuthenticationService.ITokenService' } | Select -First 1 | |
| if ($svcUrl) { | |
| $svcSite = $sites | Where { $_.name -eq 'MOAuthService' } | Select -First 1 | |
| if ($moSite.host) { | |
| $svcUrl.address = "http://$($moSite.host)/TokenService.svc" | |
| } else { | |
| $svcUrl.address = "http://$($hostFqdn):$($svcSite.port)/TokenService.svc" | |
| } | |
| } | |
| $config.Save($path) | |
| } | |
| Function CreateJob { | |
| DropJob | |
| Write-Host 'Create Job MO Token Cleanup.' | |
| InvokeSqlCmdFile 'CreateJobMOTokenCleanup.sql' | |
| } | |
| Function DropJob { | |
| Write-Host 'Drop Job MO Token Cleanup.' | |
| InvokeSqlCmdFile 'DropJobMOTokenCleanup.sql' | |
| } | |
| Function SslBindingName($cert) { | |
| $parts = ($cert.ip, $cert.port, $cert.host) | Where { $_ } | |
| [string]::Join('!', $parts) | |
| } | |
| Function FindCert($friendlyName) { | |
| Get-ChildItem -Recurse $certPath | Where { | |
| $_ -is [System.Security.Cryptography.X509Certificates.X509Certificate2] -and | |
| $_.FriendlyName -eq $friendlyName | |
| } | Select -First 1 | |
| } | |
| Function GetWebAppDir($site) { | |
| Join-Path $site.dir $site.name | |
| } | |
| Function StopAppPool($site) { | |
| Push-Location IIS:\AppPools | |
| Write-Host "Recycling '$($site.appPool)'." | |
| Stop-WebAppPool $site.appPool | |
| Pop-Location | |
| } | |
| Function GetParameters($packageDir, $site) { | |
| $parametersPath = Join-Path $packageDir "$($site.package).SetParameters.xml" | |
| #Write-Host "Using parameters file '$parametersPath'." | |
| $parameters = [xml](Get-Content $parametersPath) | |
| $webAppName = GetParameter $parameters 'IIS Web Application Name' | |
| $newParameters = @{ 'IIS Web Application Name' = $site.name } | |
| $moDB = GetParameter $parameters 'MOEntities-Web.config Connection String' | |
| if ($moDB) { | |
| $newParameters['MOEntities-Web.config Connection String'] = | |
| $moDB.value -ireplace 'data source=[^;]+', "data source=$sqlServerInstance" | |
| } | |
| $tokenDB = GetParameter $parameters 'TokenModel-Web.config Connection String' | |
| if ($tokenDB) { | |
| $newParameters['TokenModel-Web.config Connection String'] = | |
| $tokenDB.value -ireplace 'server=[^;]+', "server=$sqlServerInstance" | |
| } | |
| $newParameters | |
| } | |
| Function GetParameter($parameters, $name) { | |
| $parameters.parameters.setParameter | Where { $_.name -eq $name } | Select -First 1 | |
| } | |
| Function CreateSqlLogin($site) { | |
| if($site.user -eq '') { return } | |
| $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $sqlServerInstance | |
| # drop login if it exists | |
| if ($server.Logins.Contains("$($site.domain)\$($site.user)")) { | |
| Write-Host "Deleting the existing login '$($site.domain)\$($site.user)'." | |
| $server.Logins["$($site.domain)\$($site.user)"].Drop() | |
| } | |
| Write-Host "Create login '$($site.domain)\$($site.user)'." | |
| InvokeSqlcmd "CREATE LOGIN [$($site.domain)\$($site.user)] FROM WINDOWS WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english]" | |
| $site.sqlRoles | ForEach-Object { CreateSqlUser $server $site $_ } | |
| } | |
| Function CreateSqlUser($server, $site, $sqlRole) { | |
| foreach($database in $server.Databases | Where { $_.Name -eq $sqlRole.db }) { | |
| if($database.Users.Contains("$($site.domain)\$($site.user)")) { | |
| Write-Host "Drop user '$($site.domain)\$($site.user)' in database '$($sqlRole.db)'." | |
| $database.Users["$($site.domain)\$($site.user)"].Drop(); | |
| } | |
| } | |
| Write-Host "Create user '$($site.domain)\$($site.user)' in database '$($sqlRole.db)'." | |
| InvokeSqlcmd "USE [$($sqlRole.db)]; CREATE USER [$($site.domain)\$($site.user)] FOR LOGIN [$($site.domain)\$($site.user)]" | |
| Write-Host "Add role '$($sqlRole.role)' to user '$($site.domain)\$($site.user)' in database '$($sqlRole.db)'." | |
| InvokeSqlcmd "USE [$($sqlRole.db)]; EXEC sp_addrolemember '$($sqlRole.role)', '$($site.domain)\$($site.user)'" | |
| } | |
| Function InvokeSqlCmdFile($name) { | |
| $scriptPath = Join-Path $PSScriptRoot $name | |
| $script = Get-Content $scriptPath | |
| $query = [string]::Join(' ', [string[]]$script) | |
| InvokeSqlCmd $query | |
| } | |
| Function InvokeSqlCmd($query) { | |
| #Write-Host "Executing query '$query'." | |
| Invoke-Sqlcmd -Query $query -ServerInstance $sqlServerInstance | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment