Atria.Remote.Setup.ps1

<#PSScriptInfo
 
.VERSION 1.0.10
 
.GUID 438791b3-087c-446d-a8bd-924e6d97bc82
 
.AUTHOR Automate101
 
.COMPANYNAME Automate101
 
.COPYRIGHT Automate101 Ltd
 
.TAGS
 
.LICENSEURI https://getatria.com/
 
.PROJECTURI https://getatria.com/
 
.ICONURI https://getatria.com/wp-content/uploads/2020/06/Atria_ApprovedIcon_WithWordmark_Colour_PNG.png
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
#>

#requires -RunAsAdministrator

<#
 
.DESCRIPTION
 Installs the necesarry components to manage a remote active directory with Atria
 
#>
 

param(
    [parameter(Mandatory=$true)]
    [string]$InstallToken,
    [parameter(Mandatory=$true)]
    [string]$ExternalApiUrl
)

$ErrorActionPreference = "stop"

$url = '{0}/api/environments/install/installs/setup-data?token={1}' -f $ExternalApiUrl, $InstallToken

$response = Invoke-WebRequest $url -UseBasicParsing

if ($response.StatusCode -ne 200) {
  Write-Verbose $response.Content
  Write-Error "Failed to get data from external api"
  exit
}

$installJson = ConvertFrom-Json $response.Content
$installConfig = $InstallJson.Data

$RepositoryName = 'Atria'
$V2RepositoryLocation = $installConfig.FeedUrl.Replace('/v3/index.json', '/v2')

$passwordSecureString = ConvertTo-SecureString -String $InstallConfig.feedUser -AsPlainText -force

$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($InstallConfig.feedToken, $passwordSecureString)

if (([Net.ServicePointManager]::SecurityProtocol).ToString().split(',').Trim() -notContains 'Tls12') {
  [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
}

if (!(Get-PackageProvider | Where-Object {$_.Name -eq 'NuGet'})) {
  $null = Install-PackageProvider -Name Nuget -MinimumVersion 2.8.5.201 -Force -Confirm:$false
}

Write-Host 'Setup PSRepository'
$repo = Get-PSRepository -Name $RepositoryName -ErrorAction SilentlyContinue
if (!$repo) {
  Register-PSRepository -Name $RepositoryName -SourceLocation $V2RepositoryLocation -Credential $credential -InstallationPolicy Trusted
} else {
  Set-PSRepository -Name $RepositoryName -SourceLocation $V2RepositoryLocation -Credential $credential -InstallationPolicy Trusted
}

#Install-Module -Name Atria.Install -Repository Atria -Credential $credential
Install-Module -Name Atria.Tools -Repository $RepositoryName -Credential $credential -Force -AllowClobber
Install-Module -Name Atria.Platform -Repository $RepositoryName -Credential $credential -Force -AllowClobber

Get-Module Atria.Tools | Remove-Module -Confirm:$false -Force
Import-Module -Name Atria.Tools -force

Connect-AtriaFeed -UserName $InstallConfig.feedUser -PersonalAccessToken $InstallConfig.feedToken -FeedUrl $InstallConfig.feedUrl

Unregister-PSRepository -Name Atria

Write-Host "Please enter domain admin credentials"

If ($installConfig.EnvironmentType -eq 'Dedicated' -or $installConfig.EnvironmentType -eq 'Private') {
    Install-AtriaConfigService `
     -Credential (Get-Credential) `
     -StoreSecretsInPlatformEnvironment `
     -MessagingUrl $installConfig.messagingUrl `
     -MessagingUsername $installConfig.messagingUser `
     -MessagingPassword $installConfig.messagingPassword `
     -CustomerId $installConfig.CustomerId `
     -EnvironmentId $installConfig.EnvironmentId
     
    Install-AtriaAgent
    
    Install-AtriaProvisioning
    
    Install-AtriaDirectory
} else {
    Install-AtriaConfigService `
     -Credential (Get-Credential) `
     -StoreSecretsInPlatformEnvironment `
     -MessagingUrl $installConfig.messagingUrl `
     -MessagingUsername $installConfig.messagingUser `
     -MessagingPassword $installConfig.messagingPassword `
     -LocationId $installConfig.LocationId `
     -EnvironmentId $installConfig.EnvironmentId
     
    Install-AtriaAgent
    
    Install-AtriaProvisioning
    
    Install-AtriaDirectory
}