[CmdletBinding()] Param( [Parameter(Mandatory=$True)] [string]$client, [Parameter(Mandatory=$True)] [string]$emDashEnvironment, [Parameter(Mandatory=$True)] [string]$buildNumber ) function ValidateCredentials($credential, $errorMessage) { if (-Not($credential.UserName -and $credential.Password.Length -ne 0)) { Write-Error $errorMessage Exit } } function Get-RemoteFile ($url,$Target,$username,$password, $sourceFile) { $authInfo = $username + ":" + $password $authInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::Default.GetBytes($authInfo)) $webRequest = [System.Net.WebRequest]::Create($url) $webRequest.Headers["Authorization"] = "Basic " + $authInfo $webRequest.PreAuthenticate = $true $webRequest.Timeout = 600000 try { $resp = $webRequest.GetResponse() } catch [System.Net.WebException] { $resp = $_.Exception.Response } if ($resp.StatusCode -ne 'OK') { Write-Error "ERROR - Unable to get Team City artefact at URL $url" exit 1 } # get a download stream from the server response $responsestream = $resp.GetResponseStream() # create the target file on the local system and the download buffer $targetfile = New-Object IO.FileStream ($Target,[IO.FileMode]::Create) [byte[]]$readbuffer = New-Object byte[] 1024 # loop through the download stream and send the data to the target file do{ $readlength = $responsestream.Read($readbuffer,0,1024) $targetfile.Write($readbuffer,0,$readlength) } while ($readlength -ne 0) $targetfile.close() $destinationFolder = $sourceFile -replace '.zip','' $destination = 'c:\emdash\installer' [io.compression.zipfile]::ExtractToDirectory($Target, $destination) } function Get-EmdashDatabaseAndServer ($username,$password,$client,$environment) { $authInfo = $username + ":" + $password $authInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::Default.GetBytes($authInfo)) $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $authHeader = "Basic " + $authInfo $headers.Add("Authorization", $authHeader) $url = "https://downloads.emdashsoftware.com/LicenceManager/api/Environment?client=" + $client + "&emDashEnvironment=" + $environment $response = Invoke-RestMethod $url -Headers $headers Return $response } $teamCityUser="emdash\emdash" $teamCityPassword="D0wnLoads!" $appPoolCredentials = $host.ui.PromptForCredential("Service account credentials", "Please enter emDash service account username and password.", "", "") ValidateCredentials($appPoolCredentials,"emDash service account credentials must be entered.") $appPoolPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($appPoolCredentials.Password)) Add-Type -assembly "system.io.compression.filesystem" $sourceFileUrl = "https://downloads.emdashsoftware.com/LicenceManager/api/Artefact?buildNumber=" + $buildNumber + "&artefactName=Build" $sourceFile = "Build.zip" # Create the installer directory If (Test-Path 'c:\emdash\installer'){ Remove-Item 'c:\emdash\installer\*' -recurse } Else { New-Item 'c:\emdash\installer' -type directory } $targetFilePath = 'c:\emdash\installer\' + $sourceFile Get-RemoteFile $sourceFileUrl $targetFilePath $teamCityUser $teamCityPassword $sourceFile 'c:\emdash\installer' $command = 'c:\emdash\installer\emDash.Build.exe' # Update the connection string for build.exe $emdashDatabaseAndServer = Get-EmdashDatabaseAndServer $teamCityUser $teamCityPassword $client $emDashEnvironment $database = $emdashDatabaseAndServer.EmdashDatabaseName $server = $emdashDatabaseAndServer.EmdashDatabaseServer $argList = "$database $server" Invoke-Expression "c:\emdash\installer\scripts\workflows\UpdateBuildExeConfigFile.ps1 $argList" # Updated the build exe connection strings $AllArgs = @('EMDASHDEPLOY',$client , $emDashEnvironment, $teamCityUser, $teamCityPassword, $buildNumber, $appPoolCredentials.UserName, $appPoolPassword, $appPoolCredentials.UserName, $appPoolPassword, $database,$server) Write-Host "Starting Deploy emDash." & $command $AllArgs Write-Host "Completed Deploy emDash."