Scripts/Connect-BPAServer.ps1
function Connect-BPAServer { <# .SYNOPSIS Connect to an AutoMate BPA management server .DESCRIPTION Connect-BPAServer gathers connection information for AutoMate BPA, and tests authentication. This module supports connecting to multiple servers at once. .PARAMETER BPAServer The AutoMate BPA management server. One or more can be provided. The same credentials are used for all servers. .PARAMETER Port The TCP port for the management API. .PARAMETER Credential The credentials use during authentication. .PARAMETER UserName The username to use during authentication. .PARAMETER Password The password to use during authentication. .PARAMETER CredentialStoreFilePath The file that credentials are stored in. .EXAMPLE Connect-BPAServer -BPAServer "bpa01" -Credential (Get-Credential) .NOTES Author(s): : David Seibel Contributor(s) : Date Created : 08/24/2016 Date Modified : 04/27/2017 .LINK https://github.com/davidseibel/PoshBPA #> [CmdletBinding(DefaultParameterSetName = "ByCredentialStore")] param( [Parameter(Position = 0, Mandatory = $true)] [ValidateNotNullOrEmpty()] [string[]]$BPAServer, [ValidateNotNullOrEmpty()] [int]$Port = 9608, [Parameter(ParameterSetName = "ByCredential")] [ValidateNotNullOrEmpty()] [System.Management.Automation.PSCredential]$Credential, [Parameter(ParameterSetName = "ByUserPass")] [ValidateNotNullOrEmpty()] [string]$UserName, [Parameter(ParameterSetName = "ByUserPass")] [ValidateNotNullOrEmpty()] [Security.SecureString]$Password, [Parameter(ParameterSetName = "ByCredentialStore")] [ValidateScript({ if (Test-Path -Path $_) { $true } else { throw [System.Management.Automation.PSArgumentException]"CredentialStoreFilePath '$_' does not exist!" } })] [string]$CredentialStoreFilePath = "$($env:APPDATA)\PoshBPA\credstore.xml", [switch]$SaveCredential ) if ($PSCmdlet.ParameterSetName -eq "ByUserPass") { $Credential = New-Object System.Management.Automation.PSCredential ($UserName, $Password) } foreach ($server in $BPAServer) { if ($null -eq (Get-Variable BPAConnectionInfo -ErrorAction SilentlyContinue)) { # Store connection info for multiple servers in an array $global:BPAConnectionInfo = @() } elseif ($global:BPAConnectionInfo.Server -contains $server) { throw "Already connected to server $server." } # If no credentials were supplied, check the credential store if ($PSCmdlet.ParameterSetName -eq "ByCredentialStore") { if (Test-Path -Path $CredentialStoreFilePath) { $tempCred = Get-BPACredentialStoreItem -BPAServer $server -File $CredentialStoreFilePath } # If no credentials are found in the store, prompt if ($tempCred -eq $null) { if (-not ($tempCred = Get-Credential -Message "Enter credentials for BPA server '$server'")) { throw "No credentials specified for server '$server'!" } } $Credential = $tempCred } try { $serverInfo = Invoke-RestMethod "http://$($server):$($Port)/BPAManagement/info/get" -Method Get -ErrorAction Stop } catch { throw "Failed to connect to BPA server $($server):$($Port)!" return } $tempConnectionInfo = [PSCustomObject]@{ "Server" = $server "Port" = $Port "Credential" = $Credential "Info" = $serverInfo.Data } # Make sure connection info variable is still an array, if not, make it an array if ($global:BPAConnectionInfo -is [System.Array]) { $global:BPAConnectionInfo += $tempConnectionInfo } else { $global:BPAConnectionInfo = @($global:BPAConnectionInfo, $tempConnectionInfo) } # Test authentication against BPA try { $result = Invoke-BPARestMethod -Resource 'users/authenticate' -RestMethod Get -BPAServer $server # Only save credential if authentication is successful if ($SaveCredential.ToBool()) { New-BPACredentialStoreItem -BPAServer $server -Credential $Credential -File $CredentialStoreFilePath } } catch { if ($result -ine "Success") { Disconnect-BPAServer -BPAServer $server throw "Failed to authenticate as $($Credential.Username)!" if ($SaveCredential.ToBool()) { Write-Warning "Credentials will not be stored!" } } } } } |