Created
January 28, 2016 23:45
-
-
Save crshnbrn66/2a45a52712dd8190ac73 to your computer and use it in GitHub Desktop.
This file contains 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
#http://www.iis.net/learn/extensions/configuring-application-request-routing-(arr)/achieving-high-availability-and-scalability-arr-and-nlb | |
# This script is used in octopus to deploy ARR to host targetted for it. | |
# | |
function stop-WAS | |
{ | |
$i = 0 | |
$service = get-service WAS -ErrorAction SilentlyContinue | |
if($service) | |
{ | |
if($service.status -ne 'Stopped') | |
{ | |
While($i -lt 3) | |
{ | |
$i +=1 | |
Write-output 'Stopping WAS' | |
Stop-Service $service.Name -Force | Write-Host | |
$service = get-service $service.name | |
Start-Sleep 10 | |
} | |
if($i -eq 3 -and $service.Status -ne 'Stopped') | |
{ | |
write-error 'Cannot Stop WAS Service -- Stop manually' | |
} | |
else{ $Status ='Stopped'} | |
} | |
else | |
{ | |
$Status ='Stopped' | |
} | |
} | |
return $status | |
} | |
function start-WAS | |
{ | |
$i =0 | |
$service = get-service WAS -ErrorAction SilentlyContinue | |
If($service) | |
{ | |
if (($service.status) -ne 'Running') | |
{ | |
While($i -lt 3) | |
{ | |
$i +=1 | |
Write-output 'Starting WAS' | |
start-Service $service.Name | Write-Host | |
$service = get-service $service.name | |
Start-Sleep 10 | |
} | |
if($i -eq 3 -and $service.Status -ne 'Running') | |
{ | |
write-error 'Cannot start WAS Service -- Start manually' | |
} | |
} | |
} | |
} | |
function stop-wmsvc | |
{ | |
$service = get-service wmsvc -ErrorAction SilentlyContinue | |
if($service) | |
{ | |
if($service.status -ne 'Stopped') | |
{ | |
While($i -lt 3) | |
{ | |
$i +=1 | |
Write-output 'Stopping WMSVC' | |
Stop-Service $service.Name | Write-Host | |
$service = get-service $service.name | |
Start-Sleep 10 | |
} | |
if($i -eq 3 -and $service.Status -ne 'Stopped') | |
{ | |
write-error 'Cannot Stop wmsvc Service -- Stop manually' | |
} | |
else{ $Status ='Stopped'} | |
} | |
else | |
{ | |
$Status ='Stopped' | |
} | |
} | |
return $status | |
} | |
function start-wmsvc | |
{ | |
$service = get-service wmsvc -ErrorAction SilentlyContinue | |
if($service) | |
{ | |
While((($service.status) -ne 'Running') -or ($i -lt 3)) | |
{ | |
$i +=1 | |
Write-output 'Starting WMSVC' | |
start-Service $service.Name | Write-Host | |
$service = get-service $service.name | |
Start-Sleep 10 | |
} | |
if($i -eq 3 -and $service.Status -ne 'Running') | |
{ | |
write-error 'Cannot start wmsvc Service -- Start manually' | |
} | |
} | |
} | |
function start-w3svc | |
{ | |
$service = get-service w3svc -ErrorAction SilentlyContinue | |
if($service) | |
{ | |
While((($service.status) -ne 'Running') -or ($i -lt 3)) | |
{ | |
$i +=1 | |
Write-output 'Starting W3SVC' | |
start-Service $service.Name | Write-Host | |
$service = get-service $service.name | |
Start-Sleep 10 | |
} | |
if($i -eq 3 -and $service.Status -ne 'Running') | |
{ | |
write-error 'Cannot start w3svc Service -- Start manually' | |
} | |
} | |
} | |
function ping-msiModule | |
{ | |
param([string]$msiModulePath) | |
pushd | |
$MsiInstalled = $null | |
if(((Get-Module -name msi).Name)-ne 'MSI') | |
{ | |
import-module $msiModulePath |Write-Host | |
if((Get-Module -name msi).Name) | |
{$installed = $true} | |
} | |
if(((Get-Module -name msi).Name)-eq 'MSI') | |
{ | |
$installed = $true | |
} | |
else | |
{ | |
$installed = $false | |
} | |
popd | |
return $installed | |
} | |
function remove-msiModule | |
{ | |
param([string]$msiModulePath) | |
pushd | |
$MsiInstalled = $null | |
if(((Get-Module -name msi).Name)-ne 'MSI') | |
{ | |
import-module $msiModulePath |Write-Host | |
if((Get-Module -name msi).Name) | |
{$installed = $true} | |
} | |
if(((Get-Module -name msi).Name)-eq 'MSI') | |
{ | |
Remove-Module 'MSI' |Write-Host | |
$installed = $true | |
} | |
else | |
{ | |
Write-host 'Cannot remove module MSI' | |
$installed = $false | |
} | |
popd | |
return $installed | |
} | |
function ping-MsiInstalled | |
{ | |
param([string]$msiName) | |
$MsiInstalled = (Get-MSIProductInfo | Where-Object{$_.ProductName -like "*$MsiName*"}).ProductCode | |
write-host "PingMsiInstalled: $msiName Product Code is $msiInstalled" | |
return $MsiInstalled | |
} | |
function install-MSI | |
{ | |
param([string]$fullMsiPackagePath) | |
if(test-path $fullMsiPackagePath) | |
{ | |
$PackageName = (get-item $fullMsiPackagePath).name | |
$PackagePath = (get-childitem $fullMsiPackagePath).FullName | |
$logPath = "c:\temp\" | |
if(!(test-path $logPath -ErrorAction SilentlyContinue)) | |
{ | |
mkdir $logPath | |
} | |
$a = " /i $PackagePath /l c:\temp\$packageName.log /quiet" | |
write-host "Install MSI: Installing $fullMsiPackagePath" | |
start-process msiexec.exe -ArgumentList $a -Wait | |
} | |
} | |
function uninstall-MSI | |
{ | |
param([string]$fullMsiPackagePath) | |
if(test-path $fullMsiPackagePath) | |
{ | |
$PackageName = (get-item $fullMsiPackagePath).name | |
$PackagePath = (get-childitem $fullMsiPackagePath).FullName | |
$args2 = " /x $PackagePath /quiet" | |
start-process msiexec.exe -ArgumentList $args2 -Wait | |
} | |
} | |
function get-MsiName | |
{ | |
param([string]$packagePath) | |
$packageName = (Get-MSISummaryInfo -path $packagePath).Subject | |
write-host "GetMSIName: Package name is $packageName" | |
return $packageName | |
} | |
function Confirm-IIS | |
{ | |
#if this function finds out in the addition of windows features that anything must be installed it'll pass back the value Reboot | |
#Other values returned by this function include Installed - which means all necessary features are installed. | |
#or if it cannot install a function this will throw an exception. | |
$installed = $false #confirm IIS modules are installed. | |
#Not installing these modules as they cannot seem to be found on 2k12 r2 "PowerShell-V2","NET-Framework-Core","NET-Framework-Features","Web-Asp-Net","Web-Net-Ext" | |
$iisModules = "FileAndStorage-Services","File-Services","FS-FileServer","Storage-Services","Web-Server","Web-WebServer","Web-Common-Http","Web-Default-Doc","Web-Dir-Browsing","Web-Http-Errors","Web-Static-Content","Web-Http-Redirect","Web-Health","Web-Http-Logging","Web-Request-Monitor","Web-Performance","Web-Stat-Compression","Web-Security","Web-Filtering","Web-Basic-Auth","Web-CertProvider","Web-IP-Security","Web-Url-Auth","Web-Windows-Auth","Web-App-Dev","Web-Net-Ext45","Web-ASP","Web-Asp-Net45","Web-ISAPI-Ext","Web-ISAPI-Filter","Web-Ftp-Server","Web-Ftp-Service","Web-Mgmt-Tools","Web-Mgmt-Console","Web-Scripting-Tools","Web-Mgmt-Service","NET-Framework-45-Features","NET-Framework-45-Core","NET-Framework-45-ASPNET","NET-WCF-Services45","NET-WCF-TCP-PortSharing45","MSMQ","MSMQ-Services","MSMQ-Server","FS-SMB1","User-Interfaces-Infra","Server-Gui-Mgmt-Infra","Server-Gui-Shell","PowerShellRoot","PowerShell","WoW64-Support" | |
Write-host "checking iisModules: $iisModules" | |
foreach($module in $iisModules) | |
{ | |
Write-output "func Confirm-IIS: checking for $module" | |
$featureStatus = (Get-WindowsFeature -Name $module).InstallState | |
if($featureStatus -ne 'Installed') | |
{ | |
try | |
{ | |
$installResults = Add-WindowsFeature -Name $module | |
if(($installResults.RestartNeeded -eq $true)-and ($installed -ne 'Reboot')) #only set Reboot one and only one time | |
{ | |
Write-host "Func Confirm-IIS: $module Requires a Reboot" | |
$installed = 'Reboot' | |
} | |
elseif(($installResults.Success -eq $true) -and ($installed -ne 'Reboot')) | |
{ | |
Write-host "Func Confirm-IIS: $module has been installed" | |
$installed = 'Installed' | |
} | |
} | |
catch | |
{ | |
"Error was $_" | |
$line = $_.InvocationInfo.ScriptLineNumber | |
"Error was in Line $line" | |
} | |
} | |
else | |
{ | |
Write-host "Func Confirm-IIS: $module has already been installed" | |
} | |
} | |
} | |
#msi module location is found here https://psmsi.codeplex.com/wikipage?title=Help.v2.3.0&FocusElement=Comment | |
#for this module the msi was installed and the corresponding module directory was extraced and included with this Solution.\ | |
#$installPath = 'D:\arrInstall' | |
Write-Output "InstallMSI Octopus Variable is set to: $installMSI" | |
Write-Output "InstallPath Octopus Variable is set to: $installPath" | |
if(($installMSI -ne $null) -and ($installPath -ne $null)) | |
{ | |
write-output "Attempting to Confirm Necessary Features are installed" | |
$IISConfigured = Confirm-IIS #proper IIS Features must be installed first. If they get installed and windows claims a reboot is necessary then this script must be run again after the required features are added. | |
if($IISConfigured -ne 'Reboot') | |
{ | |
$msiModulePath = "$installPath\modules\MSI" | |
write-host "install path: $installPath ModulePath: $msiModulePath" | |
$moduleInstalled = ping-msiModule $msiModulePath | |
$msiPath = "$installPath" | |
$msi2install = 'webfarm_v1.1_amd64_en_us.msi','requestRouter_x64.msi','rewrite_amd64_en-US.msi' | |
Write-Output "MsiModules are $msi2Install" | |
if ($moduleInstalled) | |
{ | |
try | |
{ | |
foreach($msi in $msi2install) | |
{ | |
$msiFullPath = (get-item "$msiPath\$msi").FullName | |
$arrFriendlyName = get-msiName $msiFullPath | |
$installed = ping-MsiInstalled $arrFriendlyName | |
write-output "$arrFriendlyName ID is $installed" | |
if(!$installed) | |
{ | |
$serviceStatusWas = Stop-WAS | |
$serviceStatusWmsvc = stop-wmsvc | |
if($InstallMsi -eq 'Install') | |
{ | |
write-output "Attempting to install $msifullpath $arrFriendlyName" | |
Install-MSI $msiFullPath | |
ping-MsiInstalled $arrFriendlyName | write-host | |
} | |
} | |
elseif ($installMsi -eq 'Remove') | |
{ | |
write-output "Attempting to remove $msifullpath $arrFriendlyName" | |
uninstall-MSI $msiFullPath | |
ping-MsiInstalled $arrFriendlyName | write-host | |
} | |
elseif ($installMSI -eq $null) | |
{ | |
write-host 'InstallMSI switch must be either set to Install or Remove' | |
} | |
} | |
if($serviceStatusWas -eq 'Stopped') | |
{ | |
start-WAS | |
$w3start = $true | |
} | |
if($serviceStatusWmsvc -eq 'Stopped') | |
{ | |
start-wmsvc | |
$w3start = $true | |
} | |
if($w3start -eq $true) | |
{ | |
start-w3svc | |
} | |
} | |
catch | |
{ | |
} | |
} | |
else | |
{ | |
Write-Error "Error: Cannot install MSI Powershell module Exiting -- $msiModulePath" | |
} | |
remove-msiModule | |
} | |
else | |
{ | |
Write-Warning "Windows Features have been added machine must be Rebooted and Octopus Step run Once again" | |
} | |
} | |
else | |
{ | |
Write-Warning "Octopus variables are not setInstallMSI " | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment