NetboxPS/Functions/Helpers/BuildNewURI.ps1

93 lines
2.6 KiB
PowerShell
Raw Normal View History

<#
2020-04-09 09:57:20 -04:00
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2020 v5.7.172
Created on: 3/26/2020 14:22
Created by: Claussen
Organization: NEOnet
Filename: BuildNewURI.ps1
===========================================================================
.DESCRIPTION
A description of the file.
#>
function BuildNewURI {
<#
.SYNOPSIS
Create a new URI for Netbox
2020-04-09 09:57:20 -04:00
.DESCRIPTION
Internal function used to build a URIBuilder object.
2020-04-09 09:57:20 -04:00
.PARAMETER Hostname
Hostname of the Netbox API
2020-04-09 09:57:20 -04:00
.PARAMETER Segments
Array of strings for each segment in the URL path
2020-04-09 09:57:20 -04:00
.PARAMETER Parameters
Hashtable of query parameters to include
2020-04-09 09:57:20 -04:00
.PARAMETER HTTPS
Whether to use HTTPS or HTTP
2020-04-09 09:57:20 -04:00
.PARAMETER Port
A description of the Port parameter.
2020-04-09 09:57:20 -04:00
.PARAMETER APIInfo
A description of the APIInfo parameter.
2020-04-09 09:57:20 -04:00
.EXAMPLE
PS C:\> BuildNewURI
2020-04-09 09:57:20 -04:00
.NOTES
Additional information about the function.
#>
2020-04-09 09:57:20 -04:00
[CmdletBinding()]
[OutputType([System.UriBuilder])]
param
(
[Parameter(Mandatory = $false)]
[string[]]$Segments,
2020-04-09 09:57:20 -04:00
[Parameter(Mandatory = $false)]
[hashtable]$Parameters,
2020-04-09 09:57:20 -04:00
[switch]$SkipConnectedCheck
)
2020-04-09 09:57:20 -04:00
Write-Verbose "Building URI"
2020-04-09 09:57:20 -04:00
if (-not $SkipConnectedCheck) {
# There is no point in continuing if we have not successfully connected to an API
$null = CheckNetboxIsConnected
}
2020-04-09 09:57:20 -04:00
# Begin a URI builder with HTTP/HTTPS and the provided hostname
$uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort)
2020-04-09 09:57:20 -04:00
# Generate the path by trimming excess slashes and whitespace from the $segments[] and joining together
$uriBuilder.Path = "api/{0}/" -f ($Segments.ForEach({
$_.trim('/').trim()
}) -join '/')
2020-04-09 09:57:20 -04:00
Write-Verbose " URIPath: $($uriBuilder.Path)"
2020-04-09 09:57:20 -04:00
if ($parameters) {
# Loop through the parameters and use the HttpUtility to create a Query string
[System.Collections.Specialized.NameValueCollection]$URIParams = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
2020-04-09 09:57:20 -04:00
foreach ($param in $Parameters.GetEnumerator()) {
Write-Verbose " Adding URI parameter $($param.Key):$($param.Value)"
$URIParams[$param.Key] = $param.Value
}
2020-04-09 09:57:20 -04:00
$uriBuilder.Query = $URIParams.ToString()
}
2020-04-09 09:57:20 -04:00
Write-Verbose " Completed building URIBuilder"
# Return the entire UriBuilder object
$uriBuilder
}