Scripts/Invoke-BPARestMethod.ps1
function Invoke-BPARestMethod { <# .SYNOPSIS Invokes a command against the AutoMate BPA management API. .DESCRIPTION Invoke-BPARestMethod makes calls against the REST API and returns results. .PARAMETER Resource The REST resource to call. .PARAMETER RestMethod The REST method to use. .PARAMETER FilterScript Applies the specified filter to the results from the API prior to property addition and date conversion to improve performance. When passing dates in the filter, they must be in UTC. .PARAMETER BPAServer The AutoMate BPA management server. .EXAMPLE # Call the API to get server information Invoke-BPARestMethod -Resource "info/get" -RestMethod Get .NOTES Author(s): : David Seibel Contributor(s) : Date Created : 08/24/2016 Date Modified : 04/09/2018 .LINK http://cloud.networkautomation.com/installs/AutoMate/v10/10.5.0.56/BPA_RESTful_API.html #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [string]$Resource, [ValidateSet("Get","Post","Put","Delete")] [string]$RestMethod = "Get", [ValidateNotNullOrEmpty()] [string]$Body = "", [ScriptBlock]$FilterScript, [string]$BPAServer ) $ProgressPreference = "SilentlyContinue" if (-not $BPAConnectionInfo) { throw "No servers are currently connected!" } if (($null -eq $BPAServer) -and ($RestMethod -ne "Get")) { throw "BPAServer must be specified when performing updates!" } if ($BPAServer) { $thisConnectionInfo = $BPAConnectionInfo | Where-Object {$_.Server -ieq $BPAServer} } else { $thisConnectionInfo = $BPAConnectionInfo } if (-not $thisConnectionInfo) { throw "Invalid server '$BPAServer' specified!" } foreach ($connection in $thisConnectionInfo) { $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $connection.Credential.UserName,$connection.Credential.GetNetworkCredential().Password))) $splat = @{ Method = $RestMethod Uri = "http://$($connection.Server):$($connection.Port)/BPAManagement/$Resource" Headers = @{Authorization=("Basic {0}" -f $base64AuthInfo)} UseBasicParsing = $true } if ($Body) { Write-Debug $Body $splat += @{ Body = $Body ContentType = "application/json" } } $request = Invoke-WebRequest @splat $content = $request.Content if ($null -ne $content) { $content = $content.Replace('"__type":', '"___type":') $tempResult = $content | ConvertFrom-Json } if ($tempResult.Result -ine "success") { throw "$($tempResult.Result) : $($tempResult.Info)" } if ($PSBoundParameters.ContainsKey("FilterScript")) { $items = $tempResult.Data | Where-Object -FilterScript $FilterScript } else { $items = $tempResult.Data } foreach ($item in $items) { if ($null -ne $item) { # Add a property to indicate the type of object being returned if ($null -ne ($item | Get-Member -Name "Type")) { $item.PSObject.TypeNames.Insert(0,"BPAConstructType.$(($item.Type -as [BPAConstructType]).ToString())") $item | Add-Member -MemberType NoteProperty -Name "TypeName" -Value ($item.Type -as [BPAConstructType]).ToString() if ($item.TypeName -eq "Condition") { $item | Add-Member -MemberType NoteProperty -Name "TriggerTypeName" -Value ($item.TriggerType -as [BPATriggerType]).ToString() } if ($null -ne ($item | Get-Member -Name "AgentType")) { $item | Add-Member -MemberType NoteProperty -Name "AgentTypeName" -Value ($item.AgentType -as [BPAAgentType]).ToString() } } $item | Add-Member -MemberType NoteProperty -Name "BPAServer" -Value $connection.Server # Convert UTC dates to local time foreach ($property in $item | Get-Member -MemberType NoteProperty) { if ($item."$($property.Name)" -is [DateTime]) { $item."$($property.Name)" = $item."$($property.Name)".ToLocalTime() } } $item } } } } |