mirror of
https://github.com/benclaussen/NetboxPS.git
synced 2025-12-13 18:02:29 +00:00
v1.3.3
v1.3.3
This commit is contained in:
commit
1f89363cbd
10 changed files with 248 additions and 78 deletions
|
|
@ -49,21 +49,12 @@ function BuildNewURI {
|
|||
[OutputType([System.UriBuilder])]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$Hostname,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string[]]$Segments,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[hashtable]$Parameters,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[boolean]$HTTPS = $true,
|
||||
|
||||
[ValidateRange(1, 65535)]
|
||||
[uint16]$Port = 443,
|
||||
|
||||
[switch]$SkipConnectedCheck
|
||||
)
|
||||
|
||||
|
|
@ -74,28 +65,8 @@ function BuildNewURI {
|
|||
$null = CheckNetboxIsConnected
|
||||
}
|
||||
|
||||
if (-not $Hostname) {
|
||||
$Hostname = Get-NetboxHostname
|
||||
}
|
||||
|
||||
if ($HTTPS) {
|
||||
Write-Verbose " Setting scheme to HTTPS"
|
||||
$Scheme = 'https'
|
||||
} else {
|
||||
Write-Warning " Connecting via non-secure HTTP is not-recommended"
|
||||
|
||||
Write-Verbose " Setting scheme to HTTP"
|
||||
$Scheme = 'http'
|
||||
|
||||
if (-not $PSBoundParameters.ContainsKey('Port')) {
|
||||
# Set the port to 80 if the user did not supply it
|
||||
Write-Verbose " Setting port to 80 as default because it was not supplied by the user"
|
||||
$Port = 80
|
||||
}
|
||||
}
|
||||
|
||||
# Begin a URI builder with HTTP/HTTPS and the provided hostname
|
||||
$uriBuilder = [System.UriBuilder]::new($Scheme, $Hostname, $Port)
|
||||
$uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort)
|
||||
|
||||
# Generate the path by trimming excess slashes and whitespace from the $segments[] and joining together
|
||||
$uriBuilder.Path = "api/{0}/" -f ($Segments.ForEach({
|
||||
|
|
|
|||
|
|
@ -4,13 +4,22 @@
|
|||
Connects to the Netbox API and ensures Credential work properly
|
||||
|
||||
.DESCRIPTION
|
||||
A detailed description of the Connect-NetboxAPI function.
|
||||
Connects to the Netbox API and ensures Credential work properly
|
||||
|
||||
.PARAMETER Hostname
|
||||
A description of the Hostname parameter.
|
||||
The hostname for the resource such as netbox.domain.com
|
||||
|
||||
.PARAMETER Credential
|
||||
A description of the Credential parameter.
|
||||
Credential object containing the API key in the password. Username is not applicable
|
||||
|
||||
.PARAMETER Scheme
|
||||
Scheme for the URI such as HTTP or HTTPS. Defaults to HTTPS
|
||||
|
||||
.PARAMETER Port
|
||||
Port for the resource. Value between 1-65535
|
||||
|
||||
.PARAMETER URI
|
||||
The full URI for the resource such as "https://netbox.domain.com:8443"
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\> Connect-NetboxAPI -Hostname "netbox.domain.com"
|
||||
|
|
@ -21,14 +30,26 @@
|
|||
Additional information about the function.
|
||||
#>
|
||||
|
||||
[CmdletBinding()]
|
||||
[CmdletBinding(DefaultParameterSetName = 'Manual')]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[Parameter(ParameterSetName = 'Manual',
|
||||
Mandatory = $true)]
|
||||
[string]$Hostname,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[pscredential]$Credential
|
||||
[pscredential]$Credential,
|
||||
|
||||
[Parameter(ParameterSetName = 'Manual')]
|
||||
[ValidateSet('https', 'http', IgnoreCase = $true)]
|
||||
[string]$Scheme = 'https',
|
||||
|
||||
[Parameter(ParameterSetName = 'Manual')]
|
||||
[uint16]$Port = 443,
|
||||
|
||||
[Parameter(ParameterSetName = 'URI',
|
||||
Mandatory = $true)]
|
||||
[string]$URI
|
||||
)
|
||||
|
||||
if (-not $Credential) {
|
||||
|
|
@ -42,9 +63,25 @@
|
|||
}
|
||||
}
|
||||
|
||||
$null = Set-NetboxHostName -Hostname $Hostname
|
||||
$null = Set-NetboxCredential -Credential $Credential
|
||||
|
||||
switch ($PSCmdlet.ParameterSetName) {
|
||||
'Manual' {
|
||||
$uriBuilder = [System.UriBuilder]::new($Scheme, $Hostname, $Port)
|
||||
}
|
||||
|
||||
'URI' {
|
||||
$uriBuilder = [System.UriBuilder]::new($URI)
|
||||
if ([string]::IsNullOrWhiteSpace($uriBuilder.Host)) {
|
||||
throw "URI appears to be invalid. Must be in format [host.name], [scheme://host.name], or [scheme://host.name:port]"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$null = Set-NetboxHostName -Hostname $uriBuilder.Host
|
||||
$null = Set-NetboxHostScheme -Scheme $uriBuilder.Scheme
|
||||
$null = Set-NetboxHostPort -Port $uriBuilder.Port
|
||||
|
||||
try {
|
||||
Write-Verbose "Verifying API connectivity..."
|
||||
$null = VerifyAPIConnectivity
|
||||
|
|
|
|||
11
Functions/Setup/Get-NetboxHostPort.ps1
Normal file
11
Functions/Setup/Get-NetboxHostPort.ps1
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function Get-NetboxHostPort {
|
||||
[CmdletBinding()]
|
||||
param ()
|
||||
|
||||
Write-Verbose "Getting Netbox host port"
|
||||
if ($null -eq $script:NetboxConfig.HostPort) {
|
||||
throw "Netbox host port is not set! You may set it with Set-NetboxHostPort -Port 'https'"
|
||||
}
|
||||
|
||||
$script:NetboxConfig.HostPort
|
||||
}
|
||||
11
Functions/Setup/Get-NetboxHostScheme.ps1
Normal file
11
Functions/Setup/Get-NetboxHostScheme.ps1
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
function Get-NetboxHostScheme {
|
||||
[CmdletBinding()]
|
||||
param ()
|
||||
|
||||
Write-Verbose "Getting Netbox host scheme"
|
||||
if ($null -eq $script:NetboxConfig.Hostscheme) {
|
||||
throw "Netbox host sceme is not set! You may set it with Set-NetboxHostScheme -Scheme 'https'"
|
||||
}
|
||||
|
||||
$script:NetboxConfig.HostScheme
|
||||
}
|
||||
15
Functions/Setup/Set-NetboxHostPort.ps1
Normal file
15
Functions/Setup/Set-NetboxHostPort.ps1
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
function Set-NetboxHostPort {
|
||||
[CmdletBinding(ConfirmImpact = 'Low',
|
||||
SupportsShouldProcess = $true)]
|
||||
[OutputType([string])]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[uint16]$Port
|
||||
)
|
||||
|
||||
if ($PSCmdlet.ShouldProcess('Netbox Port', 'Set')) {
|
||||
$script:NetboxConfig.HostPort = $Port
|
||||
$script:NetboxConfig.HostPort
|
||||
}
|
||||
}
|
||||
20
Functions/Setup/Set-NetboxHostScheme.ps1
Normal file
20
Functions/Setup/Set-NetboxHostScheme.ps1
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
function Set-NetboxHostScheme {
|
||||
[CmdletBinding(ConfirmImpact = 'Low',
|
||||
SupportsShouldProcess = $true)]
|
||||
[OutputType([string])]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $false)]
|
||||
[ValidateSet('https', 'http', IgnoreCase = $true)]
|
||||
[string]$Scheme = 'https'
|
||||
)
|
||||
|
||||
if ($PSCmdlet.ShouldProcess('Netbox Host Scheme', 'Set')) {
|
||||
if ($Scheme -eq 'http') {
|
||||
Write-Warning "Connecting via non-secure HTTP is not-recommended"
|
||||
}
|
||||
|
||||
$script:NetboxConfig.HostScheme = $Scheme
|
||||
$script:NetboxConfig.HostScheme
|
||||
}
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
#
|
||||
# Generated by: Ben Claussen
|
||||
#
|
||||
# Generated on: 2021-03-30
|
||||
# Generated on: 2021-03-31
|
||||
#
|
||||
|
||||
@{
|
||||
|
|
@ -12,7 +12,7 @@
|
|||
RootModule = 'NetboxPS.psm1'
|
||||
|
||||
# Version number of this module.
|
||||
ModuleVersion = '1.3.2'
|
||||
ModuleVersion = '1.3.3'
|
||||
|
||||
# Supported PSEditions
|
||||
# CompatiblePSEditions = @()
|
||||
|
|
|
|||
|
|
@ -104,6 +104,10 @@
|
|||
<File Build="0" Shared="True" ReferenceFunction="Invoke-Get-NetboxCircuitType_ps1" ExportFunctions="True">Functions\Circuits\Types\Get-NetboxCircuitType.ps1</File>
|
||||
<File Build="0" Shared="True" ReferenceFunction="Invoke-Get-ModelDefinition_ps1" ExportFunctions="True">Functions\Helpers\Get-ModelDefinition.ps1</File>
|
||||
<File Build="0" Shared="True" ReferenceFunction="Invoke-Set-NetboxIPAMPrefix_ps1" ExportFunctions="True">Functions\IPAM\Prefix\Set-NetboxIPAMPrefix.ps1</File>
|
||||
<File Build="0" Shared="True" ReferenceFunction="Invoke-Set-NetboxHostScheme_ps1" ExportFunctions="True">Functions\Setup\Set-NetboxHostScheme.ps1</File>
|
||||
<File Build="0" Shared="True" ReferenceFunction="Invoke-Get-NetboxHostScheme_ps1" ExportFunctions="True">Functions\Setup\Get-NetboxHostScheme.ps1</File>
|
||||
<File Build="0" Shared="True" ReferenceFunction="Invoke-Set-NetboxHostPort_ps1" ExportFunctions="True">Functions\Setup\Set-NetboxHostPort.ps1</File>
|
||||
<File Build="0" Shared="True" ReferenceFunction="Invoke-Get-NetboxHostPort_ps1" ExportFunctions="True">Functions\Setup\Get-NetboxHostPort.ps1</File>
|
||||
</Files>
|
||||
<StartupScript>R:\Netbox\NetboxPS\Test-Module.ps1</StartupScript>
|
||||
</Project>
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
#
|
||||
# Generated by: Ben Claussen
|
||||
#
|
||||
# Generated on: 2021-03-30
|
||||
# Generated on: 2021-03-31
|
||||
#
|
||||
|
||||
@{
|
||||
|
|
@ -12,7 +12,7 @@
|
|||
RootModule = 'NetboxPS.psm1'
|
||||
|
||||
# Version number of this module.
|
||||
ModuleVersion = '1.3.2'
|
||||
ModuleVersion = '1.3.3'
|
||||
|
||||
# Supported PSEditions
|
||||
# CompatiblePSEditions = @()
|
||||
|
|
|
|||
|
|
@ -264,21 +264,12 @@ function BuildNewURI {
|
|||
[OutputType([System.UriBuilder])]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string]$Hostname,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[string[]]$Segments,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[hashtable]$Parameters,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[boolean]$HTTPS = $true,
|
||||
|
||||
[ValidateRange(1, 65535)]
|
||||
[uint16]$Port = 443,
|
||||
|
||||
[switch]$SkipConnectedCheck
|
||||
)
|
||||
|
||||
|
|
@ -289,28 +280,8 @@ function BuildNewURI {
|
|||
$null = CheckNetboxIsConnected
|
||||
}
|
||||
|
||||
if (-not $Hostname) {
|
||||
$Hostname = Get-NetboxHostname
|
||||
}
|
||||
|
||||
if ($HTTPS) {
|
||||
Write-Verbose " Setting scheme to HTTPS"
|
||||
$Scheme = 'https'
|
||||
} else {
|
||||
Write-Warning " Connecting via non-secure HTTP is not-recommended"
|
||||
|
||||
Write-Verbose " Setting scheme to HTTP"
|
||||
$Scheme = 'http'
|
||||
|
||||
if (-not $PSBoundParameters.ContainsKey('Port')) {
|
||||
# Set the port to 80 if the user did not supply it
|
||||
Write-Verbose " Setting port to 80 as default because it was not supplied by the user"
|
||||
$Port = 80
|
||||
}
|
||||
}
|
||||
|
||||
# Begin a URI builder with HTTP/HTTPS and the provided hostname
|
||||
$uriBuilder = [System.UriBuilder]::new($Scheme, $Hostname, $Port)
|
||||
$uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort)
|
||||
|
||||
# Generate the path by trimming excess slashes and whitespace from the $segments[] and joining together
|
||||
$uriBuilder.Path = "api/{0}/" -f ($Segments.ForEach({
|
||||
|
|
@ -476,6 +447,22 @@ function CheckNetboxIsConnected {
|
|||
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region File Clear-NetboxCredential.ps1
|
||||
|
||||
function Clear-NetboxCredential {
|
||||
[CmdletBinding(ConfirmImpact = 'Medium', SupportsShouldProcess = $true)]
|
||||
param
|
||||
(
|
||||
[switch]$Force
|
||||
)
|
||||
|
||||
if ($Force -or ($PSCmdlet.ShouldProcess('Netbox Credentials', 'Clear'))) {
|
||||
$script:NetboxConfig.Credential = $null
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region File Connect-NetboxAPI.ps1
|
||||
|
|
@ -486,13 +473,22 @@ function Connect-NetboxAPI {
|
|||
Connects to the Netbox API and ensures Credential work properly
|
||||
|
||||
.DESCRIPTION
|
||||
A detailed description of the Connect-NetboxAPI function.
|
||||
Connects to the Netbox API and ensures Credential work properly
|
||||
|
||||
.PARAMETER Hostname
|
||||
A description of the Hostname parameter.
|
||||
The hostname for the resource such as netbox.domain.com
|
||||
|
||||
.PARAMETER Credential
|
||||
A description of the Credential parameter.
|
||||
Credential object containing the API key in the password. Username is not applicable
|
||||
|
||||
.PARAMETER Scheme
|
||||
Scheme for the URI such as HTTP or HTTPS. Defaults to HTTPS
|
||||
|
||||
.PARAMETER Port
|
||||
Port for the resource. Value between 1-65535
|
||||
|
||||
.PARAMETER URI
|
||||
The full URI for the resource such as "https://netbox.domain.com:8443"
|
||||
|
||||
.EXAMPLE
|
||||
PS C:\> Connect-NetboxAPI -Hostname "netbox.domain.com"
|
||||
|
|
@ -503,14 +499,26 @@ function Connect-NetboxAPI {
|
|||
Additional information about the function.
|
||||
#>
|
||||
|
||||
[CmdletBinding()]
|
||||
[CmdletBinding(DefaultParameterSetName = 'Manual')]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[Parameter(ParameterSetName = 'Manual',
|
||||
Mandatory = $true)]
|
||||
[string]$Hostname,
|
||||
|
||||
[Parameter(Mandatory = $false)]
|
||||
[pscredential]$Credential
|
||||
[pscredential]$Credential,
|
||||
|
||||
[Parameter(ParameterSetName = 'Manual')]
|
||||
[ValidateSet('https', 'http', IgnoreCase = $true)]
|
||||
[string]$Scheme = 'https',
|
||||
|
||||
[Parameter(ParameterSetName = 'Manual')]
|
||||
[uint16]$Port = 443,
|
||||
|
||||
[Parameter(ParameterSetName = 'URI',
|
||||
Mandatory = $true)]
|
||||
[string]$URI
|
||||
)
|
||||
|
||||
if (-not $Credential) {
|
||||
|
|
@ -524,9 +532,25 @@ function Connect-NetboxAPI {
|
|||
}
|
||||
}
|
||||
|
||||
$null = Set-NetboxHostName -Hostname $Hostname
|
||||
$null = Set-NetboxCredential -Credential $Credential
|
||||
|
||||
switch ($PSCmdlet.ParameterSetName) {
|
||||
'Manual' {
|
||||
$uriBuilder = [System.UriBuilder]::new($Scheme, $Hostname, $Port)
|
||||
}
|
||||
|
||||
'URI' {
|
||||
$uriBuilder = [System.UriBuilder]::new($URI)
|
||||
if ([string]::IsNullOrWhiteSpace($uriBuilder.Host)) {
|
||||
throw "URI appears to be invalid. Must be in format [host.name], [scheme://host.name], or [scheme://host.name:port]"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$null = Set-NetboxHostName -Hostname $uriBuilder.Host
|
||||
$null = Set-NetboxHostScheme -Scheme $uriBuilder.Scheme
|
||||
$null = Set-NetboxHostPort -Port $uriBuilder.Port
|
||||
|
||||
try {
|
||||
Write-Verbose "Verifying API connectivity..."
|
||||
$null = VerifyAPIConnectivity
|
||||
|
|
@ -1689,6 +1713,38 @@ function Get-NetboxHostname {
|
|||
|
||||
#endregion
|
||||
|
||||
#region File Get-NetboxHostPort.ps1
|
||||
|
||||
function Get-NetboxHostPort {
|
||||
[CmdletBinding()]
|
||||
param ()
|
||||
|
||||
Write-Verbose "Getting Netbox host port"
|
||||
if ($null -eq $script:NetboxConfig.HostPort) {
|
||||
throw "Netbox host port is not set! You may set it with Set-NetboxHostPort -Port 'https'"
|
||||
}
|
||||
|
||||
$script:NetboxConfig.HostPort
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region File Get-NetboxHostScheme.ps1
|
||||
|
||||
function Get-NetboxHostScheme {
|
||||
[CmdletBinding()]
|
||||
param ()
|
||||
|
||||
Write-Verbose "Getting Netbox host scheme"
|
||||
if ($null -eq $script:NetboxConfig.Hostscheme) {
|
||||
throw "Netbox host sceme is not set! You may set it with Set-NetboxHostScheme -Scheme 'https'"
|
||||
}
|
||||
|
||||
$script:NetboxConfig.HostScheme
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region File Get-NetboxIPAMAddress.ps1
|
||||
|
||||
function Get-NetboxIPAMAddress {
|
||||
|
|
@ -4237,6 +4293,51 @@ function Set-NetboxHostName {
|
|||
|
||||
#endregion
|
||||
|
||||
#region File Set-NetboxHostPort.ps1
|
||||
|
||||
function Set-NetboxHostPort {
|
||||
[CmdletBinding(ConfirmImpact = 'Low',
|
||||
SupportsShouldProcess = $true)]
|
||||
[OutputType([string])]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[uint16]$Port
|
||||
)
|
||||
|
||||
if ($PSCmdlet.ShouldProcess('Netbox Port', 'Set')) {
|
||||
$script:NetboxConfig.HostPort = $Port
|
||||
$script:NetboxConfig.HostPort
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region File Set-NetboxHostScheme.ps1
|
||||
|
||||
function Set-NetboxHostScheme {
|
||||
[CmdletBinding(ConfirmImpact = 'Low',
|
||||
SupportsShouldProcess = $true)]
|
||||
[OutputType([string])]
|
||||
param
|
||||
(
|
||||
[Parameter(Mandatory = $false)]
|
||||
[ValidateSet('https', 'http', IgnoreCase = $true)]
|
||||
[string]$Scheme = 'https'
|
||||
)
|
||||
|
||||
if ($PSCmdlet.ShouldProcess('Netbox Host Scheme', 'Set')) {
|
||||
if ($Scheme -eq 'http') {
|
||||
Write-Warning "Connecting via non-secure HTTP is not-recommended"
|
||||
}
|
||||
|
||||
$script:NetboxConfig.HostScheme = $Scheme
|
||||
$script:NetboxConfig.HostScheme
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region File Set-NetboxIPAMAddress.ps1
|
||||
|
||||
<#
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue