├── Tests ├── .gitignore ├── VirtualizationChoices.json ├── credential.example.ps1 ├── common.ps1 ├── IPAMChoices.json ├── integration │ └── DCIM.Site.Tests.ps1 ├── Setup.Tests.ps1 ├── DCIMChoices.json └── DCIM.Platforms.Tests.ps1 ├── _config.yml ├── .vscode ├── PSScriptAnalyzerSettings.psd1 └── settings.json ├── Functions ├── Setup │ ├── Support │ │ ├── GetNetboxConfigVariable.ps1 │ │ ├── Test-NetboxAPIConnected.ps1 │ │ ├── VerifyAPIConnectivity.ps1 │ │ ├── SetupNetboxConfigVariable.ps1 │ │ ├── Get-NetboxAPIDefinition.ps1 │ │ └── Get-NetboxContentType.ps1 │ ├── Get-NetboxCredential.ps1 │ ├── Clear-NetboxCredential.ps1 │ ├── Get-NetboxHostPort.ps1 │ ├── Get-NetboxHostScheme.ps1 │ ├── Get-NetboxHostname.ps1 │ ├── Get-NetboxInvokeParams.ps1 │ ├── Get-NetboxTimeout.ps1 │ ├── Set-NetboxCipherSSL.ps1 │ ├── Set-NetboxHostPort.ps1 │ ├── Set-NetboxHostName.ps1 │ ├── Set-NetboxInvokeParams.ps1 │ ├── Get-NetboxVersion.ps1 │ ├── Set-NetboxTimeout.ps1 │ ├── Set-NetboxHostScheme.ps1 │ ├── Set-NetboxUnstrustedSSL.ps1 │ ├── Set-NetboxCredential.ps1 │ └── Connect-NetboxAPI.ps1 ├── Helpers │ ├── CheckNetboxIsConnected.ps1 │ ├── ThrowNetboxRESTError.ps1 │ ├── GetNetboxAPIErrorBody.ps1 │ ├── CreateEnum.ps1 │ ├── Get-ModelDefinition.ps1 │ ├── BuildNewURI.ps1 │ ├── BuildURIComponents.ps1 │ └── InvokeNetboxRequest.ps1 ├── Extras │ └── Tags │ │ └── Get-NetboxTag.ps1 ├── Virtualization │ ├── VirtualizationCluster │ │ ├── Get-NetboxVirtualizationClusterGroup.ps1 │ │ └── Get-NetboxVirtualizationCluster.ps1 │ ├── VirtualMachineInterface │ │ ├── Add-NetboxVirtualMachineInterface.ps1 │ │ ├── Set-NetboxVirtualMachineInterface.ps1 │ │ └── Get-NetboxVirtualMachineInterface.ps1 │ └── VirtualMachine │ │ ├── Remove-NetboxVirtualMachine.ps1 │ │ ├── Set-NetboxVirtualMachine.ps1 │ │ ├── New-NetboxVirtualMachine.ps1 │ │ └── Get-NetboxVirtualMachine.ps1 ├── DCIM │ ├── Interfaces │ │ ├── Get-NetboxDCIMInterfaceConnection.ps1 │ │ ├── Remove-NetboxDCIMInterfaceConnection.ps1 │ │ ├── Remove-NetboxDCIMInterface.ps1 │ │ ├── Add-NetboxDCIMInterfaceConnection.ps1 │ │ ├── Set-NetboxDCIMInterfaceConnection.ps1 │ │ ├── Get-NetboxDCIMInterface.ps1 │ │ ├── Add-NetboxDCIMInterface.ps1 │ │ └── Set-NetboxDCIMInterface.ps1 │ ├── RearPorts │ │ ├── Get-NetboxDCIMRearPort.ps1 │ │ ├── Remove-NetboxDCIMRearPort.ps1 │ │ ├── Add-NetboxDCIMRearPort.ps1 │ │ └── Set-NetboxDCIMRearPort.ps1 │ ├── FrontPorts │ │ ├── Get-NetboxDCIMFrontPort.ps1 │ │ ├── Remove-NetboxDCIMFrontPort.ps1 │ │ ├── Add-NetboxDCIMFrontPort.ps1 │ │ └── Set-NetboxDCIMFrontPort.ps1 │ ├── Cable Terminations │ │ └── Get-NetboxDCIMCableTermination.ps1 │ ├── Devices │ │ ├── Get-NetboxDCIMDeviceType.ps1 │ │ ├── Remove-NetboxDCIMDevice.ps1 │ │ ├── Get-NetboxDCIMDeviceRole.ps1 │ │ ├── New-NetboxDCIMDevice.ps1 │ │ ├── Get-NetboxDCIMDevice.ps1 │ │ └── Set-NetboxDCIMDevice.ps1 │ ├── Cables │ │ └── Get-NetboxDCIMCable.ps1 │ ├── Get-NetboxDCIMPlatform.ps1 │ └── Sites │ │ ├── Remove-NetboxDCIMSite.ps1 │ │ ├── New-NetboxDCIMSite.ps1 │ │ └── Get-NetboxDCIMSite.ps1 ├── IPAM │ ├── Address │ │ ├── Remove-NetboxIPAMAddress.ps1 │ │ ├── Get-NetboxIPAMAvailableIP.ps1 │ │ ├── Get-NetboxIPAMAddress.ps1 │ │ ├── New-NetboxIPAMAddress.ps1 │ │ └── Set-NetboxIPAMAddress.ps1 │ ├── Range │ │ ├── Remove-NetboxIPAMAddressRange.ps1 │ │ ├── Set-NetboxIPAMAddressRange.ps1 │ │ ├── Get-NetboxIPAMAddressRange.ps1 │ │ └── New-NetboxIPAMAddressRange.ps1 │ ├── Prefix │ │ ├── New-NetboxIPAMPrefix.ps1 │ │ ├── Set-NetboxIPAMPrefix.ps1 │ │ └── Get-NetboxIPAMPrefix.ps1 │ ├── Aggregate │ │ └── Get-NetboxIPAMAggregate.ps1 │ ├── VLAN │ │ ├── New-NetboxIPAMVLAN.ps1 │ │ └── Get-NetboxIPAMVLAN.ps1 │ └── Role │ │ └── Get-NetboxIPAMRole.ps1 ├── Circuits │ ├── Circuits │ │ ├── New-NetboxCircuit.ps1 │ │ └── Get-NetboxCircuit.ps1 │ ├── Types │ │ └── Get-NetboxCircuitType.ps1 │ ├── Providers │ │ └── Get-NetboxCircuitProvider.ps1 │ └── Terminations │ │ └── Get-NetboxCircuitTermination.ps1 └── Tenancy │ ├── Tenants │ ├── New-NetboxTenant.ps1 │ └── Get-NetboxTenant.ps1 │ ├── ContactRoles │ ├── New-NetboxContactRole.ps1 │ ├── Set-NetboxContactRole.ps1 │ └── Get-NetboxContactRole.ps1 │ ├── ContactAssignment │ ├── New-NetboxContactAssignment.ps1 │ ├── Set-NetboxContactAssignment.ps1 │ └── Get-NetboxContactAssignment.ps1 │ └── Contacts │ ├── New-NetboxContact.ps1 │ ├── Set-NetboxContact.ps1 │ └── Get-NetboxContact.ps1 ├── .gitignore ├── .github └── workflows │ ├── release.yml │ └── pssa.yml ├── NetboxPS.psm1 ├── LICENSE ├── NetboxPS.psproj.bak ├── README.md └── deploy.ps1 /Tests/.gitignore: -------------------------------------------------------------------------------- 1 | credential.ps1 -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-slate -------------------------------------------------------------------------------- /.vscode/PSScriptAnalyzerSettings.psd1: -------------------------------------------------------------------------------- 1 | @{ 2 | ExcludeRules = @( 3 | 'PSUseToExportFieldsInManifest' 4 | ) 5 | } -------------------------------------------------------------------------------- /Functions/Setup/Support/GetNetboxConfigVariable.ps1: -------------------------------------------------------------------------------- 1 | function GetNetboxConfigVariable { 2 | return $script:NetboxConfig 3 | } -------------------------------------------------------------------------------- /Tests/VirtualizationChoices.json: -------------------------------------------------------------------------------- 1 | {"virtual-machine:status":[{"label":"Active","value":1},{"label":"Offline","value":0},{"label":"Staged","value":3}]} -------------------------------------------------------------------------------- /Functions/Setup/Support/Test-NetboxAPIConnected.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Test-NetboxAPIConnected { 3 | [CmdletBinding()] 4 | param () 5 | 6 | $script:NetboxConfig.Connected 7 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.TempPoint.* 2 | *.Export.* 3 | *.psprojs 4 | *.psbuild 5 | *.RestorePoint.* 6 | *.Run.ps1 7 | *.Package.ps1 8 | CustomMenu.inf 9 | Test-Module.ps1 10 | Staging/ 11 | concatenated.ps1 -------------------------------------------------------------------------------- /Functions/Helpers/CheckNetboxIsConnected.ps1: -------------------------------------------------------------------------------- 1 | 2 | function CheckNetboxIsConnected { 3 | [CmdletBinding()] 4 | param () 5 | 6 | Write-Verbose "Checking connection status" 7 | if (-not $script:NetboxConfig.Connected) { 8 | throw "Not connected to a Netbox API! Please run 'Connect-NetboxAPI'" 9 | } 10 | } -------------------------------------------------------------------------------- /Functions/Helpers/ThrowNetboxRESTError.ps1: -------------------------------------------------------------------------------- 1 | 2 | function ThrowNetboxRESTError { 3 | $uriSegments = [System.Collections.ArrayList]::new(@('fake', 'url')) 4 | 5 | $URIParameters = @{ 6 | } 7 | 8 | $uri = BuildNewURI -Segments $uriSegments -Parameters $URIParameters 9 | 10 | InvokeNetboxRequest -URI $uri -Raw 11 | } -------------------------------------------------------------------------------- /Functions/Setup/Get-NetboxCredential.ps1: -------------------------------------------------------------------------------- 1 | function Get-NetboxCredential { 2 | [CmdletBinding()] 3 | [OutputType([pscredential])] 4 | param () 5 | 6 | if (-not $script:NetboxConfig.Credential) { 7 | throw "Netbox Credentials not set! You may set with Set-NetboxCredential" 8 | } 9 | 10 | $script:NetboxConfig.Credential 11 | } -------------------------------------------------------------------------------- /Functions/Setup/Support/VerifyAPIConnectivity.ps1: -------------------------------------------------------------------------------- 1 | function VerifyAPIConnectivity { 2 | [CmdletBinding()] 3 | param () 4 | 5 | $uriSegments = [System.Collections.ArrayList]::new(@('extras')) 6 | 7 | $uri = BuildNewURI -Segments $uriSegments -Parameters @{'format' = 'json' } -SkipConnectedCheck 8 | 9 | InvokeNetboxRequest -URI $uri 10 | } -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | release: 5 | types: [published] 6 | 7 | jobs: 8 | build: 9 | 10 | runs-on: ubuntu-latest 11 | 12 | steps: 13 | - uses: actions/checkout@v1 14 | - name: Publish 15 | run: | 16 | pwsh -Command "Publish-Module -Path ./NetboxPS -NuGetApiKey ${{ secrets.PSGALLERY_API_KEY }}" 17 | -------------------------------------------------------------------------------- /Functions/Setup/Clear-NetboxCredential.ps1: -------------------------------------------------------------------------------- 1 | function Clear-NetboxCredential { 2 | [CmdletBinding(ConfirmImpact = 'Medium', SupportsShouldProcess = $true)] 3 | param 4 | ( 5 | [switch]$Force 6 | ) 7 | 8 | if ($Force -or ($PSCmdlet.ShouldProcess('Netbox Credentials', 'Clear'))) { 9 | $script:NetboxConfig.Credential = $null 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Functions/Setup/Get-NetboxHostPort.ps1: -------------------------------------------------------------------------------- 1 | function Get-NetboxHostPort { 2 | [CmdletBinding()] 3 | param () 4 | 5 | Write-Verbose "Getting Netbox host port" 6 | if ($null -eq $script:NetboxConfig.HostPort) { 7 | throw "Netbox host port is not set! You may set it with Set-NetboxHostPort -Port 'https'" 8 | } 9 | 10 | $script:NetboxConfig.HostPort 11 | } -------------------------------------------------------------------------------- /Functions/Setup/Get-NetboxHostScheme.ps1: -------------------------------------------------------------------------------- 1 | function Get-NetboxHostScheme { 2 | [CmdletBinding()] 3 | param () 4 | 5 | Write-Verbose "Getting Netbox host scheme" 6 | if ($null -eq $script:NetboxConfig.Hostscheme) { 7 | throw "Netbox host sceme is not set! You may set it with Set-NetboxHostScheme -Scheme 'https'" 8 | } 9 | 10 | $script:NetboxConfig.HostScheme 11 | } -------------------------------------------------------------------------------- /Functions/Setup/Get-NetboxHostname.ps1: -------------------------------------------------------------------------------- 1 | function Get-NetboxHostname { 2 | [CmdletBinding()] 3 | param () 4 | 5 | Write-Verbose "Getting Netbox hostname" 6 | if ($null -eq $script:NetboxConfig.Hostname) { 7 | throw "Netbox Hostname is not set! You may set it with Set-NetboxHostname -Hostname 'hostname.domain.tld'" 8 | } 9 | 10 | $script:NetboxConfig.Hostname 11 | } -------------------------------------------------------------------------------- /Functions/Setup/Get-NetboxInvokeParams.ps1: -------------------------------------------------------------------------------- 1 | function Get-NetboxInvokeParams { 2 | [CmdletBinding()] 3 | param () 4 | 5 | Write-Verbose "Getting Netbox InvokeParams" 6 | if ($null -eq $script:NetboxConfig.InvokeParams) { 7 | throw "Netbox Invoke Params is not set! You may set it with Set-NetboxInvokeParams -InvokeParams ..." 8 | } 9 | 10 | $script:NetboxConfig.InvokeParams 11 | } -------------------------------------------------------------------------------- /Functions/Setup/Get-NetboxTimeout.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxTimeout { 3 | [CmdletBinding()] 4 | [OutputType([uint16])] 5 | param () 6 | 7 | Write-Verbose "Getting Netbox Timeout" 8 | if ($null -eq $script:NetboxConfig.Timeout) { 9 | throw "Netbox Timeout is not set! You may set it with Set-NetboxTimeout -TimeoutSeconds [uint16]" 10 | } 11 | 12 | $script:NetboxConfig.Timeout 13 | } -------------------------------------------------------------------------------- /Functions/Setup/Set-NetboxCipherSSL.ps1: -------------------------------------------------------------------------------- 1 | Function Set-NetboxCipherSSL { 2 | [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessforStateChangingFunctions", "")] 3 | Param( ) 4 | # Hack for allowing TLS 1.1 and TLS 1.2 (by default it is only SSL3 and TLS (1.0)) 5 | $AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12' 6 | [System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols 7 | 8 | } -------------------------------------------------------------------------------- /Tests/credential.example.ps1: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2021, Alexis La Goutte 3 | # 4 | # SPDX-License-Identifier: Apache-2.0 5 | # 6 | 7 | # Copy this file to credential.ps1 (on Tests folder) and change connection settings.. 8 | 9 | $script:hostname = "10.44.23.213" 10 | $script:token = "aaaaaaaaaaaaaaaaaa" 11 | 12 | #Uncomment if you want to personnalize some setting 13 | #script:pester_site1 = "pester_site1" 14 | -------------------------------------------------------------------------------- /NetboxPS.psm1: -------------------------------------------------------------------------------- 1 | # Build a list of common parameters so we can omit them to build URI parameters 2 | $script:CommonParameterNames = New-Object System.Collections.ArrayList 3 | [void]$script:CommonParameterNames.AddRange(@([System.Management.Automation.PSCmdlet]::CommonParameters)) 4 | [void]$script:CommonParameterNames.AddRange(@([System.Management.Automation.PSCmdlet]::OptionalCommonParameters)) 5 | [void]$script:CommonParameterNames.Add('Raw') 6 | 7 | SetupNetboxConfigVariable 8 | -------------------------------------------------------------------------------- /Functions/Setup/Set-NetboxHostPort.ps1: -------------------------------------------------------------------------------- 1 | function Set-NetboxHostPort { 2 | [CmdletBinding(ConfirmImpact = 'Low', 3 | SupportsShouldProcess = $true)] 4 | [OutputType([string])] 5 | param 6 | ( 7 | [Parameter(Mandatory = $true)] 8 | [uint16]$Port 9 | ) 10 | 11 | if ($PSCmdlet.ShouldProcess('Netbox Port', 'Set')) { 12 | $script:NetboxConfig.HostPort = $Port 13 | $script:NetboxConfig.HostPort 14 | } 15 | } -------------------------------------------------------------------------------- /.github/workflows/pssa.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: [pull_request] 3 | jobs: 4 | lint: 5 | name: Run PSSA 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@v1 9 | - name: lint 10 | uses: docker://devblackops/github-action-psscriptanalyzer:2.3.0 11 | with: 12 | settingsPath: .vscode/PSScriptAnalyzerSettings.psd1 13 | sendComment: false 14 | failOnInfos: true 15 | failOnErrors: true 16 | failOnWarnings: true -------------------------------------------------------------------------------- /Functions/Setup/Set-NetboxHostName.ps1: -------------------------------------------------------------------------------- 1 | function Set-NetboxHostName { 2 | [CmdletBinding(ConfirmImpact = 'Low', 3 | SupportsShouldProcess = $true)] 4 | [OutputType([string])] 5 | param 6 | ( 7 | [Parameter(Mandatory = $true)] 8 | [string]$Hostname 9 | ) 10 | 11 | if ($PSCmdlet.ShouldProcess('Netbox Hostname', 'Set')) { 12 | $script:NetboxConfig.Hostname = $Hostname.Trim() 13 | $script:NetboxConfig.Hostname 14 | } 15 | } -------------------------------------------------------------------------------- /Functions/Setup/Set-NetboxInvokeParams.ps1: -------------------------------------------------------------------------------- 1 | function Set-NetboxInvokeParams { 2 | [CmdletBinding(ConfirmImpact = 'Low', 3 | SupportsShouldProcess = $true)] 4 | [OutputType([string])] 5 | param( 6 | [Parameter(Mandatory = $true)] 7 | [array]$InvokeParams 8 | ) 9 | 10 | if ($PSCmdlet.ShouldProcess('Netbox Invoke Params', 'Set')) { 11 | $script:NetboxConfig.InvokeParams = $InvokeParams 12 | $script:NetboxConfig.InvokeParams 13 | } 14 | } -------------------------------------------------------------------------------- /Functions/Setup/Get-NetboxVersion.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxVersion { 3 | [CmdletBinding()] 4 | param () 5 | 6 | $Segments = [System.Collections.ArrayList]::new(@('status')) 7 | 8 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary @{ 9 | 'format' = 'json' 10 | } 11 | 12 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters -SkipConnectedCheck 13 | 14 | InvokeNetboxRequest -URI $URI 15 | } 16 | -------------------------------------------------------------------------------- /Functions/Setup/Set-NetboxTimeout.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Set-NetboxTimeout { 3 | [CmdletBinding(ConfirmImpact = 'Low', 4 | SupportsShouldProcess = $true)] 5 | [OutputType([uint16])] 6 | param 7 | ( 8 | [Parameter(Mandatory = $false)] 9 | [ValidateRange(1, 65535)] 10 | [uint16]$TimeoutSeconds = 30 11 | ) 12 | 13 | if ($PSCmdlet.ShouldProcess('Netbox Timeout', 'Set')) { 14 | $script:NetboxConfig.Timeout = $TimeoutSeconds 15 | $script:NetboxConfig.Timeout 16 | } 17 | } -------------------------------------------------------------------------------- /Functions/Helpers/GetNetboxAPIErrorBody.ps1: -------------------------------------------------------------------------------- 1 | 2 | function GetNetboxAPIErrorBody { 3 | param 4 | ( 5 | [Parameter(Mandatory = $true)] 6 | [System.Net.HttpWebResponse]$Response 7 | ) 8 | 9 | # This takes the $Response stream and turns it into a useable object... generally a string. 10 | # If the body is JSON, you should be able to use ConvertFrom-Json 11 | 12 | $reader = New-Object System.IO.StreamReader($Response.GetResponseStream()) 13 | $reader.BaseStream.Position = 0 14 | $reader.DiscardBufferedData() 15 | $reader.ReadToEnd() 16 | } -------------------------------------------------------------------------------- /Functions/Setup/Support/SetupNetboxConfigVariable.ps1: -------------------------------------------------------------------------------- 1 | function SetupNetboxConfigVariable { 2 | [CmdletBinding()] 3 | param 4 | ( 5 | [switch]$Overwrite 6 | ) 7 | 8 | Write-Verbose "Checking for NetboxConfig hashtable" 9 | if ((-not ($script:NetboxConfig)) -or $Overwrite) { 10 | Write-Verbose "Creating NetboxConfig hashtable" 11 | $script:NetboxConfig = @{ 12 | 'Connected' = $false 13 | 'Choices' = @{ 14 | } 15 | 'APIDefinition' = $null 16 | 'ContentTypes' = $null 17 | } 18 | } 19 | 20 | Write-Verbose "NetboxConfig hashtable already exists" 21 | } -------------------------------------------------------------------------------- /Tests/common.ps1: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2021, Alexis La Goutte 3 | # 4 | # SPDX-License-Identifier: Apache-2.0 5 | # 6 | [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")] 7 | Param() 8 | 9 | $script:pester_site1 = "pester_site1" 10 | 11 | $Credential = New-Object System.Management.Automation.PSCredential("username", (ConvertTo-SecureString $token -AsPlainText -Force)) 12 | $script:invokeParams = @{ 13 | hostname = $hostname; 14 | Credential = $Credential; 15 | SkipCertificateCheck = $true; 16 | } 17 | 18 | . ../credential.ps1 19 | #TODO: Add check if no ipaddress/token info... -------------------------------------------------------------------------------- /Functions/Setup/Set-NetboxHostScheme.ps1: -------------------------------------------------------------------------------- 1 | function Set-NetboxHostScheme { 2 | [CmdletBinding(ConfirmImpact = 'Low', 3 | SupportsShouldProcess = $true)] 4 | [OutputType([string])] 5 | param 6 | ( 7 | [Parameter(Mandatory = $false)] 8 | [ValidateSet('https', 'http', IgnoreCase = $true)] 9 | [string]$Scheme = 'https' 10 | ) 11 | 12 | if ($PSCmdlet.ShouldProcess('Netbox Host Scheme', 'Set')) { 13 | if ($Scheme -eq 'http') { 14 | Write-Warning "Connecting via non-secure HTTP is not-recommended" 15 | } 16 | 17 | $script:NetboxConfig.HostScheme = $Scheme 18 | $script:NetboxConfig.HostScheme 19 | } 20 | } -------------------------------------------------------------------------------- /Functions/Setup/Support/Get-NetboxAPIDefinition.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxAPIDefinition { 3 | [CmdletBinding()] 4 | param 5 | ( 6 | [ValidateSet('json', 'yaml', IgnoreCase = $true)] 7 | [string]$Format = 'json' 8 | ) 9 | 10 | #$URI = "https://netbox.neonet.org/api/schema/?format=json" 11 | 12 | $Segments = [System.Collections.ArrayList]::new(@('schema')) 13 | 14 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary @{ 15 | 'format' = $Format.ToLower() 16 | } 17 | 18 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters -SkipConnectedCheck 19 | 20 | InvokeNetboxRequest -URI $URI 21 | } 22 | -------------------------------------------------------------------------------- /Functions/Helpers/CreateEnum.ps1: -------------------------------------------------------------------------------- 1 | 2 | function CreateEnum { 3 | [CmdletBinding()] 4 | param 5 | ( 6 | [Parameter(Mandatory = $true)] 7 | [string]$EnumName, 8 | 9 | [Parameter(Mandatory = $true)] 10 | [pscustomobject]$Values, 11 | 12 | [switch]$PassThru 13 | ) 14 | 15 | $definition = @" 16 | public enum $EnumName 17 | {`n$(foreach ($value in $values) { 18 | "`t$($value.label) = $($value.value),`n" 19 | }) 20 | } 21 | "@ 22 | if (-not ([System.Management.Automation.PSTypeName]"$EnumName").Type) { 23 | #Write-Host $definition -ForegroundColor Green 24 | Add-Type -TypeDefinition $definition -PassThru:$PassThru 25 | } else { 26 | Write-Warning "EnumType $EnumName already exists." 27 | } 28 | } -------------------------------------------------------------------------------- /Functions/Setup/Set-NetboxUnstrustedSSL.ps1: -------------------------------------------------------------------------------- 1 | Function Set-NetboxUntrustedSSL { 2 | [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessforStateChangingFunctions", "")] 3 | Param( ) 4 | # Hack for allowing untrusted SSL certs with https connections 5 | Add-Type -TypeDefinition @" 6 | using System.Net; 7 | using System.Security.Cryptography.X509Certificates; 8 | public class TrustAllCertsPolicy : ICertificatePolicy { 9 | public bool CheckValidationResult( 10 | ServicePoint srvPoint, X509Certificate certificate, 11 | WebRequest request, int certificateProblem) { 12 | return true; 13 | } 14 | } 15 | "@ 16 | 17 | [System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy 18 | 19 | } -------------------------------------------------------------------------------- /Functions/Extras/Tags/Get-NetboxTag.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxTag { 3 | [CmdletBinding()] 4 | [OutputType([pscustomobject])] 5 | param 6 | ( 7 | [Parameter(ValueFromPipelineByPropertyName = $true)] 8 | [uint64]$Id, 9 | 10 | [string]$Name, 11 | 12 | [string]$Slug, 13 | 14 | [uint16]$Limit, 15 | 16 | [uint16]$Offset, 17 | 18 | [switch]$Raw 19 | ) 20 | 21 | process { 22 | 23 | $Segments = [System.Collections.ArrayList]::new(@('extras', 'tags')) 24 | 25 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters 26 | 27 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 28 | 29 | InvokeNetboxRequest -URI $URI -Raw:$Raw 30 | } 31 | } -------------------------------------------------------------------------------- /Functions/Virtualization/VirtualizationCluster/Get-NetboxVirtualizationClusterGroup.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxVirtualizationClusterGroup { 3 | [CmdletBinding()] 4 | param 5 | ( 6 | [string]$Name, 7 | 8 | [string]$Slug, 9 | 10 | [string]$Description, 11 | 12 | [string]$Query, 13 | 14 | [uint64[]]$Id, 15 | 16 | [uint16]$Limit, 17 | 18 | [uint16]$Offset, 19 | 20 | [switch]$Raw 21 | ) 22 | 23 | $Segments = [System.Collections.ArrayList]::new(@('virtualization', 'cluster-groups')) 24 | 25 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 26 | 27 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 28 | 29 | InvokeNetboxRequest -URI $uri -Raw:$Raw 30 | } -------------------------------------------------------------------------------- /Functions/DCIM/Interfaces/Get-NetboxDCIMInterfaceConnection.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxDCIMInterfaceConnection { 3 | [CmdletBinding()] 4 | [OutputType([pscustomobject])] 5 | param 6 | ( 7 | [uint16]$Limit, 8 | 9 | [uint16]$Offset, 10 | 11 | [uint64]$Id, 12 | 13 | [object]$Connection_Status, 14 | 15 | [uint64]$Site, 16 | 17 | [uint64]$Device, 18 | 19 | [switch]$Raw 20 | ) 21 | 22 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'interface-connections')) 23 | 24 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Raw' 25 | 26 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 27 | 28 | InvokeNetboxRequest -URI $URI -Raw:$Raw 29 | } -------------------------------------------------------------------------------- /Functions/DCIM/RearPorts/Get-NetboxDCIMRearPort.ps1: -------------------------------------------------------------------------------- 1 | function Get-NetboxDCIMRearPort { 2 | [CmdletBinding()] 3 | [OutputType([pscustomobject])] 4 | param 5 | ( 6 | [uint16]$Limit, 7 | 8 | [uint16]$Offset, 9 | 10 | [Parameter(ValueFromPipelineByPropertyName = $true)] 11 | [uint64]$Id, 12 | 13 | [string]$Name, 14 | 15 | [string]$Device, 16 | 17 | [uint64]$Device_Id, 18 | 19 | [string]$Type, 20 | 21 | [switch]$Raw 22 | ) 23 | 24 | process { 25 | 26 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'rear-ports')) 27 | 28 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters 29 | 30 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 31 | 32 | InvokeNetboxRequest -URI $URI -Raw:$Raw 33 | } 34 | } -------------------------------------------------------------------------------- /Functions/DCIM/FrontPorts/Get-NetboxDCIMFrontPort.ps1: -------------------------------------------------------------------------------- 1 | function Get-NetboxDCIMFrontPort { 2 | [CmdletBinding()] 3 | [OutputType([pscustomobject])] 4 | param 5 | ( 6 | [uint16]$Limit, 7 | 8 | [uint16]$Offset, 9 | 10 | [Parameter(ValueFromPipelineByPropertyName = $true)] 11 | [uint64]$Id, 12 | 13 | [string]$Name, 14 | 15 | [string]$Device, 16 | 17 | [uint64]$Device_Id, 18 | 19 | [string]$Type, 20 | 21 | [switch]$Raw 22 | ) 23 | 24 | process { 25 | 26 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'front-ports')) 27 | 28 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters 29 | 30 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 31 | 32 | InvokeNetboxRequest -URI $URI -Raw:$Raw 33 | } 34 | } -------------------------------------------------------------------------------- /Tests/IPAMChoices.json: -------------------------------------------------------------------------------- 1 | {"aggregate:family":[{"label":"IPv4","value":4},{"label":"IPv6","value":6}],"prefix:family":[{"label":"IPv4","value":4},{"label":"IPv6","value":6}],"prefix:status":[{"label":"Container","value":0},{"label":"Active","value":1},{"label":"Reserved","value":2},{"label":"Deprecated","value":3}],"ip-address:family":[{"label":"IPv4","value":4},{"label":"IPv6","value":6}],"ip-address:status":[{"label":"Active","value":1},{"label":"Reserved","value":2},{"label":"Deprecated","value":3},{"label":"DHCP","value":5}],"ip-address:role":[{"label":"Loopback","value":10},{"label":"Secondary","value":20},{"label":"Anycast","value":30},{"label":"VIP","value":40},{"label":"VRRP","value":41},{"label":"HSRP","value":42},{"label":"GLBP","value":43},{"label":"CARP","value":44}],"vlan:status":[{"label":"Active","value":1},{"label":"Reserved","value":2},{"label":"Deprecated","value":3}],"service:protocol":[{"label":"TCP","value":6},{"label":"UDP","value":17}]} -------------------------------------------------------------------------------- /Functions/Virtualization/VirtualMachineInterface/Add-NetboxVirtualMachineInterface.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Add-NetboxVirtualMachineInterface { 3 | [CmdletBinding()] 4 | param 5 | ( 6 | [Parameter(Mandatory = $true)] 7 | [string]$Name, 8 | 9 | [Parameter(Mandatory = $true)] 10 | [uint64]$Virtual_Machine, 11 | 12 | [boolean]$Enabled = $true, 13 | 14 | [string]$MAC_Address, 15 | 16 | [uint16]$MTU, 17 | 18 | [string]$Description, 19 | 20 | [switch]$Raw 21 | ) 22 | 23 | $Segments = [System.Collections.ArrayList]::new(@('virtualization', 'interfaces')) 24 | 25 | $PSBoundParameters.Enabled = $Enabled 26 | 27 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 28 | 29 | $uri = BuildNewURI -Segments $URIComponents.Segments 30 | 31 | InvokeNetboxRequest -URI $uri -Method POST -Body $URIComponents.Parameters 32 | } -------------------------------------------------------------------------------- /Functions/DCIM/RearPorts/Remove-NetboxDCIMRearPort.ps1: -------------------------------------------------------------------------------- 1 | function Remove-NetboxDCIMRearPort { 2 | 3 | [CmdletBinding(ConfirmImpact = 'High', 4 | SupportsShouldProcess = $true)] 5 | param 6 | ( 7 | [Parameter(Mandatory = $true, 8 | ValueFromPipelineByPropertyName = $true)] 9 | [uint64[]]$Id, 10 | 11 | [switch]$Force 12 | ) 13 | 14 | begin { 15 | 16 | } 17 | 18 | process { 19 | foreach ($RearPortID in $Id) { 20 | $CurrentPort = Get-NetboxDCIMRearPort -Id $RearPortID -ErrorAction Stop 21 | 22 | if ($Force -or $pscmdlet.ShouldProcess("Name: $($CurrentPort.Name) | ID: $($CurrentPort.Id)", "Remove")) { 23 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'rear-ports', $CurrentPort.Id)) 24 | 25 | $URI = BuildNewURI -Segments $Segments 26 | 27 | InvokeNetboxRequest -URI $URI -Method DELETE 28 | } 29 | } 30 | } 31 | 32 | end { 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /Functions/DCIM/Cable Terminations/Get-NetboxDCIMCableTermination.ps1: -------------------------------------------------------------------------------- 1 | function Get-NetboxDCIMCableTermination { 2 | [CmdletBinding()] 3 | #region Parameters 4 | param 5 | ( 6 | [uint16]$Limit, 7 | 8 | [uint16]$Offset, 9 | 10 | [Parameter(ValueFromPipelineByPropertyName = $true)] 11 | [uint64[]]$Id, 12 | 13 | [uint64]$Cable, 14 | 15 | [string]$Cable_End, 16 | 17 | [string]$Termination_Type, 18 | 19 | [uint64]$Termination_ID, 20 | 21 | [switch]$Raw 22 | ) 23 | 24 | #endregion Parameters 25 | 26 | process { 27 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'cable-terminations')) 28 | 29 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Raw' 30 | 31 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 32 | 33 | InvokeNetboxRequest -URI $URI -Raw:$Raw 34 | } 35 | } -------------------------------------------------------------------------------- /Functions/DCIM/FrontPorts/Remove-NetboxDCIMFrontPort.ps1: -------------------------------------------------------------------------------- 1 | function Remove-NetboxDCIMFrontPort { 2 | 3 | [CmdletBinding(ConfirmImpact = 'High', 4 | SupportsShouldProcess = $true)] 5 | param 6 | ( 7 | [Parameter(Mandatory = $true, 8 | ValueFromPipelineByPropertyName = $true)] 9 | [uint64[]]$Id, 10 | 11 | [switch]$Force 12 | ) 13 | 14 | begin { 15 | 16 | } 17 | 18 | process { 19 | foreach ($FrontPortID in $Id) { 20 | $CurrentPort = Get-NetboxDCIMFrontPort -Id $FrontPortID -ErrorAction Stop 21 | 22 | if ($Force -or $pscmdlet.ShouldProcess("Name: $($CurrentPort.Name) | ID: $($CurrentPort.Id)", "Remove")) { 23 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'front-ports', $CurrentPort.Id)) 24 | 25 | $URI = BuildNewURI -Segments $Segments 26 | 27 | InvokeNetboxRequest -URI $URI -Method DELETE 28 | } 29 | } 30 | } 31 | 32 | end { 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /Functions/Setup/Set-NetboxCredential.ps1: -------------------------------------------------------------------------------- 1 | function Set-NetboxCredential { 2 | [CmdletBinding(DefaultParameterSetName = 'CredsObject', 3 | ConfirmImpact = 'Low', 4 | SupportsShouldProcess = $true)] 5 | [OutputType([pscredential])] 6 | param 7 | ( 8 | [Parameter(ParameterSetName = 'CredsObject', 9 | Mandatory = $true)] 10 | [pscredential]$Credential, 11 | 12 | [Parameter(ParameterSetName = 'UserPass', 13 | Mandatory = $true)] 14 | [securestring]$Token 15 | ) 16 | 17 | if ($PSCmdlet.ShouldProcess('Netbox Credentials', 'Set')) { 18 | switch ($PsCmdlet.ParameterSetName) { 19 | 'CredsObject' { 20 | $script:NetboxConfig.Credential = $Credential 21 | break 22 | } 23 | 24 | 'UserPass' { 25 | $script:NetboxConfig.Credential = [System.Management.Automation.PSCredential]::new('notapplicable', $Token) 26 | break 27 | } 28 | } 29 | 30 | $script:NetboxConfig.Credential 31 | } 32 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Ben Claussen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /Functions/DCIM/FrontPorts/Add-NetboxDCIMFrontPort.ps1: -------------------------------------------------------------------------------- 1 | function Add-NetboxDCIMFrontPort { 2 | [CmdletBinding()] 3 | [OutputType([pscustomobject])] 4 | param 5 | ( 6 | [Parameter(Mandatory = $true)] 7 | [uint64]$Device, 8 | 9 | [uint64]$Module, 10 | 11 | [Parameter(Mandatory = $true)] 12 | [string]$Name, 13 | 14 | [string]$Label, 15 | 16 | [Parameter(Mandatory = $true)] 17 | [string]$Type, 18 | 19 | [ValidatePattern('^[0-9a-f]{6}$')] 20 | [string]$Color, 21 | 22 | [Parameter(Mandatory = $true)] 23 | [uint64]$Rear_Port, 24 | 25 | [uint64]$Rear_Port_Position, 26 | 27 | [string]$Description, 28 | 29 | [bool]$Mark_Connected, 30 | 31 | [uint16[]]$Tags 32 | 33 | ) 34 | 35 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'front-ports')) 36 | 37 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters 38 | 39 | $URI = BuildNewURI -Segments $URIComponents.Segments 40 | 41 | InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method POST 42 | } -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | // Place your settings in this file to overwrite default and user settings. 2 | { 3 | "editor.formatOnSave": true, 4 | "files.trimTrailingWhitespace": true, 5 | "powershell.scriptAnalysis.settingsPath": ".vscode/PSScriptAnalyzerSettings.psd1", 6 | "powershell.codeFormatting.newLineAfterCloseBrace": false, 7 | "powershell.codeFormatting.useCorrectCasing": true, 8 | "powershell.codeFormatting.preset": "OTBS", 9 | "powershell.codeFormatting.addWhitespaceAroundPipe": true, 10 | "powershell.codeFormatting.autoCorrectAliases": true, 11 | "powershell.codeFormatting.newLineAfterCloseBrace": true, 12 | "powershell.codeFormatting.newLineAfterOpenBrace": true, 13 | "powershell.codeFormatting.openBraceOnSameLine": true, 14 | "powershell.codeFormatting.whitespaceAfterSeparator": true, 15 | "powershell.codeFormatting.whitespaceAroundOperator": true, 16 | "powershell.codeFormatting.whitespaceBeforeOpenBrace": true, 17 | "powershell.codeFormatting.whitespaceBeforeOpenParen": true, 18 | "powershell.codeFormatting.trimWhitespaceAroundPipe": true, 19 | "[markdown]": { 20 | "files.trimTrailingWhitespace": false, 21 | } 22 | } -------------------------------------------------------------------------------- /Functions/DCIM/Devices/Get-NetboxDCIMDeviceType.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxDCIMDeviceType { 3 | [CmdletBinding()] 4 | #region Parameters 5 | param 6 | ( 7 | [uint16]$Offset, 8 | 9 | [uint16]$Limit, 10 | 11 | [uint64[]]$Id, 12 | 13 | [string]$Query, 14 | 15 | [string]$Slug, 16 | 17 | [string]$Manufacturer, 18 | 19 | [uint64]$Manufacturer_Id, 20 | 21 | [string]$Model, 22 | 23 | [string]$Part_Number, 24 | 25 | [uint16]$U_Height, 26 | 27 | [bool]$Is_Full_Depth, 28 | 29 | [bool]$Is_Console_Server, 30 | 31 | [bool]$Is_PDU, 32 | 33 | [bool]$Is_Network_Device, 34 | 35 | [uint16]$Subdevice_Role, 36 | 37 | [switch]$Raw 38 | ) 39 | #endregion Parameters 40 | 41 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'device-types')) 42 | 43 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Raw' 44 | 45 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 46 | 47 | InvokeNetboxRequest -URI $URI -Raw:$Raw 48 | } -------------------------------------------------------------------------------- /Functions/DCIM/RearPorts/Add-NetboxDCIMRearPort.ps1: -------------------------------------------------------------------------------- 1 | function Add-NetboxDCIMRearPort { 2 | [CmdletBinding()] 3 | [OutputType([pscustomobject])] 4 | param 5 | ( 6 | [Parameter(Mandatory = $true)] 7 | [uint64]$Device, 8 | 9 | [uint64]$Module, 10 | 11 | [Parameter(Mandatory = $true)] 12 | [string]$Name, 13 | 14 | [string]$Label, 15 | 16 | [Parameter(Mandatory = $true)] 17 | [string]$Type, 18 | 19 | [ValidatePattern('^[0-9a-f]{6}$')] 20 | [string]$Color, 21 | 22 | [uint16]$Positions = 1, 23 | 24 | [string]$Description, 25 | 26 | [bool]$Mark_Connected, 27 | 28 | [uint16[]]$Tags 29 | ) 30 | 31 | begin { 32 | 33 | } 34 | 35 | process { 36 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'rear-ports')) 37 | 38 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters 39 | 40 | $URI = BuildNewURI -Segments $URIComponents.Segments 41 | 42 | InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method POST 43 | } 44 | 45 | end { 46 | 47 | } 48 | } -------------------------------------------------------------------------------- /Functions/DCIM/Interfaces/Remove-NetboxDCIMInterfaceConnection.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Remove-NetboxDCIMInterfaceConnection { 3 | [CmdletBinding(ConfirmImpact = 'High', 4 | SupportsShouldProcess = $true)] 5 | [OutputType([void])] 6 | param 7 | ( 8 | [Parameter(Mandatory = $true, 9 | ValueFromPipelineByPropertyName = $true)] 10 | [uint64[]]$Id, 11 | 12 | [switch]$Force 13 | ) 14 | 15 | begin { 16 | 17 | } 18 | 19 | process { 20 | foreach ($ConnectionID in $Id) { 21 | $CurrentConnection = Get-NetboxDCIMInterfaceConnection -Id $ConnectionID -ErrorAction Stop 22 | 23 | if ($Force -or $pscmdlet.ShouldProcess("Connection ID $($ConnectionID.Id)", "REMOVE")) { 24 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'interface-connections', $CurrentConnection.Id)) 25 | 26 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force' 27 | 28 | $URI = BuildNewURI -Segments $URIComponents.Segments 29 | 30 | InvokeNetboxRequest -URI $URI -Method DELETE 31 | } 32 | } 33 | } 34 | 35 | end { 36 | 37 | } 38 | } -------------------------------------------------------------------------------- /Functions/DCIM/Cables/Get-NetboxDCIMCable.ps1: -------------------------------------------------------------------------------- 1 | function Get-NetboxDCIMCable { 2 | [CmdletBinding()] 3 | #region Parameters 4 | param 5 | ( 6 | [uint16]$Limit, 7 | 8 | [uint16]$Offset, 9 | 10 | [Parameter(ValueFromPipelineByPropertyName = $true)] 11 | [uint64[]]$Id, 12 | 13 | [string]$Label, 14 | 15 | [string]$Termination_A_Type, 16 | 17 | [uint64]$Termination_A_ID, 18 | 19 | [string]$Termination_B_Type, 20 | 21 | [uint64]$Termination_B_ID, 22 | 23 | [string]$Type, 24 | 25 | [string]$Status, 26 | 27 | [string]$Color, 28 | 29 | [uint64]$Device_ID, 30 | 31 | [string]$Device, 32 | 33 | [uint64]$Rack_Id, 34 | 35 | [string]$Rack, 36 | 37 | [uint64]$Location_ID, 38 | 39 | [string]$Location, 40 | 41 | [switch]$Raw 42 | ) 43 | 44 | #endregion Parameters 45 | 46 | process { 47 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'cables')) 48 | 49 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Raw' 50 | 51 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 52 | 53 | InvokeNetboxRequest -URI $URI -Raw:$Raw 54 | } 55 | } -------------------------------------------------------------------------------- /Functions/IPAM/Address/Remove-NetboxIPAMAddress.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Remove-NetboxIPAMAddress { 3 | <# 4 | .SYNOPSIS 5 | Remove an IP address from Netbox 6 | 7 | .DESCRIPTION 8 | Removes/deletes an IP address from Netbox by ID and optional other filters 9 | 10 | .PARAMETER Id 11 | Database ID of the IP address object. 12 | 13 | .PARAMETER Force 14 | Do not confirm. 15 | 16 | .EXAMPLE 17 | PS C:\> Remove-NetboxIPAMAddress -Id $value1 18 | 19 | .NOTES 20 | Additional information about the function. 21 | #> 22 | 23 | [CmdletBinding(ConfirmImpact = 'High', 24 | SupportsShouldProcess = $true)] 25 | param 26 | ( 27 | [Parameter(Mandatory = $true, 28 | ValueFromPipelineByPropertyName = $true)] 29 | [uint64[]]$Id, 30 | 31 | [switch]$Force 32 | ) 33 | 34 | process { 35 | foreach ($IPId in $Id) { 36 | $CurrentIP = Get-NetboxIPAMAddress -Id $IPId -ErrorAction Stop 37 | 38 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-addresses', $IPId)) 39 | 40 | if ($Force -or $pscmdlet.ShouldProcess($CurrentIP.Address, "Delete")) { 41 | $URI = BuildNewURI -Segments $Segments 42 | 43 | InvokeNetboxRequest -URI $URI -Method DELETE 44 | } 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /Functions/IPAM/Range/Remove-NetboxIPAMAddressRange.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Remove-NetboxIPAMAddressRange { 3 | <# 4 | .SYNOPSIS 5 | Remove an IP address range from Netbox 6 | 7 | .DESCRIPTION 8 | Removes/deletes an IP address range from Netbox by ID 9 | 10 | .PARAMETER Id 11 | Database ID of the IP address range object. 12 | 13 | .PARAMETER Force 14 | Do not confirm. 15 | 16 | .EXAMPLE 17 | PS C:\> Remove-NetboxIPAMAddressRange -Id 1234 18 | 19 | .NOTES 20 | Additional information about the function. 21 | #> 22 | 23 | [CmdletBinding(ConfirmImpact = 'High', 24 | SupportsShouldProcess = $true)] 25 | param 26 | ( 27 | [Parameter(Mandatory = $true, 28 | ValueFromPipelineByPropertyName = $true)] 29 | [uint64[]]$Id, 30 | 31 | [switch]$Force 32 | ) 33 | 34 | process { 35 | foreach ($Range_Id in $Id) { 36 | $CurrentRange = Get-NetboxIPAMAddressRange -Id $Range_Id -ErrorAction Stop 37 | 38 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-ranges', $Range_Id)) 39 | 40 | if ($Force -or $pscmdlet.ShouldProcess($CurrentRange.start_address, "Delete")) { 41 | $URI = BuildNewURI -Segments $Segments 42 | 43 | InvokeNetboxRequest -URI $URI -Method DELETE 44 | } 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /Functions/IPAM/Prefix/New-NetboxIPAMPrefix.ps1: -------------------------------------------------------------------------------- 1 | 2 | function New-NetboxIPAMPrefix { 3 | [CmdletBinding(ConfirmImpact = 'low', 4 | SupportsShouldProcess = $true)] 5 | [CmdletBinding()] 6 | param 7 | ( 8 | [Parameter(Mandatory = $true)] 9 | [string]$Prefix, 10 | 11 | [object]$Status = 'Active', 12 | 13 | [uint64]$Tenant, 14 | 15 | [object]$Role, 16 | 17 | [bool]$IsPool, 18 | 19 | [string]$Description, 20 | 21 | [uint64]$Site, 22 | 23 | [uint64]$VRF, 24 | 25 | [uint64]$VLAN, 26 | 27 | [hashtable]$Custom_Fields, 28 | 29 | [switch]$Raw 30 | ) 31 | 32 | # $PSBoundParameters.Status = ValidateIPAMChoice -ProvidedValue $Status -PrefixStatus 33 | 34 | <# 35 | # As of 2018/10/18, this does not appear to be a validated IPAM choice 36 | if ($null -ne $Role) { 37 | $PSBoundParameters.Role = ValidateIPAMChoice -ProvidedValue $Role -PrefixRole 38 | } 39 | #> 40 | 41 | $segments = [System.Collections.ArrayList]::new(@('ipam', 'prefixes')) 42 | 43 | $URIComponents = BuildURIComponents -URISegments $segments -ParametersDictionary $PSBoundParameters 44 | 45 | $URI = BuildNewURI -Segments $URIComponents.Segments 46 | 47 | if ($PSCmdlet.ShouldProcess($Prefix, 'Create new Prefix')) { 48 | InvokeNetboxRequest -URI $URI -Method POST -Body $URIComponents.Parameters -Raw:$Raw 49 | } 50 | } -------------------------------------------------------------------------------- /Functions/DCIM/Devices/Remove-NetboxDCIMDevice.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Remove-NetboxDCIMDevice { 3 | <# 4 | .SYNOPSIS 5 | Delete a device 6 | 7 | .DESCRIPTION 8 | Deletes a device from Netbox by ID 9 | 10 | .PARAMETER Id 11 | Database ID of the device 12 | 13 | .PARAMETER Force 14 | Force deletion without any prompts 15 | 16 | .EXAMPLE 17 | PS C:\> Remove-NetboxDCIMDevice -Id $value1 18 | 19 | .NOTES 20 | Additional information about the function. 21 | #> 22 | 23 | [CmdletBinding(ConfirmImpact = 'High', 24 | SupportsShouldProcess = $true)] 25 | param 26 | ( 27 | [Parameter(Mandatory = $true, 28 | ValueFromPipelineByPropertyName = $true)] 29 | [uint64[]]$Id, 30 | 31 | [switch]$Force 32 | ) 33 | 34 | begin { 35 | 36 | } 37 | 38 | process { 39 | foreach ($DeviceID in $Id) { 40 | $CurrentDevice = Get-NetboxDCIMDevice -Id $DeviceID -ErrorAction Stop 41 | 42 | if ($Force -or $pscmdlet.ShouldProcess("Name: $($CurrentDevice.Name) | ID: $($CurrentDevice.Id)", "Remove")) { 43 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'devices', $CurrentDevice.Id)) 44 | 45 | $URI = BuildNewURI -Segments $Segments 46 | 47 | InvokeNetboxRequest -URI $URI -Method DELETE 48 | } 49 | } 50 | } 51 | 52 | end { 53 | 54 | } 55 | } -------------------------------------------------------------------------------- /Functions/DCIM/Interfaces/Remove-NetboxDCIMInterface.ps1: -------------------------------------------------------------------------------- 1 | function Remove-NetboxDCIMInterface { 2 | <# 3 | .SYNOPSIS 4 | Removes an interface 5 | 6 | .DESCRIPTION 7 | Removes an interface by ID from a device 8 | 9 | .PARAMETER Id 10 | A description of the Id parameter. 11 | 12 | .PARAMETER Force 13 | A description of the Force parameter. 14 | 15 | .EXAMPLE 16 | PS C:\> Remove-NetboxDCIMInterface -Id $value1 17 | 18 | .NOTES 19 | Additional information about the function. 20 | #> 21 | 22 | [CmdletBinding(ConfirmImpact = 'High', 23 | SupportsShouldProcess = $true)] 24 | param 25 | ( 26 | [Parameter(Mandatory = $true, 27 | ValueFromPipelineByPropertyName = $true)] 28 | [uint64[]]$Id, 29 | 30 | [switch]$Force 31 | ) 32 | 33 | begin { 34 | 35 | } 36 | 37 | process { 38 | foreach ($InterfaceId in $Id) { 39 | $CurrentInterface = Get-NetboxDCIMInterface -Id $InterfaceId -ErrorAction Stop 40 | 41 | if ($Force -or $pscmdlet.ShouldProcess("Name: $($CurrentInterface.Name) | ID: $($CurrentInterface.Id)", "Remove")) { 42 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'interfaces', $CurrentInterface.Id)) 43 | 44 | $URI = BuildNewURI -Segments $Segments 45 | 46 | InvokeNetboxRequest -URI $URI -Method DELETE 47 | } 48 | } 49 | } 50 | 51 | end { 52 | 53 | } 54 | } -------------------------------------------------------------------------------- /Functions/Virtualization/VirtualMachine/Remove-NetboxVirtualMachine.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Remove-NetboxVirtualMachine { 3 | <# 4 | .SYNOPSIS 5 | Delete a virtual machine 6 | 7 | .DESCRIPTION 8 | Deletes a virtual machine from Netbox by ID 9 | 10 | .PARAMETER Id 11 | Database ID of the virtual machine 12 | 13 | .PARAMETER Force 14 | Force deletion without any prompts 15 | 16 | .EXAMPLE 17 | PS C:\> Remove-NetboxVirtualMachine -Id $value1 18 | 19 | .NOTES 20 | Additional information about the function. 21 | #> 22 | 23 | [CmdletBinding(ConfirmImpact = 'High', 24 | SupportsShouldProcess = $true)] 25 | param 26 | ( 27 | [Parameter(Mandatory = $true, 28 | ValueFromPipelineByPropertyName = $true)] 29 | [uint64[]]$Id, 30 | 31 | [switch]$Force 32 | ) 33 | 34 | begin { 35 | 36 | } 37 | 38 | process { 39 | foreach ($VMId in $Id) { 40 | $CurrentVM = Get-NetboxVirtualMachine -Id $VMId -ErrorAction Stop 41 | 42 | if ($Force -or $pscmdlet.ShouldProcess("$($CurrentVM.Name)/$($CurrentVM.Id)", "Remove")) { 43 | $Segments = [System.Collections.ArrayList]::new(@('virtualization', 'virtual-machines', $CurrentVM.Id)) 44 | 45 | $URI = BuildNewURI -Segments $Segments 46 | 47 | InvokeNetboxRequest -URI $URI -Method DELETE 48 | } 49 | } 50 | } 51 | 52 | end { 53 | 54 | } 55 | } -------------------------------------------------------------------------------- /Functions/Helpers/Get-ModelDefinition.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-ModelDefinition { 3 | [CmdletBinding(DefaultParameterSetName = 'ByName')] 4 | param 5 | ( 6 | [Parameter(ParameterSetName = 'ByName', 7 | Mandatory = $true)] 8 | [string]$ModelName, 9 | 10 | [Parameter(ParameterSetName = 'ByPath', 11 | Mandatory = $true)] 12 | [string]$URIPath, 13 | 14 | [Parameter(ParameterSetName = 'ByPath')] 15 | [string]$Method = "post" 16 | ) 17 | 18 | switch ($PsCmdlet.ParameterSetName) { 19 | 'ByName' { 20 | $script:NetboxConfig.APIDefinition.definitions.$ModelName 21 | break 22 | } 23 | 24 | 'ByPath' { 25 | switch ($Method) { 26 | "get" { 27 | 28 | break 29 | } 30 | 31 | "post" { 32 | if (-not $URIPath.StartsWith('/')) { 33 | $URIPath = "/$URIPath" 34 | } 35 | 36 | if (-not $URIPath.EndsWith('/')) { 37 | $URIPath = "$URIPath/" 38 | } 39 | 40 | $ModelName = $script:NetboxConfig.APIDefinition.paths.$URIPath.post.parameters.schema.'$ref'.split('/')[-1] 41 | $script:NetboxConfig.APIDefinition.definitions.$ModelName 42 | break 43 | } 44 | } 45 | 46 | break 47 | } 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Functions/DCIM/Devices/Get-NetboxDCIMDeviceRole.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxDCIMDeviceRole { 3 | [CmdletBinding()] 4 | param 5 | ( 6 | [uint16]$Limit, 7 | 8 | [uint16]$Offset, 9 | 10 | [Parameter(ParameterSetName = 'ById')] 11 | [uint64[]]$Id, 12 | 13 | [string]$Name, 14 | 15 | [string]$Slug, 16 | 17 | [string]$Color, 18 | 19 | [bool]$VM_Role, 20 | 21 | [switch]$Raw 22 | ) 23 | 24 | switch ($PSCmdlet.ParameterSetName) { 25 | 'ById' { 26 | foreach ($DRId in $Id) { 27 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'device-roles', $DRId)) 28 | 29 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Raw' 30 | 31 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 32 | 33 | InvokeNetboxRequest -URI $URI -Raw:$Raw 34 | } 35 | 36 | break 37 | } 38 | 39 | default { 40 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'device-roles')) 41 | 42 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Raw' 43 | 44 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 45 | 46 | InvokeNetboxRequest -URI $URI -Raw:$Raw 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /Functions/DCIM/Get-NetboxDCIMPlatform.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxDCIMPlatform { 3 | [CmdletBinding()] 4 | [OutputType([pscustomobject])] 5 | param 6 | ( 7 | [uint16]$Limit, 8 | 9 | [uint16]$Offset, 10 | 11 | [Parameter(ParameterSetName = 'ById')] 12 | [uint64[]]$Id, 13 | 14 | [string]$Name, 15 | 16 | [string]$Slug, 17 | 18 | [uint64]$Manufacturer_Id, 19 | 20 | [string]$Manufacturer, 21 | 22 | [switch]$Raw 23 | ) 24 | 25 | switch ($PSCmdlet.ParameterSetName) { 26 | 'ById' { 27 | foreach ($PlatformID in $Id) { 28 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'platforms', $PlatformID)) 29 | 30 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Raw' 31 | 32 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 33 | 34 | InvokeNetboxRequest -URI $URI -Raw:$Raw 35 | } 36 | 37 | break 38 | } 39 | 40 | default { 41 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'platforms')) 42 | 43 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Raw' 44 | 45 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 46 | 47 | InvokeNetboxRequest -URI $URI -Raw:$Raw 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /Functions/DCIM/Interfaces/Add-NetboxDCIMInterfaceConnection.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Add-NetboxDCIMInterfaceConnection { 3 | <# 4 | .SYNOPSIS 5 | Create a new connection between two interfaces 6 | 7 | .DESCRIPTION 8 | Create a new connection between two interfaces 9 | 10 | .PARAMETER Connection_Status 11 | Is it connected or planned? 12 | 13 | .PARAMETER Interface_A 14 | Database ID of interface A 15 | 16 | .PARAMETER Interface_B 17 | Database ID of interface B 18 | 19 | .EXAMPLE 20 | PS C:\> Add-NetboxDCIMInterfaceConnection -Interface_A $value1 -Interface_B $value2 21 | 22 | .NOTES 23 | Additional information about the function. 24 | #> 25 | 26 | [CmdletBinding()] 27 | [OutputType([pscustomobject])] 28 | param 29 | ( 30 | [object]$Connection_Status, 31 | 32 | [Parameter(Mandatory = $true)] 33 | [uint64]$Interface_A, 34 | 35 | [Parameter(Mandatory = $true)] 36 | [uint64]$Interface_B 37 | ) 38 | 39 | # Verify if both Interfaces exist 40 | Get-NetboxDCIMInterface -Id $Interface_A -ErrorAction Stop | Out-null 41 | Get-NetboxDCIMInterface -Id $Interface_B -ErrorAction Stop | Out-null 42 | 43 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'interface-connections')) 44 | 45 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters 46 | 47 | $URI = BuildNewURI -Segments $URIComponents.Segments 48 | 49 | InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method POST 50 | } -------------------------------------------------------------------------------- /Functions/DCIM/RearPorts/Set-NetboxDCIMRearPort.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Set-NetboxDCIMRearPort { 3 | [CmdletBinding(ConfirmImpact = 'Medium', 4 | SupportsShouldProcess = $true)] 5 | [OutputType([pscustomobject])] 6 | param 7 | ( 8 | [Parameter(Mandatory = $true, 9 | ValueFromPipelineByPropertyName = $true)] 10 | [uint64[]]$Id, 11 | 12 | [uint64]$Device, 13 | 14 | [uint64]$Module, 15 | 16 | [string]$Name, 17 | 18 | [string]$Label, 19 | 20 | [string]$Type, 21 | 22 | [ValidatePattern('^[0-9a-f]{6}$')] 23 | [string]$Color, 24 | 25 | [uint16]$Positions, 26 | 27 | [string]$Description, 28 | 29 | [bool]$Mark_Connected, 30 | 31 | [uint16[]]$Tags, 32 | 33 | [switch]$Force 34 | ) 35 | 36 | begin { 37 | 38 | } 39 | 40 | process { 41 | foreach ($RearPortID in $Id) { 42 | $CurrentPort = Get-NetboxDCIMRearPort -Id $RearPortID -ErrorAction Stop 43 | 44 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'rear-ports', $CurrentPort.Id)) 45 | 46 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' 47 | 48 | $URI = BuildNewURI -Segments $Segments 49 | 50 | if ($Force -or $pscmdlet.ShouldProcess("Rear Port ID $($CurrentPort.Id)", "Set")) { 51 | InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method PATCH 52 | } 53 | } 54 | } 55 | 56 | end { 57 | 58 | } 59 | } -------------------------------------------------------------------------------- /Functions/Circuits/Circuits/New-NetboxCircuit.ps1: -------------------------------------------------------------------------------- 1 | 2 | function New-NetboxCircuit { 3 | [CmdletBinding(ConfirmImpact = 'Low', 4 | SupportsShouldProcess = $true)] 5 | [OutputType([pscustomobject])] 6 | param 7 | ( 8 | [Parameter(Mandatory = $true, 9 | ValueFromPipelineByPropertyName = $true)] 10 | [string]$CID, 11 | 12 | [Parameter(Mandatory = $true)] 13 | [uint64]$Provider, 14 | 15 | [Parameter(Mandatory = $true)] 16 | [uint64]$Type, 17 | 18 | #[ValidateSet('Active', 'Planned', 'Provisioning', 'Offline', 'Deprovisioning', 'Decommissioned ')] 19 | [uint16]$Status = 'Active', 20 | 21 | [string]$Description, 22 | 23 | [uint64]$Tenant, 24 | 25 | [string]$Termination_A, 26 | 27 | [datetime]$Install_Date, 28 | 29 | [string]$Termination_Z, 30 | 31 | [ValidateRange(0, 2147483647)] 32 | [uint64]$Commit_Rate, 33 | 34 | [string]$Comments, 35 | 36 | [hashtable]$Custom_Fields, 37 | 38 | [switch]$Force, 39 | 40 | [switch]$Raw 41 | ) 42 | 43 | process { 44 | $Segments = [System.Collections.ArrayList]::new(@('circuits', 'circuits')) 45 | $Method = 'POST' 46 | 47 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 48 | 49 | $URI = BuildNewURI -Segments $URIComponents.Segments 50 | 51 | if ($Force -or $PSCmdlet.ShouldProcess($CID, 'Create new circuit')) { 52 | InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters -Raw:$Raw 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /Functions/DCIM/FrontPorts/Set-NetboxDCIMFrontPort.ps1: -------------------------------------------------------------------------------- 1 | function Set-NetboxDCIMFrontPort { 2 | [CmdletBinding(ConfirmImpact = 'Medium', 3 | SupportsShouldProcess = $true)] 4 | [OutputType([pscustomobject])] 5 | param 6 | ( 7 | [Parameter(Mandatory = $true, 8 | ValueFromPipelineByPropertyName = $true)] 9 | [uint64[]]$Id, 10 | 11 | [uint16]$Device, 12 | 13 | [uint16]$Module, 14 | 15 | [string]$Name, 16 | 17 | [string]$Label, 18 | 19 | [string]$Type, 20 | 21 | [ValidatePattern('^[0-9a-f]{6}$')] 22 | [string]$Color, 23 | 24 | [uint64]$Rear_Port, 25 | 26 | [uint16]$Rear_Port_Position, 27 | 28 | [string]$Description, 29 | 30 | [bool]$Mark_Connected, 31 | 32 | [uint64[]]$Tags, 33 | 34 | [switch]$Force 35 | ) 36 | 37 | begin { 38 | 39 | } 40 | 41 | process { 42 | foreach ($FrontPortID in $Id) { 43 | $CurrentPort = Get-NetboxDCIMFrontPort -Id $FrontPortID -ErrorAction Stop 44 | 45 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'front-ports', $CurrentPort.Id)) 46 | 47 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' 48 | 49 | $URI = BuildNewURI -Segments $Segments 50 | 51 | if ($Force -or $pscmdlet.ShouldProcess("Front Port ID $($CurrentPort.Id)", "Set")) { 52 | InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method PATCH 53 | } 54 | } 55 | } 56 | 57 | end { 58 | 59 | } 60 | } -------------------------------------------------------------------------------- /Functions/DCIM/Devices/New-NetboxDCIMDevice.ps1: -------------------------------------------------------------------------------- 1 | 2 | function New-NetboxDCIMDevice { 3 | [CmdletBinding(ConfirmImpact = 'low', 4 | SupportsShouldProcess = $true)] 5 | [OutputType([pscustomobject])] 6 | #region Parameters 7 | param 8 | ( 9 | [Parameter(Mandatory = $true)] 10 | [string]$Name, 11 | 12 | [Parameter(Mandatory = $true)] 13 | [object]$Device_Role, 14 | 15 | [Parameter(Mandatory = $true)] 16 | [object]$Device_Type, 17 | 18 | [Parameter(Mandatory = $true)] 19 | [uint64]$Site, 20 | 21 | [object]$Status = 'Active', 22 | 23 | [uint64]$Platform, 24 | 25 | [uint64]$Tenant, 26 | 27 | [uint64]$Cluster, 28 | 29 | [uint64]$Rack, 30 | 31 | [uint16]$Position, 32 | 33 | [object]$Face, 34 | 35 | [string]$Serial, 36 | 37 | [string]$Asset_Tag, 38 | 39 | [uint64]$Virtual_Chassis, 40 | 41 | [uint64]$VC_Priority, 42 | 43 | [uint64]$VC_Position, 44 | 45 | [uint64]$Primary_IP4, 46 | 47 | [uint64]$Primary_IP6, 48 | 49 | [string]$Comments, 50 | 51 | [hashtable]$Custom_Fields 52 | ) 53 | #endregion Parameters 54 | 55 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'devices')) 56 | 57 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters 58 | 59 | $URI = BuildNewURI -Segments $URIComponents.Segments 60 | 61 | if ($PSCmdlet.ShouldProcess($Name, 'Create new Device')) { 62 | InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method POST 63 | } 64 | } -------------------------------------------------------------------------------- /Functions/Virtualization/VirtualMachineInterface/Set-NetboxVirtualMachineInterface.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Set-NetboxVirtualMachineInterface { 3 | [CmdletBinding(ConfirmImpact = 'Medium', 4 | SupportsShouldProcess = $true)] 5 | [OutputType([pscustomobject])] 6 | param 7 | ( 8 | [Parameter(Mandatory = $true, 9 | ValueFromPipelineByPropertyName = $true)] 10 | [uint64[]]$Id, 11 | 12 | [string]$Name, 13 | 14 | [string]$MAC_Address, 15 | 16 | [uint16]$MTU, 17 | 18 | [string]$Description, 19 | 20 | [boolean]$Enabled, 21 | 22 | [uint64]$Virtual_Machine, 23 | 24 | [switch]$Force 25 | ) 26 | 27 | begin { 28 | 29 | } 30 | 31 | process { 32 | foreach ($VMI_ID in $Id) { 33 | Write-Verbose "Obtaining VM Interface..." 34 | $CurrentVMI = Get-NetboxVirtualMachineInterface -Id $VMI_ID -ErrorAction Stop 35 | Write-Verbose "Finished obtaining VM Interface" 36 | 37 | $Segments = [System.Collections.ArrayList]::new(@('virtualization', 'interfaces', $CurrentVMI.Id)) 38 | 39 | if ($Force -or $pscmdlet.ShouldProcess("Interface $($CurrentVMI.Id) on VM $($CurrentVMI.Virtual_Machine.Name)", "Set")) { 40 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force' 41 | 42 | $URI = BuildNewURI -Segments $URIComponents.Segments 43 | 44 | InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method PATCH 45 | } 46 | } 47 | } 48 | 49 | end { 50 | 51 | } 52 | } -------------------------------------------------------------------------------- /Functions/Circuits/Types/Get-NetboxCircuitType.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxCircuitType { 3 | [CmdletBinding(DefaultParameterSetName = 'Query')] 4 | param 5 | ( 6 | [Parameter(ParameterSetName = 'ById')] 7 | [uint64[]]$Id, 8 | 9 | [Parameter(ParameterSetName = 'Query')] 10 | [string]$Name, 11 | 12 | [Parameter(ParameterSetName = 'Query')] 13 | [string]$Slug, 14 | 15 | [Parameter(ParameterSetName = 'Query')] 16 | [string]$Query, 17 | 18 | [Parameter(ParameterSetName = 'Query')] 19 | [uint16]$Limit, 20 | 21 | [Parameter(ParameterSetName = 'Query')] 22 | [uint16]$Offset, 23 | 24 | [switch]$Raw 25 | ) 26 | 27 | switch ($PSCmdlet.ParameterSetName) { 28 | 'ById' { 29 | foreach ($i in $ID) { 30 | $Segments = [System.Collections.ArrayList]::new(@('circuits', 'circuit_types', $i)) 31 | 32 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName "Id" 33 | 34 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 35 | 36 | InvokeNetboxRequest -URI $URI -Raw:$Raw 37 | } 38 | } 39 | 40 | default { 41 | $Segments = [System.Collections.ArrayList]::new(@('circuits', 'circuit-types')) 42 | 43 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters 44 | 45 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 46 | 47 | InvokeNetboxRequest -URI $URI -Raw:$Raw 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /Functions/IPAM/Range/Set-NetboxIPAMAddressRange.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Set-NetboxIPAMAddressRange { 3 | [CmdletBinding(ConfirmImpact = 'Medium', 4 | SupportsShouldProcess = $true)] 5 | param 6 | ( 7 | [Parameter(Mandatory = $true, 8 | ValueFromPipelineByPropertyName = $true)] 9 | [uint64[]]$Id, 10 | 11 | [string]$Start_Address, 12 | 13 | [string]$End_Address, 14 | 15 | [object]$Status, 16 | 17 | [uint64]$Tenant, 18 | 19 | [uint64]$VRF, 20 | 21 | [object]$Role, 22 | 23 | [hashtable]$Custom_Fields, 24 | 25 | [string]$Description, 26 | 27 | [string]$Comments, 28 | 29 | [object[]]$Tags, 30 | 31 | [switch]$Mark_Utilized, 32 | 33 | [switch]$Force, 34 | 35 | [switch]$Raw 36 | ) 37 | 38 | begin { 39 | $Method = 'PATCH' 40 | } 41 | 42 | process { 43 | foreach ($RangeID in $Id) { 44 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-ranges', $RangeID)) 45 | 46 | Write-Verbose "Obtaining IP range from ID $RangeID" 47 | $CurrentRange = Get-NetboxIPAMAddressRange -Id $RangeID -ErrorAction Stop 48 | 49 | if ($Force -or $PSCmdlet.ShouldProcess("$($CurrentRange.Start_Address) - $($CurrentRange.End_Address)", 'Set')) { 50 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force' 51 | 52 | $URI = BuildNewURI -Segments $URIComponents.Segments 53 | 54 | InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method $Method 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Functions/Virtualization/VirtualMachine/Set-NetboxVirtualMachine.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Set-NetboxVirtualMachine { 3 | [CmdletBinding(ConfirmImpact = 'Medium', 4 | SupportsShouldProcess = $true)] 5 | param 6 | ( 7 | [Parameter(Mandatory = $true, 8 | ValueFromPipelineByPropertyName = $true)] 9 | [uint64]$Id, 10 | 11 | [string]$Name, 12 | 13 | [uint64]$Role, 14 | 15 | [uint64]$Cluster, 16 | 17 | [object]$Status, 18 | 19 | [uint64]$Platform, 20 | 21 | [uint64]$Primary_IP4, 22 | 23 | [uint64]$Primary_IP6, 24 | 25 | [byte]$VCPUs, 26 | 27 | [uint64]$Memory, 28 | 29 | [uint64]$Disk, 30 | 31 | [uint64]$Tenant, 32 | 33 | [string]$Comments, 34 | 35 | [hashtable]$Custom_Fields, 36 | 37 | [switch]$Force 38 | ) 39 | 40 | # if ($null -ne $Status) { 41 | # $PSBoundParameters.Status = ValidateVirtualizationChoice -ProvidedValue $Status -VirtualMachineStatus 42 | # } 43 | 44 | process { 45 | $Segments = [System.Collections.ArrayList]::new(@('virtualization', 'virtual-machines', $Id)) 46 | 47 | Write-Verbose "Obtaining VM from ID $Id" 48 | 49 | #$CurrentVM = Get-NetboxVirtualMachine -Id $Id -ErrorAction Stop 50 | 51 | Write-Verbose "Finished obtaining VM" 52 | 53 | if ($Force -or $pscmdlet.ShouldProcess($ID, "Set properties on VM ID")) { 54 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force' 55 | 56 | $URI = BuildNewURI -Segments $URIComponents.Segments 57 | 58 | InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method PATCH 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /Functions/DCIM/Sites/Remove-NetboxDCIMSite.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .NOTES 3 | =========================================================================== 4 | Created with: SAPIEN Technologies, Inc., PowerShell Studio 2020 v5.7.181 5 | Created on: 2020-10-02 15:52 6 | Created by: Claussen 7 | Organization: NEOnet 8 | Filename: New-NetboxDCIMSite.ps1 9 | =========================================================================== 10 | .DESCRIPTION 11 | A description of the file. 12 | #> 13 | 14 | 15 | function Remove-NetboxDCIMSite { 16 | <# 17 | .SYNOPSIS 18 | Remove a Site 19 | 20 | .DESCRIPTION 21 | Remove a DCIM Site from Netbox 22 | 23 | .EXAMPLE 24 | Remove-NetboxDCIMSite -Id 1 25 | 26 | Remove DCM Site with id 1 27 | 28 | .EXAMPLE 29 | Get-NetboxDCIMSite -name My Site | Remove-NetboxDCIMSite -confirm:$false 30 | 31 | Remove DCM Site with name My Site without confirmation 32 | 33 | #> 34 | 35 | [CmdletBinding(ConfirmImpact = 'High', 36 | SupportsShouldProcess = $true)] 37 | param 38 | ( 39 | [Parameter(Mandatory = $true, 40 | ValueFromPipelineByPropertyName = $true)] 41 | [uint]$Id 42 | 43 | ) 44 | 45 | begin { 46 | 47 | } 48 | 49 | process { 50 | $CurrentSite = Get-NetboxDCIMSite -Id $Id -ErrorAction Stop 51 | 52 | if ($pscmdlet.ShouldProcess("$($CurrentSite.Name)/$($CurrentSite.Id)", "Remove Site")) { 53 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'sites', $CurrentSite.Id)) 54 | 55 | $URI = BuildNewURI -Segments $Segments 56 | 57 | InvokeNetboxRequest -URI $URI -Method DELETE 58 | } 59 | } 60 | 61 | end { 62 | 63 | } 64 | } -------------------------------------------------------------------------------- /Functions/DCIM/Devices/Get-NetboxDCIMDevice.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxDCIMDevice { 3 | [CmdletBinding()] 4 | #region Parameters 5 | param 6 | ( 7 | [uint16]$Limit, 8 | 9 | [uint16]$Offset, 10 | 11 | [Parameter(ValueFromPipelineByPropertyName = $true)] 12 | [uint64[]]$Id, 13 | 14 | [string]$Query, 15 | 16 | [string]$Name, 17 | 18 | [uint64]$Manufacturer_Id, 19 | 20 | [string]$Manufacturer, 21 | 22 | [uint64]$Device_Type_Id, 23 | 24 | [uint64]$Role_Id, 25 | 26 | [string]$Role, 27 | 28 | [uint64]$Tenant_Id, 29 | 30 | [string]$Tenant, 31 | 32 | [uint64]$Platform_Id, 33 | 34 | [string]$Platform, 35 | 36 | [string]$Asset_Tag, 37 | 38 | [uint64]$Site_Id, 39 | 40 | [string]$Site, 41 | 42 | [uint64]$Rack_Group_Id, 43 | 44 | [uint64]$Rack_Id, 45 | 46 | [uint64]$Cluster_Id, 47 | 48 | [uint64]$Model, 49 | 50 | [object]$Status, 51 | 52 | [bool]$Is_Full_Depth, 53 | 54 | [bool]$Is_Console_Server, 55 | 56 | [bool]$Is_PDU, 57 | 58 | [bool]$Is_Network_Device, 59 | 60 | [string]$MAC_Address, 61 | 62 | [bool]$Has_Primary_IP, 63 | 64 | [uint64]$Virtual_Chassis_Id, 65 | 66 | [uint16]$Position, 67 | 68 | [string]$Serial, 69 | 70 | [switch]$Raw 71 | ) 72 | 73 | #endregion Parameters 74 | 75 | process { 76 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'devices')) 77 | 78 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Raw' 79 | 80 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 81 | 82 | InvokeNetboxRequest -URI $URI -Raw:$Raw 83 | } 84 | } -------------------------------------------------------------------------------- /Functions/DCIM/Devices/Set-NetboxDCIMDevice.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Set-NetboxDCIMDevice { 3 | [CmdletBinding(SupportsShouldProcess = $true)] 4 | param 5 | ( 6 | [Parameter(Mandatory = $true, 7 | ValueFromPipelineByPropertyName = $true)] 8 | [uint64[]]$Id, 9 | 10 | [string]$Name, 11 | 12 | [object]$Device_Role, 13 | 14 | [object]$Device_Type, 15 | 16 | [uint64]$Site, 17 | 18 | [object]$Status, 19 | 20 | [uint64]$Platform, 21 | 22 | [uint64]$Tenant, 23 | 24 | [uint64]$Cluster, 25 | 26 | [uint64]$Rack, 27 | 28 | [uint16]$Position, 29 | 30 | [object]$Face, 31 | 32 | [string]$Serial, 33 | 34 | [string]$Asset_Tag, 35 | 36 | [uint64]$Virtual_Chassis, 37 | 38 | [uint64]$VC_Priority, 39 | 40 | [uint64]$VC_Position, 41 | 42 | [uint64]$Primary_IP4, 43 | 44 | [uint64]$Primary_IP6, 45 | 46 | [string]$Comments, 47 | 48 | [hashtable]$Custom_Fields, 49 | 50 | [switch]$Force 51 | ) 52 | 53 | begin { 54 | 55 | } 56 | 57 | process { 58 | foreach ($DeviceID in $Id) { 59 | $CurrentDevice = Get-NetboxDCIMDevice -Id $DeviceID -ErrorAction Stop 60 | 61 | if ($Force -or $pscmdlet.ShouldProcess("$($CurrentDevice.Name)", "Set")) { 62 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'devices', $CurrentDevice.Id)) 63 | 64 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force' 65 | 66 | $URI = BuildNewURI -Segments $URIComponents.Segments 67 | 68 | InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method PATCH 69 | } 70 | } 71 | } 72 | 73 | end { 74 | 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Functions/IPAM/Address/Get-NetboxIPAMAvailableIP.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxIPAMAvailableIP { 3 | <# 4 | .SYNOPSIS 5 | A convenience method for returning available IP addresses within a prefix 6 | 7 | .DESCRIPTION 8 | By default, the number of IPs returned will be equivalent to PAGINATE_COUNT. An arbitrary limit 9 | (up to MAX_PAGE_SIZE, if set) may be passed, however results will not be paginated 10 | 11 | .PARAMETER Prefix_ID 12 | A description of the Prefix_ID parameter. 13 | 14 | .PARAMETER Limit 15 | A description of the Limit parameter. 16 | 17 | .PARAMETER Raw 18 | A description of the Raw parameter. 19 | 20 | .PARAMETER NumberOfIPs 21 | A description of the NumberOfIPs parameter. 22 | 23 | .EXAMPLE 24 | Get-NetboxIPAMAvailableIP -Prefix_ID (Get-NetboxIPAMPrefix -Prefix 192.0.2.0/24).id 25 | 26 | Get (Next) Available IP on the Prefix 192.0.2.0/24 27 | 28 | .EXAMPLE 29 | Get-NetboxIPAMAvailableIP -Prefix_ID 2 -NumberOfIPs 3 30 | 31 | Get 3 (Next) Available IP on the Prefix 192.0.2.0/24 32 | 33 | .NOTES 34 | Additional information about the function. 35 | #> 36 | 37 | [CmdletBinding()] 38 | param 39 | ( 40 | [Parameter(Mandatory = $true, 41 | ValueFromPipelineByPropertyName = $true)] 42 | [Alias('Id')] 43 | [uint64]$Prefix_ID, 44 | 45 | [Alias('NumberOfIPs')] 46 | [uint64]$Limit, 47 | 48 | [switch]$Raw 49 | ) 50 | 51 | process { 52 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'prefixes', $Prefix_ID, 'available-ips')) 53 | 54 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'prefix_id' 55 | 56 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 57 | 58 | InvokeNetboxRequest -URI $uri -Raw:$Raw 59 | } 60 | } -------------------------------------------------------------------------------- /Functions/Circuits/Providers/Get-NetboxCircuitProvider.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxCircuitProvider { 3 | [CmdletBinding(DefaultParameterSetName = 'Query')] 4 | param 5 | ( 6 | [Parameter(ParameterSetName = 'ById', 7 | Mandatory = $true)] 8 | [uint64[]]$Id, 9 | 10 | [Parameter(ParameterSetName = 'Query', 11 | Mandatory = $false)] 12 | [string]$Name, 13 | 14 | [Parameter(ParameterSetName = 'Query')] 15 | [string]$Query, 16 | 17 | [Parameter(ParameterSetName = 'Query')] 18 | [string]$Slug, 19 | 20 | [Parameter(ParameterSetName = 'Query')] 21 | [string]$ASN, 22 | 23 | [Parameter(ParameterSetName = 'Query')] 24 | [string]$Account, 25 | 26 | [Parameter(ParameterSetName = 'Query')] 27 | [uint16]$Limit, 28 | 29 | [Parameter(ParameterSetName = 'Query')] 30 | [uint16]$Offset, 31 | 32 | [switch]$Raw 33 | ) 34 | 35 | switch ($PSCmdlet.ParameterSetName) { 36 | 'ById' { 37 | foreach ($i in $ID) { 38 | $Segments = [System.Collections.ArrayList]::new(@('circuits', 'providers', $i)) 39 | 40 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName "Id" 41 | 42 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 43 | 44 | InvokeNetboxRequest -URI $URI -Raw:$Raw 45 | } 46 | } 47 | 48 | default { 49 | $Segments = [System.Collections.ArrayList]::new(@('circuits', 'providers')) 50 | 51 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters 52 | 53 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 54 | 55 | InvokeNetboxRequest -URI $URI -Raw:$Raw 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /Functions/Virtualization/VirtualMachine/New-NetboxVirtualMachine.ps1: -------------------------------------------------------------------------------- 1 | 2 | function New-NetboxVirtualMachine { 3 | [CmdletBinding(ConfirmImpact = 'low', 4 | SupportsShouldProcess = $true)] 5 | [OutputType([pscustomobject])] 6 | param 7 | ( 8 | [Parameter(Mandatory = $true)] 9 | [string]$Name, 10 | 11 | [Parameter(Mandatory = $true)] 12 | [uint64]$Site, 13 | 14 | [uint64]$Cluster, 15 | 16 | [uint64]$Tenant, 17 | 18 | [object]$Status = 'Active', 19 | 20 | [uint64]$Role, 21 | 22 | [uint64]$Platform, 23 | 24 | [uint16]$vCPUs, 25 | 26 | [uint64]$Memory, 27 | 28 | [uint64]$Disk, 29 | 30 | [uint64]$Primary_IP4, 31 | 32 | [uint64]$Primary_IP6, 33 | 34 | [hashtable]$Custom_Fields, 35 | 36 | [string]$Comments 37 | ) 38 | 39 | # $ModelDefinition = $script:NetboxConfig.APIDefinition.definitions.WritableVirtualMachineWithConfigContext 40 | 41 | # # Validate the status against the APIDefinition 42 | # if ($ModelDefinition.properties.status.enum -inotcontains $Status) { 43 | # throw ("Invalid value [] for Status. Must be one of []" -f $Status, ($ModelDefinition.properties.status.enum -join ', ')) 44 | # } 45 | 46 | #$PSBoundParameters.Status = ValidateVirtualizationChoice -ProvidedValue $Status -VirtualMachineStatus 47 | 48 | if ($PSBoundParameters.ContainsKey('Cluster') -and (-not $PSBoundParameters.ContainsKey('Site'))) { 49 | throw "You must specify a site ID with a cluster ID" 50 | } 51 | 52 | $Segments = [System.Collections.ArrayList]::new(@('virtualization', 'virtual-machines')) 53 | 54 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 55 | 56 | $URI = BuildNewURI -Segments $URIComponents.Segments 57 | 58 | if ($PSCmdlet.ShouldProcess($name, 'Create new Virtual Machine')) { 59 | InvokeNetboxRequest -URI $URI -Method POST -Body $URIComponents.Parameters 60 | } 61 | } 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /Functions/Tenancy/Tenants/New-NetboxTenant.ps1: -------------------------------------------------------------------------------- 1 | 2 | function New-NetboxTenant { 3 | <# 4 | .SYNOPSIS 5 | Create a new tenant in Netbox 6 | 7 | .DESCRIPTION 8 | Creates a new tenant object in Netbox 9 | 10 | .PARAMETER Name 11 | The tenant name, e.g "Contoso Inc" 12 | 13 | .PARAMETER Slug 14 | The unique URL for the tenant. Can only contain hypens, A-Z, a-z, 0-9, and underscores 15 | 16 | .PARAMETER Description 17 | Short description of the tenant 18 | 19 | .PARAMETER Custom_Fields 20 | Hashtable of custom field values. 21 | 22 | .PARAMETER Raw 23 | Return the unparsed data from the HTTP request 24 | 25 | .EXAMPLE 26 | PS C:\> New-NetboxTenant -Name 'Contoso Inc' -Slug 'contoso-inc' 27 | 28 | .NOTES 29 | Additional information about the function. 30 | #> 31 | 32 | [CmdletBinding(ConfirmImpact = 'Low', 33 | SupportsShouldProcess = $true)] 34 | [OutputType([pscustomobject])] 35 | param 36 | ( 37 | [Parameter(Mandatory = $true, 38 | ValueFromPipelineByPropertyName = $true)] 39 | [ValidateLength(1, 100)] 40 | [string]$Name, 41 | 42 | [Parameter(Mandatory = $true)] 43 | [ValidateLength(1, 100)] 44 | [ValidatePattern('^[-a-zA-Z0-9_]+$')] 45 | [string]$Slug, 46 | 47 | [ValidateLength(0, 200)] 48 | [string]$Description, 49 | 50 | [hashtable]$Custom_Fields, 51 | 52 | [switch]$Raw 53 | ) 54 | 55 | process { 56 | $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'tenants')) 57 | $Method = 'POST' 58 | 59 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 60 | 61 | $URI = BuildNewURI -Segments $URIComponents.Segments 62 | 63 | if ($PSCmdlet.ShouldProcess($Address, 'Create new tenant')) { 64 | InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters -Raw:$Raw 65 | } 66 | } 67 | } 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /Functions/Tenancy/ContactRoles/New-NetboxContactRole.ps1: -------------------------------------------------------------------------------- 1 | 2 | function New-NetboxContactRole { 3 | <# 4 | .SYNOPSIS 5 | Create a new contact role in Netbox 6 | 7 | .DESCRIPTION 8 | Creates a new contact role object in Netbox 9 | 10 | .PARAMETER Name 11 | The contact role name, e.g "Network Support" 12 | 13 | .PARAMETER Slug 14 | The unique URL for the role. Can only contain hypens, A-Z, a-z, 0-9, and underscores 15 | 16 | .PARAMETER Description 17 | Short description of the contact role 18 | 19 | .PARAMETER Custom_Fields 20 | A description of the Custom_Fields parameter. 21 | 22 | .PARAMETER Raw 23 | Return the unparsed data from the HTTP request 24 | 25 | .EXAMPLE 26 | PS C:\> New-NetboxContact -Name 'Leroy Jenkins' -Email 'leroy.jenkins@example.com' 27 | 28 | .NOTES 29 | Additional information about the function. 30 | #> 31 | 32 | [CmdletBinding(ConfirmImpact = 'Low', 33 | SupportsShouldProcess = $true)] 34 | [OutputType([pscustomobject])] 35 | param 36 | ( 37 | [Parameter(Mandatory = $true, 38 | ValueFromPipelineByPropertyName = $true)] 39 | [ValidateLength(1, 100)] 40 | [string]$Name, 41 | 42 | [Parameter(Mandatory = $true)] 43 | [ValidateLength(1, 100)] 44 | [ValidatePattern('^[-a-zA-Z0-9_]+$')] 45 | [string]$Slug, 46 | 47 | [ValidateLength(0, 200)] 48 | [string]$Description, 49 | 50 | [hashtable]$Custom_Fields, 51 | 52 | [switch]$Raw 53 | ) 54 | 55 | process { 56 | $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'contacts')) 57 | $Method = 'POST' 58 | 59 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 60 | 61 | $URI = BuildNewURI -Segments $URIComponents.Segments 62 | 63 | if ($PSCmdlet.ShouldProcess($Name, 'Create new contact')) { 64 | InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters -Raw:$Raw 65 | } 66 | } 67 | } 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /Functions/IPAM/Aggregate/Get-NetboxIPAMAggregate.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxIPAMAggregate { 3 | [CmdletBinding(DefaultParameterSetName = 'Query')] 4 | param 5 | ( 6 | [Parameter(ParameterSetName = 'Query')] 7 | [string]$Query, 8 | 9 | [Parameter(ParameterSetName = 'ByID')] 10 | [uint64[]]$Id, 11 | 12 | [Parameter(ParameterSetName = 'Query')] 13 | [string]$Prefix, 14 | 15 | [Parameter(ParameterSetName = 'Query')] 16 | [object]$Family, 17 | 18 | [Parameter(ParameterSetName = 'Query')] 19 | [uint64]$RIR_Id, 20 | 21 | [Parameter(ParameterSetName = 'Query')] 22 | [string]$RIR, 23 | 24 | [Parameter(ParameterSetName = 'Query')] 25 | [datetime]$Date_Added, 26 | 27 | [Parameter(ParameterSetName = 'Query')] 28 | [uint16]$Limit, 29 | 30 | [Parameter(ParameterSetName = 'Query')] 31 | [uint16]$Offset, 32 | 33 | [switch]$Raw 34 | ) 35 | 36 | # if ($null -ne $Family) { 37 | # $PSBoundParameters.Family = ValidateIPAMChoice -ProvidedValue $Family -AggregateFamily 38 | # } 39 | 40 | switch ($PSCmdlet.ParameterSetName) { 41 | 'ById' { 42 | foreach ($IP_ID in $Id) { 43 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'aggregates', $IP_ID)) 44 | 45 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' 46 | 47 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 48 | 49 | InvokeNetboxRequest -URI $uri -Raw:$Raw 50 | } 51 | break 52 | } 53 | 54 | default { 55 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'aggregates')) 56 | 57 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 58 | 59 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 60 | 61 | InvokeNetboxRequest -URI $uri -Raw:$Raw 62 | 63 | break 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /Functions/Virtualization/VirtualMachineInterface/Get-NetboxVirtualMachineInterface.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxVirtualMachineInterface { 3 | <# 4 | .SYNOPSIS 5 | Gets VM interfaces 6 | 7 | .DESCRIPTION 8 | Obtains the interface objects for one or more VMs 9 | 10 | .PARAMETER Limit 11 | Number of results to return per page. 12 | 13 | .PARAMETER Offset 14 | The initial index from which to return the results. 15 | 16 | .PARAMETER Id 17 | Database ID of the interface 18 | 19 | .PARAMETER Name 20 | Name of the interface 21 | 22 | .PARAMETER Enabled 23 | True/False if the interface is enabled 24 | 25 | .PARAMETER MTU 26 | Maximum Transmission Unit size. Generally 1500 or 9000 27 | 28 | .PARAMETER Virtual_Machine_Id 29 | ID of the virtual machine to which the interface(s) are assigned. 30 | 31 | .PARAMETER Virtual_Machine 32 | Name of the virtual machine to get interfaces 33 | 34 | .PARAMETER MAC_Address 35 | MAC address assigned to the interface 36 | 37 | .PARAMETER Raw 38 | A description of the Raw parameter. 39 | 40 | .EXAMPLE 41 | PS C:\> Get-NetboxVirtualMachineInterface 42 | 43 | .NOTES 44 | Additional information about the function. 45 | #> 46 | 47 | [CmdletBinding()] 48 | param 49 | ( 50 | [Parameter(ValueFromPipeline = $true)] 51 | [uint64]$Id, 52 | 53 | [string]$Name, 54 | 55 | [string]$Query, 56 | 57 | [boolean]$Enabled, 58 | 59 | [uint16]$MTU, 60 | 61 | [uint64]$Virtual_Machine_Id, 62 | 63 | [string]$Virtual_Machine, 64 | 65 | [string]$MAC_Address, 66 | 67 | [uint16]$Limit, 68 | 69 | [uint16]$Offset, 70 | 71 | [switch]$Raw 72 | ) 73 | 74 | process { 75 | $Segments = [System.Collections.ArrayList]::new(@('virtualization', 'interfaces')) 76 | 77 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 78 | 79 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 80 | 81 | InvokeNetboxRequest -URI $uri -Raw:$Raw 82 | } 83 | } -------------------------------------------------------------------------------- /Functions/Virtualization/VirtualizationCluster/Get-NetboxVirtualizationCluster.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxVirtualizationCluster { 3 | <# 4 | .SYNOPSIS 5 | Obtains virtualization clusters from Netbox. 6 | 7 | .DESCRIPTION 8 | Obtains one or more virtualization clusters based on provided filters. 9 | 10 | .PARAMETER Limit 11 | Number of results to return per page 12 | 13 | .PARAMETER Offset 14 | The initial index from which to return the results 15 | 16 | .PARAMETER Query 17 | A general query used to search for a cluster 18 | 19 | .PARAMETER Name 20 | Name of the cluster 21 | 22 | .PARAMETER Id 23 | Database ID(s) of the cluster 24 | 25 | .PARAMETER Group 26 | String value of the cluster group. 27 | 28 | .PARAMETER Group_Id 29 | Database ID of the cluster group. 30 | 31 | .PARAMETER Type 32 | String value of the Cluster type. 33 | 34 | .PARAMETER Type_Id 35 | Database ID of the cluster type. 36 | 37 | .PARAMETER Site 38 | String value of the site. 39 | 40 | .PARAMETER Site_Id 41 | Database ID of the site. 42 | 43 | .PARAMETER Raw 44 | A description of the Raw parameter. 45 | 46 | .EXAMPLE 47 | PS C:\> Get-NetboxVirtualizationCluster 48 | 49 | .NOTES 50 | Additional information about the function. 51 | #> 52 | 53 | [CmdletBinding()] 54 | param 55 | ( 56 | [string]$Name, 57 | 58 | [Alias('q')] 59 | [string]$Query, 60 | 61 | [uint64[]]$Id, 62 | 63 | [string]$Group, 64 | 65 | [uint64]$Group_Id, 66 | 67 | [string]$Type, 68 | 69 | [uint64]$Type_Id, 70 | 71 | [string]$Site, 72 | 73 | [uint64]$Site_Id, 74 | 75 | [uint16]$Limit, 76 | 77 | [uint16]$Offset, 78 | 79 | [switch]$Raw 80 | ) 81 | 82 | $Segments = [System.Collections.ArrayList]::new(@('virtualization', 'clusters')) 83 | 84 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 85 | 86 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 87 | 88 | InvokeNetboxRequest -URI $uri -Raw:$Raw 89 | } -------------------------------------------------------------------------------- /Functions/Circuits/Terminations/Get-NetboxCircuitTermination.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxCircuitTermination { 3 | [CmdletBinding(DefaultParameterSetName = 'Query')] 4 | param 5 | ( 6 | [Parameter(ParameterSetName = 'ById', 7 | ValueFromPipelineByPropertyName = $true)] 8 | [uint64[]]$Id, 9 | 10 | [Parameter(ParameterSetName = 'Query')] 11 | [string]$Circuit_ID, 12 | 13 | [Parameter(ParameterSetName = 'Query')] 14 | [string]$Term_Side, 15 | 16 | [Parameter(ParameterSetName = 'Query')] 17 | [uint64]$Port_Speed, 18 | 19 | [Parameter(ParameterSetName = 'Query')] 20 | [string]$Query, 21 | 22 | [Parameter(ParameterSetName = 'Query')] 23 | [string]$Site_ID, 24 | 25 | [Parameter(ParameterSetName = 'Query')] 26 | [string]$Site, 27 | 28 | [Parameter(ParameterSetName = 'Query')] 29 | [string]$XConnect_ID, 30 | 31 | [Parameter(ParameterSetName = 'Query')] 32 | [uint16]$Limit, 33 | 34 | [Parameter(ParameterSetName = 'Query')] 35 | [uint16]$Offset, 36 | 37 | [switch]$Raw 38 | ) 39 | 40 | process { 41 | switch ($PSCmdlet.ParameterSetName) { 42 | 'ById' { 43 | foreach ($i in $ID) { 44 | $Segments = [System.Collections.ArrayList]::new(@('circuits', 'circuit-terminations', $i)) 45 | 46 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName "Id" 47 | 48 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 49 | 50 | InvokeNetboxRequest -URI $URI -Raw:$Raw 51 | } 52 | } 53 | 54 | default { 55 | $Segments = [System.Collections.ArrayList]::new(@('circuits', 'circuit-terminations')) 56 | 57 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters 58 | 59 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 60 | 61 | InvokeNetboxRequest -URI $URI -Raw:$Raw 62 | } 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /Functions/DCIM/Sites/New-NetboxDCIMSite.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .NOTES 3 | =========================================================================== 4 | Created with: SAPIEN Technologies, Inc., PowerShell Studio 2020 v5.7.181 5 | Created on: 2020-10-02 15:52 6 | Created by: Claussen 7 | Organization: NEOnet 8 | Filename: New-NetboxDCIMSite.ps1 9 | =========================================================================== 10 | .DESCRIPTION 11 | A description of the file. 12 | #> 13 | 14 | 15 | 16 | function New-NetboxDCIMSite { 17 | <# 18 | .SYNOPSIS 19 | Create a new Site to Netbox 20 | 21 | .DESCRIPTION 22 | Create a new Site to Netbox 23 | 24 | .EXAMPLE 25 | New-NetboxDCIMSite -name MySite 26 | 27 | Add new Site MySite on Netbox 28 | 29 | #> 30 | 31 | [CmdletBinding(ConfirmImpact = 'Low', 32 | SupportsShouldProcess = $true)] 33 | [OutputType([pscustomobject])] 34 | param 35 | ( 36 | [Parameter(Mandatory = $true)] 37 | [string]$Name, 38 | 39 | [string]$Slug, 40 | 41 | [string]$Facility, 42 | 43 | [uint64]$ASN, 44 | 45 | [decimal]$Latitude, 46 | 47 | [decimal]$Longitude, 48 | 49 | [string]$Contact_Name, 50 | 51 | [string]$Contact_Phone, 52 | 53 | [string]$Contact_Email, 54 | 55 | [uint64]$Tenant_Group, 56 | 57 | [uint64]$Tenant, 58 | 59 | [string]$Status, 60 | 61 | [uint64]$Region, 62 | 63 | [string]$Description, 64 | 65 | [string]$Comments, 66 | 67 | [switch]$Raw 68 | ) 69 | 70 | process { 71 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'sites')) 72 | $Method = 'POST' 73 | 74 | if (-not $PSBoundParameters.ContainsKey('slug')) { 75 | $PSBoundParameters.Add('slug', $name) 76 | } 77 | 78 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 79 | 80 | $URI = BuildNewURI -Segments $URIComponents.Segments 81 | 82 | if ($PSCmdlet.ShouldProcess($name, 'Create new Site')) { 83 | InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters -Raw:$Raw 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Functions/IPAM/VLAN/New-NetboxIPAMVLAN.ps1: -------------------------------------------------------------------------------- 1 | function New-NetboxIPAMVLAN { 2 | <# 3 | .SYNOPSIS 4 | Create a new VLAN 5 | 6 | .DESCRIPTION 7 | Create a new VLAN in Netbox with a status of Active by default. 8 | 9 | .PARAMETER VID 10 | The VLAN ID. 11 | 12 | .PARAMETER Name 13 | The name of the VLAN. 14 | 15 | .PARAMETER Status 16 | Status of the VLAN. Defaults to Active 17 | 18 | .PARAMETER Tenant 19 | Tenant ID 20 | 21 | .PARAMETER Role 22 | Role such as anycast, loopback, etc... Defaults to nothing 23 | 24 | .PARAMETER Description 25 | Description of IP address 26 | 27 | .PARAMETER Custom_Fields 28 | Custom field hash table. Will be validated by the API service 29 | 30 | .PARAMETER Raw 31 | Return raw results from API service 32 | 33 | .PARAMETER Address 34 | IP address in CIDR notation: 192.168.1.1/24 35 | 36 | .EXAMPLE 37 | PS C:\> Create-NetboxIPAMAddress 38 | 39 | .NOTES 40 | Additional information about the function. 41 | #> 42 | 43 | [CmdletBinding(ConfirmImpact = 'low', 44 | SupportsShouldProcess = $true)] 45 | [OutputType([pscustomobject])] 46 | param 47 | ( 48 | [Parameter(Mandatory = $true)] 49 | [uint16]$VID, 50 | 51 | [Parameter(Mandatory = $true)] 52 | [string]$Name, 53 | 54 | [object]$Status = 'Active', 55 | 56 | [uint64]$Tenant, 57 | 58 | [object]$Role, 59 | 60 | [string]$Description, 61 | 62 | [hashtable]$Custom_Fields, 63 | 64 | [switch]$Raw 65 | ) 66 | 67 | # $PSBoundParameters.Status = ValidateIPAMChoice -ProvidedValue $Status -VLANStatus 68 | 69 | # if ($null -ne $Role) { 70 | # $PSBoundParameters.Role = ValidateIPAMChoice -ProvidedValue $Role -IPAddressRole 71 | # } 72 | 73 | $segments = [System.Collections.ArrayList]::new(@('ipam', 'vlans')) 74 | 75 | $URIComponents = BuildURIComponents -URISegments $segments -ParametersDictionary $PSBoundParameters 76 | 77 | $URI = BuildNewURI -Segments $URIComponents.Segments 78 | 79 | if ($PSCmdlet.ShouldProcess($nae, 'Create new Vlan $($vid)')) { 80 | InvokeNetboxRequest -URI $URI -Method POST -Body $URIComponents.Parameters -Raw:$Raw 81 | } 82 | } -------------------------------------------------------------------------------- /Functions/DCIM/Interfaces/Set-NetboxDCIMInterfaceConnection.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Set-NetboxDCIMInterfaceConnection { 3 | <# 4 | .SYNOPSIS 5 | Update an interface connection 6 | 7 | .DESCRIPTION 8 | Update an interface connection 9 | 10 | .PARAMETER Id 11 | A description of the Id parameter. 12 | 13 | .PARAMETER Connection_Status 14 | A description of the Connection_Status parameter. 15 | 16 | .PARAMETER Interface_A 17 | A description of the Interface_A parameter. 18 | 19 | .PARAMETER Interface_B 20 | A description of the Interface_B parameter. 21 | 22 | .PARAMETER Force 23 | A description of the Force parameter. 24 | 25 | .EXAMPLE 26 | PS C:\> Set-NetboxDCIMInterfaceConnection -Id $value1 27 | 28 | .NOTES 29 | Additional information about the function. 30 | #> 31 | 32 | [CmdletBinding(ConfirmImpact = 'Medium', 33 | SupportsShouldProcess = $true)] 34 | param 35 | ( 36 | [Parameter(Mandatory = $true, 37 | ValueFromPipelineByPropertyName = $true)] 38 | [uint64[]]$Id, 39 | 40 | [object]$Connection_Status, 41 | 42 | [uint64]$Interface_A, 43 | 44 | [uint64]$Interface_B, 45 | 46 | [switch]$Force 47 | ) 48 | 49 | begin { 50 | if ((@($ID).Count -gt 1) -and ($Interface_A -or $Interface_B)) { 51 | throw "Cannot set multiple connections to the same interface" 52 | } 53 | } 54 | 55 | process { 56 | foreach ($ConnectionID in $Id) { 57 | $CurrentConnection = Get-NetboxDCIMInterfaceConnection -Id $ConnectionID -ErrorAction Stop 58 | 59 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'interface-connections', $CurrentConnection.Id)) 60 | 61 | if ($Force -or $pscmdlet.ShouldProcess("Connection ID $($CurrentConnection.Id)", "Set")) { 62 | 63 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force' 64 | 65 | $URI = BuildNewURI -Segments $URIComponents.Segments 66 | 67 | InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method PATCH 68 | } 69 | } 70 | } 71 | 72 | end { 73 | 74 | } 75 | } -------------------------------------------------------------------------------- /Functions/IPAM/Range/Get-NetboxIPAMAddressRange.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxIPAMAddressRange { 3 | [CmdletBinding(DefaultParameterSetName = 'Query')] 4 | param 5 | ( 6 | [Parameter(ParameterSetName = 'Query', 7 | Position = 0)] 8 | [string]$Range, 9 | 10 | [Parameter(ParameterSetName = 'ByID')] 11 | [uint64[]]$Id, 12 | 13 | [Parameter(ParameterSetName = 'Query')] 14 | [string]$Query, 15 | 16 | [Parameter(ParameterSetName = 'Query')] 17 | [object]$Family, 18 | 19 | [Parameter(ParameterSetName = 'Query')] 20 | [string]$VRF, 21 | 22 | [Parameter(ParameterSetName = 'Query')] 23 | [uint32]$VRF_Id, 24 | 25 | [Parameter(ParameterSetName = 'Query')] 26 | [string]$Tenant, 27 | 28 | [Parameter(ParameterSetName = 'Query')] 29 | [uint32]$Tenant_Id, 30 | 31 | [Parameter(ParameterSetName = 'Query')] 32 | [object]$Status, 33 | 34 | [Parameter(ParameterSetName = 'Query')] 35 | [object]$Role, 36 | 37 | [Parameter(ParameterSetName = 'Query')] 38 | [uint16]$Limit, 39 | 40 | [Parameter(ParameterSetName = 'Query')] 41 | [uint16]$Offset, 42 | 43 | [switch]$Raw 44 | ) 45 | 46 | switch ($PSCmdlet.ParameterSetName) { 47 | 'ById' { 48 | foreach ($Range_ID in $Id) { 49 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-ranges', $Range_ID)) 50 | 51 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' 52 | 53 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 54 | 55 | InvokeNetboxRequest -URI $uri -Raw:$Raw 56 | } 57 | 58 | break 59 | } 60 | 61 | default { 62 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-ranges')) 63 | 64 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 65 | 66 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 67 | 68 | InvokeNetboxRequest -URI $uri -Raw:$Raw 69 | 70 | break 71 | } 72 | } 73 | } -------------------------------------------------------------------------------- /Functions/Helpers/BuildNewURI.ps1: -------------------------------------------------------------------------------- 1 | 2 | function BuildNewURI { 3 | <# 4 | .SYNOPSIS 5 | Create a new URI for Netbox 6 | 7 | .DESCRIPTION 8 | Internal function used to build a URIBuilder object. 9 | 10 | .PARAMETER Hostname 11 | Hostname of the Netbox API 12 | 13 | .PARAMETER Segments 14 | Array of strings for each segment in the URL path 15 | 16 | .PARAMETER Parameters 17 | Hashtable of query parameters to include 18 | 19 | .PARAMETER HTTPS 20 | Whether to use HTTPS or HTTP 21 | 22 | .PARAMETER Port 23 | A description of the Port parameter. 24 | 25 | .PARAMETER APIInfo 26 | A description of the APIInfo parameter. 27 | 28 | .EXAMPLE 29 | PS C:\> BuildNewURI 30 | 31 | .NOTES 32 | Additional information about the function. 33 | #> 34 | 35 | [CmdletBinding()] 36 | [OutputType([System.UriBuilder])] 37 | param 38 | ( 39 | [Parameter(Mandatory = $false)] 40 | [string[]]$Segments, 41 | 42 | [Parameter(Mandatory = $false)] 43 | [hashtable]$Parameters, 44 | 45 | [switch]$SkipConnectedCheck 46 | ) 47 | 48 | Write-Verbose "Building URI" 49 | 50 | if (-not $SkipConnectedCheck) { 51 | # There is no point in continuing if we have not successfully connected to an API 52 | $null = CheckNetboxIsConnected 53 | } 54 | 55 | # Begin a URI builder with HTTP/HTTPS and the provided hostname 56 | $uriBuilder = [System.UriBuilder]::new($script:NetboxConfig.HostScheme, $script:NetboxConfig.Hostname, $script:NetboxConfig.HostPort) 57 | 58 | # Generate the path by trimming excess slashes and whitespace from the $segments[] and joining together 59 | $uriBuilder.Path = "api/{0}/" -f ($Segments.ForEach({ 60 | $_.trim('/').trim() 61 | }) -join '/') 62 | 63 | Write-Verbose " URIPath: $($uriBuilder.Path)" 64 | 65 | if ($parameters) { 66 | # Loop through the parameters and use the HttpUtility to create a Query string 67 | [System.Collections.Specialized.NameValueCollection]$URIParams = [System.Web.HttpUtility]::ParseQueryString([String]::Empty) 68 | 69 | foreach ($param in $Parameters.GetEnumerator()) { 70 | Write-Verbose " Adding URI parameter $($param.Key):$($param.Value)" 71 | $URIParams[$param.Key] = $param.Value 72 | } 73 | 74 | $uriBuilder.Query = $URIParams.ToString() 75 | } 76 | 77 | Write-Verbose " Completed building URIBuilder" 78 | # Return the entire UriBuilder object 79 | $uriBuilder 80 | } -------------------------------------------------------------------------------- /Functions/Tenancy/ContactRoles/Set-NetboxContactRole.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Set-NetboxContactRole { 3 | <# 4 | .SYNOPSIS 5 | Update a contact role in Netbox 6 | 7 | .DESCRIPTION 8 | Updates a contact role in Netbox 9 | 10 | .PARAMETER Name 11 | The contact role name, e.g "Network Support" 12 | 13 | .PARAMETER Slug 14 | The unique URL for the role. Can only contain hypens, A-Z, a-z, 0-9, and underscores 15 | 16 | .PARAMETER Description 17 | Short description of the contact role 18 | 19 | .PARAMETER Custom_Fields 20 | A description of the Custom_Fields parameter. 21 | 22 | .PARAMETER Raw 23 | Return the unparsed data from the HTTP request 24 | 25 | .EXAMPLE 26 | PS C:\> New-NetboxContact -Name 'Leroy Jenkins' -Email 'leroy.jenkins@example.com' 27 | 28 | .NOTES 29 | Additional information about the function. 30 | #> 31 | 32 | [CmdletBinding(ConfirmImpact = 'Low', 33 | SupportsShouldProcess = $true)] 34 | [OutputType([pscustomobject])] 35 | param 36 | ( 37 | [Parameter(Mandatory = $true, 38 | ValueFromPipelineByPropertyName = $true)] 39 | [uint64[]]$Id, 40 | 41 | [Parameter(ValueFromPipelineByPropertyName = $true)] 42 | [ValidateLength(1, 100)] 43 | [string]$Name, 44 | 45 | [ValidateLength(1, 100)] 46 | [ValidatePattern('^[-a-zA-Z0-9_]+$')] 47 | [string]$Slug, 48 | 49 | [ValidateLength(0, 200)] 50 | [string]$Description, 51 | 52 | [hashtable]$Custom_Fields, 53 | 54 | [switch]$Raw 55 | ) 56 | 57 | begin { 58 | $Method = 'PATCH' 59 | } 60 | 61 | process { 62 | foreach ($ContactRoleId in $Id) { 63 | $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'contacts', $ContactRoleId)) 64 | 65 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force' 66 | 67 | $URI = BuildNewURI -Segments $URIComponents.Segments 68 | 69 | $CurrentContactRole = Get-NetboxContactRole -Id $ContactRoleId -ErrorAction Stop 70 | 71 | if ($Force -or $PSCmdlet.ShouldProcess($CurrentContactRole.Name, 'Update contact role')) { 72 | InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters -Raw:$Raw 73 | } 74 | } 75 | } 76 | } 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /Tests/integration/DCIM.Site.Tests.ps1: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2021, Alexis La Goutte 3 | # 4 | # SPDX-License-Identifier: Apache-2.0 5 | # 6 | . ../common.ps1 7 | 8 | BeforeAll { 9 | Connect-NetboxAPI @invokeParams 10 | } 11 | 12 | Describe "Get (DCIM) Site" { 13 | 14 | BeforeAll { 15 | New-NetboxDCIMSite -name $pester_site1 16 | } 17 | 18 | It "Get Site Does not throw an error" { 19 | { 20 | Get-NetboxDCIMSite 21 | } | Should -Not -Throw 22 | } 23 | 24 | It "Get ALL Site" { 25 | $site = Get-NetboxDCIMSite 26 | $site.count | Should -Not -Be $NULL 27 | } 28 | 29 | It "Get Site ($pester_site1)" { 30 | $site = Get-NetboxDCIMSite | Where-Object { $_.name -eq $pester_site1 } 31 | $site.id | Should -Not -BeNullOrEmpty 32 | $site.name | Should -Be $pester_site1 33 | $site.status.value | Should -Be "active" 34 | } 35 | 36 | It "Search Site by name ($pester_site1)" { 37 | $site = Get-NetboxDCIMSite -name $pester_site1 38 | @($site).count | Should -Be 1 39 | $site.id | Should -Not -BeNullOrEmpty 40 | $site.name | Should -Be $pester_site1 41 | } 42 | 43 | AfterAll { 44 | Get-NetboxDCIMSite -name $pester_site1 | Remove-NetboxDCIMSite -confirm:$false 45 | } 46 | } 47 | 48 | Describe "New (DCIM) Site" { 49 | 50 | It "New Site with no option" { 51 | New-NetboxDCIMSite -name $pester_site1 52 | $site = Get-NetboxDCIMSite -name $pester_site1 53 | $site.id | Should -Not -BeNullOrEmpty 54 | $site.name | Should -Be $pester_site1 55 | $site.slug | Should -Be $pester_site1 56 | } 57 | 58 | It "New Site with different slug" { 59 | New-NetboxDCIMSite -name $pester_site1 -slug pester_slug 60 | $site = Get-NetboxDCIMSite -name $pester_site1 61 | $site.id | Should -Not -BeNullOrEmpty 62 | $site.name | Should -Be $pester_site1 63 | $site.slug | Should -Be "pester_slug" 64 | } 65 | 66 | AfterEach { 67 | Get-NetboxDCIMSite -name $pester_site1 | Remove-NetboxDCIMSite -confirm:$false 68 | } 69 | } 70 | 71 | Describe "Remove Site" { 72 | 73 | BeforeEach { 74 | New-NetboxDCIMSite -name $pester_site1 75 | } 76 | 77 | It "Remove Site" { 78 | $site = Get-NetboxDCIMSite -name $pester_site1 79 | Remove-NetboxDCIMSite -id $site.id -confirm:$false 80 | $site = Get-NetboxDCIMSite -name $pester_site1 81 | $site | Should -BeNullOrEmpty 82 | @($site).count | Should -Be 0 83 | } 84 | 85 | } -------------------------------------------------------------------------------- /Functions/DCIM/Interfaces/Get-NetboxDCIMInterface.ps1: -------------------------------------------------------------------------------- 1 | function Get-NetboxDCIMInterface { 2 | [CmdletBinding()] 3 | [OutputType([pscustomobject])] 4 | param 5 | ( 6 | [uint16]$Limit, 7 | 8 | [uint16]$Offset, 9 | 10 | [Parameter(ValueFromPipelineByPropertyName = $true)] 11 | [uint64]$Id, 12 | 13 | [uint64]$Name, 14 | 15 | [object]$Form_Factor, 16 | 17 | [bool]$Enabled, 18 | 19 | [uint16]$MTU, 20 | 21 | [bool]$MGMT_Only, 22 | 23 | [string]$Device, 24 | 25 | [uint64]$Device_Id, 26 | 27 | [ValidateSet('virtual', 'bridge', 'lag', '100base-tx', '1000base-t', '2.5gbase-t', '5gbase-t', '10gbase-t', '10gbase-cx4', '1000base-x-gbic', '1000base-x-sfp', '10gbase-x-sfpp', '10gbase-x-xfp', '10gbase-x-xenpak', '10gbase-x-x2', '25gbase-x-sfp28', '50gbase-x-sfp56', '40gbase-x-qsfpp', '50gbase-x-sfp28', '100gbase-x-cfp', '100gbase-x-cfp2', '200gbase-x-cfp2', '100gbase-x-cfp4', '100gbase-x-cpak', '100gbase-x-qsfp28', '200gbase-x-qsfp56', '400gbase-x-qsfpdd', '400gbase-x-osfp', '1000base-kx', '10gbase-kr', '10gbase-kx4', '25gbase-kr', '40gbase-kr4', '50gbase-kr', '100gbase-kp4', '100gbase-kr2', '100gbase-kr4', 'ieee802.11a', 'ieee802.11g', 'ieee802.11n', 'ieee802.11ac', 'ieee802.11ad', 'ieee802.11ax', 'ieee802.11ay', 'ieee802.15.1', 'other-wireless', 'gsm', 'cdma', 'lte', 'sonet-oc3', 'sonet-oc12', 'sonet-oc48', 'sonet-oc192', 'sonet-oc768', 'sonet-oc1920', 'sonet-oc3840', '1gfc-sfp', '2gfc-sfp', '4gfc-sfp', '8gfc-sfpp', '16gfc-sfpp', '32gfc-sfp28', '64gfc-qsfpp', '128gfc-qsfp28', 'infiniband-sdr', 'infiniband-ddr', 'infiniband-qdr', 'infiniband-fdr10', 'infiniband-fdr', 'infiniband-edr', 'infiniband-hdr', 'infiniband-ndr', 'infiniband-xdr', 't1', 'e1', 't3', 'e3', 'xdsl', 'docsis', 'gpon', 'xg-pon', 'xgs-pon', 'ng-pon2', 'epon', '10g-epon', 'cisco-stackwise', 'cisco-stackwise-plus', 'cisco-flexstack', 'cisco-flexstack-plus', 'cisco-stackwise-80', 'cisco-stackwise-160', 'cisco-stackwise-320', 'cisco-stackwise-480', 'juniper-vcp', 'extreme-summitstack', 'extreme-summitstack-128', 'extreme-summitstack-256', 'extreme-summitstack-512', 'other', IgnoreCase = $true)] 28 | [string]$Type, 29 | 30 | [uint64]$LAG_Id, 31 | 32 | [string]$MAC_Address, 33 | 34 | [switch]$Raw 35 | ) 36 | 37 | process { 38 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'interfaces')) 39 | 40 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters 41 | 42 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 43 | 44 | InvokeNetboxRequest -URI $URI -Raw:$Raw 45 | } 46 | } -------------------------------------------------------------------------------- /Functions/Tenancy/ContactAssignment/New-NetboxContactAssignment.ps1: -------------------------------------------------------------------------------- 1 | 2 | function New-NetboxContactAssignment { 3 | <# 4 | .SYNOPSIS 5 | Create a new contact role assignment in Netbox 6 | 7 | .DESCRIPTION 8 | Creates a new contact role assignment in Netbox 9 | 10 | .PARAMETER Content_Type 11 | The content type for this assignment. 12 | 13 | .PARAMETER Object_Id 14 | ID of the object to assign. 15 | 16 | .PARAMETER Contact 17 | ID of the contact to assign. 18 | 19 | .PARAMETER Role 20 | ID of the contact role to assign. 21 | 22 | .PARAMETER Priority 23 | Piority of the contact assignment. 24 | 25 | .PARAMETER Raw 26 | Return the unparsed data from the HTTP request 27 | 28 | .EXAMPLE 29 | PS C:\> New-NetboxContactAssignment -Content_Type 'dcim.location' -Object_id 10 -Contact 15 -Role 10 -Priority 'Primary' 30 | 31 | .NOTES 32 | Valid content types: https://docs.netbox.dev/en/stable/features/contacts/#contacts_1 33 | #> 34 | 35 | [CmdletBinding(ConfirmImpact = 'Low', 36 | SupportsShouldProcess = $true)] 37 | [OutputType([pscustomobject])] 38 | param 39 | ( 40 | [Parameter(Mandatory = $true, 41 | ValueFromPipelineByPropertyName = $true)] 42 | [ValidateSet('circuits.circuit', 'circuits.provider', 'circuits.provideraccount', 'dcim.device', 'dcim.location', 'dcim.manufacturer', 'dcim.powerpanel', 'dcim.rack', 'dcim.region', 'dcim.site', 'dcim.sitegroup', 'tenancy.tenant', 'virtualization.cluster', 'virtualization.clustergroup', 'virtualization.virtualmachine', IgnoreCase = $true)] 43 | [string]$Content_Type, 44 | 45 | [Parameter(Mandatory = $true)] 46 | [uint64]$Object_Id, 47 | 48 | [Parameter(Mandatory = $true)] 49 | [uint64]$Contact, 50 | 51 | [Parameter(Mandatory = $true)] 52 | [uint64]$Role, 53 | 54 | [ValidateSet('primary', 'secondary', 'tertiary', 'inactive', IgnoreCase = $true)] 55 | [string]$Priority, 56 | 57 | [switch]$Raw 58 | ) 59 | 60 | begin { 61 | $Method = 'POST' 62 | } 63 | 64 | process { 65 | $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'contact-assignments')) 66 | 67 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 68 | 69 | $URI = BuildNewURI -Segments $URIComponents.Segments 70 | 71 | if ($PSCmdlet.ShouldProcess($Content_Type, 'Create new contact assignment')) { 72 | InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters -Raw:$Raw 73 | } 74 | } 75 | } 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /Functions/IPAM/Prefix/Set-NetboxIPAMPrefix.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Set-NetboxIPAMPrefix { 3 | [CmdletBinding(ConfirmImpact = 'Medium', 4 | SupportsShouldProcess = $true)] 5 | param 6 | ( 7 | [Parameter(Mandatory = $true, 8 | ValueFromPipelineByPropertyName = $true)] 9 | [uint64[]]$Id, 10 | 11 | [string]$Prefix, 12 | 13 | [string]$Status, 14 | 15 | [uint64]$Tenant, 16 | 17 | [uint64]$Site, 18 | 19 | [uint64]$VRF, 20 | 21 | [uint64]$VLAN, 22 | 23 | [object]$Role, 24 | 25 | [hashtable]$Custom_Fields, 26 | 27 | [string]$Description, 28 | 29 | [switch]$Is_Pool, 30 | 31 | [switch]$Force 32 | ) 33 | 34 | begin { 35 | # Write-Verbose "Validating enum properties" 36 | # $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-addresses', 0)) 37 | $Method = 'PATCH' 38 | # 39 | # # Value validation 40 | # $ModelDefinition = GetModelDefinitionFromURIPath -Segments $Segments -Method $Method 41 | # $EnumProperties = GetModelEnumProperties -ModelDefinition $ModelDefinition 42 | # 43 | # foreach ($Property in $EnumProperties.Keys) { 44 | # if ($PSBoundParameters.ContainsKey($Property)) { 45 | # Write-Verbose "Validating property [$Property] with value [$($PSBoundParameters.$Property)]" 46 | # $PSBoundParameters.$Property = ValidateValue -ModelDefinition $ModelDefinition -Property $Property -ProvidedValue $PSBoundParameters.$Property 47 | # } else { 48 | # Write-Verbose "User did not provide a value for [$Property]" 49 | # } 50 | # } 51 | # 52 | # Write-Verbose "Finished enum validation" 53 | } 54 | 55 | process { 56 | foreach ($PrefixId in $Id) { 57 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'prefixes', $PrefixId)) 58 | 59 | Write-Verbose "Obtaining Prefix from ID $PrefixId" 60 | $CurrentPrefix = Get-NetboxIPAMPrefix -Id $PrefixId -ErrorAction Stop 61 | 62 | if ($Force -or $PSCmdlet.ShouldProcess($CurrentPrefix.Prefix, 'Set')) { 63 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force' 64 | 65 | $URI = BuildNewURI -Segments $URIComponents.Segments 66 | 67 | InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method $Method 68 | } 69 | } 70 | } 71 | } 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /Functions/IPAM/Role/Get-NetboxIPAMRole.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxIPAMRole { 3 | <# 4 | .SYNOPSIS 5 | Get IPAM Prefix/VLAN roles 6 | 7 | .DESCRIPTION 8 | A role indicates the function of a prefix or VLAN. For example, you might define Data, Voice, and Security roles. Generally, a prefix will be assigned the same functional role as the VLAN to which it is assigned (if any). 9 | 10 | .PARAMETER Id 11 | Unique ID 12 | 13 | .PARAMETER Query 14 | Search query 15 | 16 | .PARAMETER Name 17 | Role name 18 | 19 | .PARAMETER Slug 20 | Role URL slug 21 | 22 | .PARAMETER Brief 23 | Brief format 24 | 25 | .PARAMETER Limit 26 | Result limit 27 | 28 | .PARAMETER Offset 29 | Result offset 30 | 31 | .PARAMETER Raw 32 | A description of the Raw parameter. 33 | 34 | .EXAMPLE 35 | PS C:\> Get-NetboxIPAMRole 36 | 37 | .NOTES 38 | Additional information about the function. 39 | #> 40 | 41 | [CmdletBinding()] 42 | param 43 | ( 44 | [Parameter(ParameterSetName = 'Query', 45 | Position = 0)] 46 | [string]$Name, 47 | 48 | [Parameter(ParameterSetName = 'Query')] 49 | [string]$Query, 50 | 51 | [Parameter(ParameterSetName = 'ByID')] 52 | [uint64[]]$Id, 53 | 54 | [Parameter(ParameterSetName = 'Query')] 55 | [string]$Slug, 56 | 57 | [Parameter(ParameterSetName = 'Query')] 58 | [switch]$Brief, 59 | 60 | [Parameter(ParameterSetName = 'Query')] 61 | [uint16]$Limit, 62 | 63 | [Parameter(ParameterSetName = 'Query')] 64 | [uint16]$Offset, 65 | 66 | [switch]$Raw 67 | ) 68 | 69 | switch ($PSCmdlet.ParameterSetName) { 70 | 'ById' { 71 | foreach ($Role_ID in $Id) { 72 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'roles', $Role_ID)) 73 | 74 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' 75 | 76 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 77 | 78 | InvokeNetboxRequest -URI $uri -Raw:$Raw 79 | } 80 | 81 | break 82 | } 83 | 84 | default { 85 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'roles')) 86 | 87 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 88 | 89 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 90 | 91 | InvokeNetboxRequest -URI $uri -Raw:$Raw 92 | 93 | break 94 | } 95 | } 96 | } -------------------------------------------------------------------------------- /Functions/Setup/Support/Get-NetboxContentType.ps1: -------------------------------------------------------------------------------- 1 | function Get-NetboxContentType { 2 | <# 3 | .SYNOPSIS 4 | Get a content type definition from Netbox 5 | 6 | .DESCRIPTION 7 | A detailed description of the Get-NetboxContentType function. 8 | 9 | .PARAMETER Model 10 | A description of the Model parameter. 11 | 12 | .PARAMETER Id 13 | The database ID of the contact role 14 | 15 | .PARAMETER App_Label 16 | A description of the App_Label parameter. 17 | 18 | .PARAMETER Query 19 | A standard search query that will match one or more contact roles. 20 | 21 | .PARAMETER Limit 22 | Limit the number of results to this number 23 | 24 | .PARAMETER Offset 25 | Start the search at this index in results 26 | 27 | .PARAMETER Raw 28 | Return the unparsed data from the HTTP request 29 | 30 | .EXAMPLE 31 | PS C:\> Get-NetboxContentType 32 | 33 | .NOTES 34 | Additional information about the function. 35 | #> 36 | 37 | [CmdletBinding(DefaultParameterSetName = 'Query')] 38 | param 39 | ( 40 | [Parameter(ParameterSetName = 'Query', 41 | Position = 0)] 42 | [string]$Model, 43 | 44 | [Parameter(ParameterSetName = 'ByID')] 45 | [uint64[]]$Id, 46 | 47 | [Parameter(ParameterSetName = 'Query')] 48 | [string]$App_Label, 49 | 50 | [Parameter(ParameterSetName = 'Query')] 51 | [string]$Query, 52 | 53 | [Parameter(ParameterSetName = 'Query')] 54 | [uint16]$Limit, 55 | 56 | [Parameter(ParameterSetName = 'Query')] 57 | [uint16]$Offset, 58 | 59 | [switch]$Raw 60 | ) 61 | 62 | switch ($PSCmdlet.ParameterSetName) { 63 | 'ById' { 64 | foreach ($ContentType_ID in $Id) { 65 | $Segments = [System.Collections.ArrayList]::new(@('extras', 'content-types', $ContentType_ID)) 66 | 67 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' 68 | 69 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 70 | 71 | InvokeNetboxRequest -URI $uri -Raw:$Raw 72 | } 73 | 74 | break 75 | } 76 | 77 | default { 78 | $Segments = [System.Collections.ArrayList]::new(@('extras', 'content-types')) 79 | 80 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 81 | 82 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 83 | 84 | InvokeNetboxRequest -URI $uri -Raw:$Raw 85 | 86 | break 87 | } 88 | } 89 | } -------------------------------------------------------------------------------- /Functions/Tenancy/ContactRoles/Get-NetboxContactRole.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxContactRole { 3 | <# 4 | .SYNOPSIS 5 | Get a contact role from Netbox 6 | 7 | .DESCRIPTION 8 | A detailed description of the Get-NetboxContactRole function. 9 | 10 | .PARAMETER Name 11 | The specific name of the contact role. Must match exactly as is defined in Netbox 12 | 13 | .PARAMETER Id 14 | The database ID of the contact role 15 | 16 | .PARAMETER Query 17 | A standard search query that will match one or more contact roles. 18 | 19 | .PARAMETER Limit 20 | Limit the number of results to this number 21 | 22 | .PARAMETER Offset 23 | Start the search at this index in results 24 | 25 | .PARAMETER Raw 26 | Return the unparsed data from the HTTP request 27 | 28 | .EXAMPLE 29 | PS C:\> Get-NetboxContactRole 30 | 31 | .NOTES 32 | Additional information about the function. 33 | #> 34 | 35 | [CmdletBinding(DefaultParameterSetName = 'Query')] 36 | param 37 | ( 38 | [Parameter(ParameterSetName = 'Query', 39 | Position = 0)] 40 | [string]$Name, 41 | 42 | [Parameter(ParameterSetName = 'ByID')] 43 | [uint64[]]$Id, 44 | 45 | [Parameter(ParameterSetName = 'Query')] 46 | [string]$Query, 47 | 48 | [Parameter(ParameterSetName = 'Query')] 49 | [string]$Slug, 50 | 51 | [Parameter(ParameterSetName = 'Query')] 52 | [string]$Description, 53 | 54 | [Parameter(ParameterSetName = 'Query')] 55 | [uint16]$Limit, 56 | 57 | [Parameter(ParameterSetName = 'Query')] 58 | [uint16]$Offset, 59 | 60 | [switch]$Raw 61 | ) 62 | 63 | switch ($PSCmdlet.ParameterSetName) { 64 | 'ById' { 65 | foreach ($ContactRole_ID in $Id) { 66 | $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'contact-roles', $ContactRole_ID)) 67 | 68 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' 69 | 70 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 71 | 72 | InvokeNetboxRequest -URI $uri -Raw:$Raw 73 | } 74 | 75 | break 76 | } 77 | 78 | default { 79 | $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'contact-roles')) 80 | 81 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 82 | 83 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 84 | 85 | InvokeNetboxRequest -URI $uri -Raw:$Raw 86 | 87 | break 88 | } 89 | } 90 | } -------------------------------------------------------------------------------- /Functions/Tenancy/Contacts/New-NetboxContact.ps1: -------------------------------------------------------------------------------- 1 | 2 | function New-NetboxContact { 3 | <# 4 | .SYNOPSIS 5 | Create a new contact in Netbox 6 | 7 | .DESCRIPTION 8 | Creates a new contact object in Netbox which can be linked to other objects 9 | 10 | .PARAMETER Name 11 | The contacts full name, e.g "Leroy Jenkins" 12 | 13 | .PARAMETER Email 14 | Email address of the contact 15 | 16 | .PARAMETER Title 17 | Job title or other title related to the contact 18 | 19 | .PARAMETER Phone 20 | Telephone number 21 | 22 | .PARAMETER Address 23 | Physical address, usually mailing address 24 | 25 | .PARAMETER Description 26 | Short description of the contact 27 | 28 | .PARAMETER Comments 29 | Detailed comments. Markdown supported. 30 | 31 | .PARAMETER Link 32 | URI related to the contact 33 | 34 | .PARAMETER Custom_Fields 35 | A description of the Custom_Fields parameter. 36 | 37 | .PARAMETER Raw 38 | A description of the Raw parameter. 39 | 40 | .EXAMPLE 41 | PS C:\> New-NetboxContact -Name 'Leroy Jenkins' -Email 'leroy.jenkins@example.com' 42 | 43 | .NOTES 44 | Additional information about the function. 45 | #> 46 | 47 | [CmdletBinding(ConfirmImpact = 'Low', 48 | SupportsShouldProcess = $true)] 49 | [OutputType([pscustomobject])] 50 | param 51 | ( 52 | [Parameter(Mandatory = $true, 53 | ValueFromPipelineByPropertyName = $true)] 54 | [ValidateLength(1, 100)] 55 | [string]$Name, 56 | 57 | [Parameter(Mandatory = $true)] 58 | [ValidateLength(0, 254)] 59 | [string]$Email, 60 | 61 | [ValidateLength(0, 100)] 62 | [string]$Title, 63 | 64 | [ValidateLength(0, 50)] 65 | [string]$Phone, 66 | 67 | [ValidateLength(0, 200)] 68 | [string]$Address, 69 | 70 | [ValidateLength(0, 200)] 71 | [string]$Description, 72 | 73 | [string]$Comments, 74 | 75 | [ValidateLength(0, 200)] 76 | [string]$Link, 77 | 78 | [hashtable]$Custom_Fields, 79 | 80 | [switch]$Raw 81 | ) 82 | 83 | process { 84 | $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'contacts')) 85 | $Method = 'POST' 86 | 87 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 88 | 89 | $URI = BuildNewURI -Segments $URIComponents.Segments 90 | 91 | if ($PSCmdlet.ShouldProcess($Name, 'Create new contact')) { 92 | InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters -Raw:$Raw 93 | } 94 | } 95 | } 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /Functions/Helpers/BuildURIComponents.ps1: -------------------------------------------------------------------------------- 1 | 2 | function BuildURIComponents { 3 | [CmdletBinding()] 4 | [OutputType([hashtable])] 5 | param 6 | ( 7 | [Parameter(Mandatory = $true)] 8 | [System.Collections.ArrayList]$URISegments, 9 | 10 | [Parameter(Mandatory = $true)] 11 | [object]$ParametersDictionary, 12 | 13 | [string[]]$SkipParameterByName 14 | ) 15 | 16 | Write-Verbose "Building URI components" 17 | 18 | $URIParameters = @{ 19 | } 20 | 21 | foreach ($CmdletParameterName in $ParametersDictionary.Keys) { 22 | if ($CmdletParameterName -in $script:CommonParameterNames) { 23 | # These are common parameters and should not be appended to the URI 24 | Write-Debug "Skipping common parameter $CmdletParameterName" 25 | continue 26 | } 27 | 28 | if ($CmdletParameterName -in $SkipParameterByName) { 29 | Write-Debug "Skipping parameter $CmdletParameterName by SkipParameterByName" 30 | continue 31 | } 32 | 33 | switch ($CmdletParameterName) { 34 | "id" { 35 | # Check if there is one or more values for Id and build a URI or query as appropriate 36 | if (@($ParametersDictionary[$CmdletParameterName]).Count -gt 1) { 37 | Write-Verbose " Joining IDs for parameter" 38 | $URIParameters['id__in'] = $ParametersDictionary[$CmdletParameterName] -join ',' 39 | } else { 40 | Write-Verbose " Adding ID to segments" 41 | [void]$uriSegments.Add($ParametersDictionary[$CmdletParameterName]) 42 | } 43 | 44 | break 45 | } 46 | 47 | 'Query' { 48 | Write-Verbose " Adding query parameter" 49 | $URIParameters['q'] = $ParametersDictionary[$CmdletParameterName] 50 | break 51 | } 52 | 53 | 'CustomFields' { 54 | Write-Verbose " Adding custom field query parameters" 55 | foreach ($field in $ParametersDictionary[$CmdletParameterName].GetEnumerator()) { 56 | Write-Verbose " Adding parameter 'cf_$($field.Key) = $($field.Value)" 57 | $URIParameters["cf_$($field.Key.ToLower())"] = $field.Value 58 | } 59 | 60 | break 61 | } 62 | 63 | default { 64 | Write-Verbose " Adding $($CmdletParameterName.ToLower()) parameter" 65 | $URIParameters[$CmdletParameterName.ToLower()] = $ParametersDictionary[$CmdletParameterName] 66 | break 67 | } 68 | } 69 | } 70 | 71 | return @{ 72 | 'Segments' = [System.Collections.ArrayList]$URISegments 73 | 'Parameters' = $URIParameters 74 | } 75 | } -------------------------------------------------------------------------------- /Functions/IPAM/VLAN/Get-NetboxIPAMVLAN.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxIPAMVLAN { 3 | [CmdletBinding(DefaultParameterSetName = 'Query')] 4 | param 5 | ( 6 | [Parameter(ParameterSetName = 'Query', 7 | Position = 0)] 8 | [ValidateRange(1, 4096)] 9 | [uint16]$VID, 10 | 11 | [Parameter(ParameterSetName = 'ByID')] 12 | [uint64[]]$Id, 13 | 14 | [Parameter(ParameterSetName = 'Query')] 15 | [string]$Query, 16 | 17 | [Parameter(ParameterSetName = 'Query')] 18 | [string]$Name, 19 | 20 | [Parameter(ParameterSetName = 'Query')] 21 | [string]$Tenant, 22 | 23 | [Parameter(ParameterSetName = 'Query')] 24 | [uint64]$Tenant_Id, 25 | 26 | [Parameter(ParameterSetName = 'Query')] 27 | [string]$TenantGroup, 28 | 29 | [Parameter(ParameterSetName = 'Query')] 30 | [uint64]$TenantGroup_Id, 31 | 32 | [Parameter(ParameterSetName = 'Query')] 33 | [object]$Status, 34 | 35 | [Parameter(ParameterSetName = 'Query')] 36 | [string]$Region, 37 | 38 | [Parameter(ParameterSetName = 'Query')] 39 | [string]$Site, 40 | 41 | [Parameter(ParameterSetName = 'Query')] 42 | [uint64]$Site_Id, 43 | 44 | [Parameter(ParameterSetName = 'Query')] 45 | [string]$Group, 46 | 47 | [Parameter(ParameterSetName = 'Query')] 48 | [uint64]$Group_Id, 49 | 50 | [Parameter(ParameterSetName = 'Query')] 51 | [string]$Role, 52 | 53 | [Parameter(ParameterSetName = 'Query')] 54 | [uint64]$Role_Id, 55 | 56 | [Parameter(ParameterSetName = 'Query')] 57 | [uint16]$Limit, 58 | 59 | [Parameter(ParameterSetName = 'Query')] 60 | [uint16]$Offset, 61 | 62 | [switch]$Raw 63 | ) 64 | 65 | switch ($PSCmdlet.ParameterSetName) { 66 | 'ById' { 67 | foreach ($VLAN_ID in $Id) { 68 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'vlans', $VLAN_ID)) 69 | 70 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' 71 | 72 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 73 | 74 | InvokeNetboxRequest -URI $uri -Raw:$Raw 75 | } 76 | 77 | break 78 | } 79 | 80 | default { 81 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'vlans')) 82 | 83 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 84 | 85 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 86 | 87 | InvokeNetboxRequest -URI $uri -Raw:$Raw 88 | 89 | break 90 | } 91 | } 92 | } 93 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /Tests/Setup.Tests.ps1: -------------------------------------------------------------------------------- 1 | 2 | [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")] 3 | param 4 | ( 5 | ) 6 | Import-Module Pester 7 | Remove-Module NetboxPS -Force -ErrorAction SilentlyContinue 8 | 9 | $ModulePath = "$PSScriptRoot\..\dist\NetboxPS.psd1" 10 | 11 | if (Test-Path $ModulePath) { 12 | Import-Module $ModulePath -ErrorAction Stop 13 | } 14 | 15 | Describe "Setup tests" -Tag 'Core', 'Setup' -Fixture { 16 | It "Throws an error for an empty hostname" { 17 | { Get-NetboxHostname } | Should -Throw 18 | } 19 | 20 | It "Sets the hostname" { 21 | Set-NetboxHostName -HostName 'netbox.domain.com' | Should -Be 'netbox.domain.com' 22 | } 23 | 24 | It "Gets the hostname from the variable" { 25 | Get-NetboxHostName | Should -Be 'netbox.domain.com' 26 | } 27 | 28 | It "Throws an error for empty credentials" { 29 | { Get-NetboxCredential } | Should -Throw 30 | } 31 | 32 | Context "Plain text credentials" { 33 | It "Sets the credentials using plain text" { 34 | Set-NetboxCredential -Token (ConvertTo-SecureString -String "faketoken" -Force -AsPlainText) | Should -BeOfType [pscredential] 35 | } 36 | 37 | It "Checks the set credentials" { 38 | Set-NetboxCredential -Token (ConvertTo-SecureString -String "faketoken" -Force -AsPlainText) 39 | (Get-NetboxCredential).GetNetworkCredential().Password | Should -BeExactly "faketoken" 40 | } 41 | } 42 | 43 | Context "Credentials object" { 44 | $Creds = [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force)) 45 | 46 | It "Sets the credentials using [pscredential]" { 47 | Set-NetboxCredential -Credential $Creds | Should -BeOfType [pscredential] 48 | } 49 | 50 | It "Checks the set credentials" { 51 | (Get-NetboxCredential).GetNetworkCredential().Password | Should -BeExactly 'faketoken' 52 | } 53 | } 54 | 55 | <# 56 | Context "Connecting to the API" { 57 | Mock Get-NetboxCircuitsChoices { 58 | return $true 59 | } -ModuleName NetboxPS -Verifiable 60 | 61 | $Creds = [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force)) 62 | 63 | It "Connects using supplied hostname and obtained credentials" { 64 | #$null = Set-NetboxCredentials -Credentials $Creds 65 | Connect-NetboxAPI -Hostname "fake.org" | Should -Be $true 66 | } 67 | 68 | It "Connects using supplied hostname and credentials" { 69 | Connect-NetboxAPI -Hostname 'fake.org' -Credentials $Creds | Should -Be $true 70 | } 71 | 72 | 73 | 74 | Assert-MockCalled -CommandName Get-NetboxCircuitsChoices -ModuleName NetboxPS 75 | } 76 | #> 77 | } 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /Functions/IPAM/Address/Get-NetboxIPAMAddress.ps1: -------------------------------------------------------------------------------- 1 | function Get-NetboxIPAMAddress { 2 | [CmdletBinding(DefaultParameterSetName = 'Query')] 3 | param 4 | ( 5 | [Parameter(ParameterSetName = 'Query', 6 | Position = 0)] 7 | [string]$Address, 8 | 9 | [Parameter(ParameterSetName = 'ByID')] 10 | [uint64[]]$Id, 11 | 12 | [Parameter(ParameterSetName = 'Query')] 13 | [string]$Query, 14 | 15 | [Parameter(ParameterSetName = 'Query')] 16 | [object]$Family, 17 | 18 | [Parameter(ParameterSetName = 'Query')] 19 | [string]$Parent, 20 | 21 | [Parameter(ParameterSetName = 'Query')] 22 | [byte]$Mask_Length, 23 | 24 | [Parameter(ParameterSetName = 'Query')] 25 | [string]$VRF, 26 | 27 | [Parameter(ParameterSetName = 'Query')] 28 | [uint64]$VRF_Id, 29 | 30 | [Parameter(ParameterSetName = 'Query')] 31 | [string]$Tenant, 32 | 33 | [Parameter(ParameterSetName = 'Query')] 34 | [uint64]$Tenant_Id, 35 | 36 | [Parameter(ParameterSetName = 'Query')] 37 | [string]$Device, 38 | 39 | [Parameter(ParameterSetName = 'Query')] 40 | [uint64]$Device_ID, 41 | 42 | [Parameter(ParameterSetName = 'Query')] 43 | [string]$Virtual_Machine, 44 | 45 | [Parameter(ParameterSetName = 'Query')] 46 | [uint64]$Virtual_Machine_Id, 47 | 48 | [Parameter(ParameterSetName = 'Query')] 49 | [uint64]$Interface_Id, 50 | 51 | [Parameter(ParameterSetName = 'Query')] 52 | [object]$Status, 53 | 54 | [Parameter(ParameterSetName = 'Query')] 55 | [object]$Role, 56 | 57 | [Parameter(ParameterSetName = 'Query')] 58 | [uint16]$Limit, 59 | 60 | [Parameter(ParameterSetName = 'Query')] 61 | [uint16]$Offset, 62 | 63 | [switch]$Raw 64 | ) 65 | 66 | switch ($PSCmdlet.ParameterSetName) { 67 | 'ById' { 68 | foreach ($IP_ID in $Id) { 69 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-addresses', $IP_ID)) 70 | 71 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' 72 | 73 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 74 | 75 | InvokeNetboxRequest -URI $uri -Raw:$Raw 76 | } 77 | 78 | break 79 | } 80 | 81 | default { 82 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-addresses')) 83 | 84 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 85 | 86 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 87 | 88 | InvokeNetboxRequest -URI $uri -Raw:$Raw 89 | 90 | break 91 | } 92 | } 93 | } -------------------------------------------------------------------------------- /Functions/Tenancy/ContactAssignment/Set-NetboxContactAssignment.ps1: -------------------------------------------------------------------------------- 1 | 2 | 3 | function Set-NetboxContactAssignment { 4 | <# 5 | .SYNOPSIS 6 | Update a contact role assignment in Netbox 7 | 8 | .DESCRIPTION 9 | Updates a contact role assignment in Netbox 10 | 11 | .PARAMETER Content_Type 12 | The content type for this assignment. 13 | 14 | .PARAMETER Object_Id 15 | ID of the object to assign. 16 | 17 | .PARAMETER Contact 18 | ID of the contact to assign. 19 | 20 | .PARAMETER Role 21 | ID of the contact role to assign. 22 | 23 | .PARAMETER Priority 24 | Priority of the contact assignment. 25 | 26 | .PARAMETER Raw 27 | Return the unparsed data from the HTTP request 28 | 29 | .EXAMPLE 30 | PS C:\> Set-NetboxContactAssignment -Id 11 -Content_Type 'dcim.location' -Object_id 10 -Contact 15 -Role 10 -Priority 'Primary' 31 | 32 | .NOTES 33 | Valid content types: https://docs.netbox.dev/en/stable/features/contacts/#contacts_1 34 | #> 35 | 36 | [CmdletBinding(ConfirmImpact = 'Low', 37 | SupportsShouldProcess = $true)] 38 | [OutputType([pscustomobject])] 39 | param 40 | ( 41 | [Parameter(Mandatory = $true, 42 | ValueFromPipelineByPropertyName = $true)] 43 | [uint64[]]$Id, 44 | 45 | [Parameter(ValueFromPipelineByPropertyName = $true)] 46 | [ValidateSet('circuits.circuit', 'circuits.provider', 'circuits.provideraccount', 'dcim.device', 'dcim.location', 'dcim.manufacturer', 'dcim.powerpanel', 'dcim.rack', 'dcim.region', 'dcim.site', 'dcim.sitegroup', 'tenancy.tenant', 'virtualization.cluster', 'virtualization.clustergroup', 'virtualization.virtualmachine', IgnoreCase = $true)] 47 | [string]$Content_Type, 48 | 49 | [uint64]$Object_Id, 50 | 51 | [uint64]$Contact, 52 | 53 | [uint64]$Role, 54 | 55 | [ValidateSet('primary', 'secondary', 'tertiary', 'inactive', IgnoreCase = $true)] 56 | [string]$Priority, 57 | 58 | [switch]$Raw 59 | ) 60 | 61 | begin { 62 | $Method = 'Patch' 63 | } 64 | 65 | process { 66 | foreach ($ContactAssignmentId in $Id) { 67 | $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'contact-assignments', $ContactAssignmentId)) 68 | 69 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force' 70 | 71 | $URI = BuildNewURI -Segments $URIComponents.Segments 72 | 73 | $CurrentContactAssignment = Get-NetboxContactAssignment -Id $ContactAssignmentId -ErrorAction Stop 74 | 75 | if ($PSCmdlet.ShouldProcess($CurrentContactAssignment.Id, 'Update contact assignment')) { 76 | InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters -Raw:$Raw 77 | } 78 | } 79 | } 80 | } 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /NetboxPS.psproj.bak: -------------------------------------------------------------------------------- 1 | 2 | 2.0 3 | bba9b06c-49c8-47cf-8358-aca7c4e78896 4 | 1 5 | 6 | Functions 7 | Functions\DCIM 8 | Functions\Extras 9 | Functions\Circuits 10 | Tests 11 | Functions\Virtualization 12 | Functions\IPAM 13 | Functions\Tenancy 14 | 15 | 16 | NetboxPS.psd1 17 | NetboxPS.psm1 18 | Test-Module.ps1 19 | .gitignore 20 | Functions\Helpers.ps1 21 | Functions\Setup.ps1 22 | Functions\Extras\Extras.ps1 23 | Functions\Circuits\Circuits.ps1 24 | Tests\Setup.Tests.ps1 25 | Tests\Helpers.Tests.ps1 26 | Functions\Virtualization\Virtualization.ps1 27 | Tests\Virtualization.Tests.ps1 28 | Tests\IPAM.Tests.ps1 29 | Functions\IPAM\IPAM.ps1 30 | Tests\IPAMChoices.json 31 | Tests\VirtualizationChoices.json 32 | Functions\DCIM\DCIM.ps1 33 | Tests\DCIM.Devices.Tests.ps1 34 | Tests\DCIM.Interfaces.Tests.ps1 35 | Tests\DCIM.Platforms.Tests.ps1 36 | Functions\DCIM\DCIM.Devices.ps1 37 | Functions\DCIM\DCIM.Interfaces.ps1 38 | Functions\Tenancy\Tenancy.ps1 39 | 40 | R:\Netbox\NetboxPS\Test-Module.ps1 41 | -------------------------------------------------------------------------------- /Functions/IPAM/Address/New-NetboxIPAMAddress.ps1: -------------------------------------------------------------------------------- 1 | 2 | function New-NetboxIPAMAddress { 3 | <# 4 | .SYNOPSIS 5 | Create a new IP address to Netbox 6 | 7 | .DESCRIPTION 8 | Create a new IP address to Netbox with a status of Active by default. 9 | 10 | .PARAMETER Address 11 | IP address in CIDR notation: 192.168.1.1/24 12 | 13 | .PARAMETER Status 14 | Status of the IP. Defaults to Active 15 | 16 | .PARAMETER Tenant 17 | Tenant ID 18 | 19 | .PARAMETER VRF 20 | VRF ID 21 | 22 | .PARAMETER Role 23 | Role such as anycast, loopback, etc... Defaults to nothing 24 | 25 | .PARAMETER NAT_Inside 26 | ID of IP for NAT 27 | 28 | .PARAMETER Custom_Fields 29 | Custom field hash table. Will be validated by the API service 30 | 31 | .PARAMETER Interface 32 | ID of interface to apply IP 33 | 34 | .PARAMETER Description 35 | Description of IP address 36 | 37 | .PARAMETER Dns_name 38 | DNS Name of IP address (example : netbox.example.com) 39 | 40 | .PARAMETER Assigned_Object_Type 41 | Assigned Object Type dcim.interface or virtualization.vminterface 42 | 43 | .PARAMETER Assigned_Object_Id 44 | Assigned Object ID 45 | 46 | .PARAMETER Raw 47 | Return raw results from API service 48 | 49 | .EXAMPLE 50 | New-NetboxIPAMAddress -Address 192.0.2.1/32 51 | 52 | Add new IP Address 192.0.2.1/32 with status active 53 | 54 | .NOTES 55 | Additional information about the function. 56 | #> 57 | 58 | [CmdletBinding(ConfirmImpact = 'Low', 59 | SupportsShouldProcess = $true)] 60 | [OutputType([pscustomobject])] 61 | param 62 | ( 63 | [Parameter(Mandatory = $true, 64 | ValueFromPipelineByPropertyName = $true)] 65 | [string]$Address, 66 | 67 | [object]$Status = 'Active', 68 | 69 | [uint64]$Tenant, 70 | 71 | [uint64]$VRF, 72 | 73 | [object]$Role, 74 | 75 | [uint64]$NAT_Inside, 76 | 77 | [hashtable]$Custom_Fields, 78 | 79 | [uint64]$Interface, 80 | 81 | [string]$Description, 82 | 83 | [string]$Dns_name, 84 | 85 | [ValidateSet('dcim.interface', 'virtualization.vminterface', IgnoreCase = $true)] 86 | [string]$Assigned_Object_Type, 87 | 88 | [uint64]$Assigned_Object_Id, 89 | 90 | [switch]$Raw 91 | ) 92 | 93 | process { 94 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-addresses')) 95 | $Method = 'POST' 96 | 97 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 98 | 99 | $URI = BuildNewURI -Segments $URIComponents.Segments 100 | 101 | if ($PSCmdlet.ShouldProcess($Address, 'Create new IP address')) { 102 | InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters -Raw:$Raw 103 | } 104 | } 105 | } 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /Functions/DCIM/Sites/Get-NetboxDCIMSite.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxDCIMSite { 3 | [CmdletBinding(DefaultParameterSetName = 'Query')] 4 | [OutputType([pscustomobject])] 5 | param 6 | ( 7 | [Parameter(ParameterSetName = 'ByID', ValueFromPipelineByPropertyName = $true)] 8 | [uint64]$Id, 9 | 10 | [Parameter(ParameterSetName = 'Query')] 11 | [string]$Name, 12 | 13 | [Parameter(ParameterSetName = 'Query')] 14 | [string]$Query, 15 | 16 | [Parameter(ParameterSetName = 'Query')] 17 | [string]$Slug, 18 | 19 | [Parameter(ParameterSetName = 'Query')] 20 | [string]$Facility, 21 | 22 | [Parameter(ParameterSetName = 'Query')] 23 | [uint64]$ASN, 24 | 25 | [Parameter(ParameterSetName = 'Query')] 26 | [decimal]$Latitude, 27 | 28 | [Parameter(ParameterSetName = 'Query')] 29 | [decimal]$Longitude, 30 | 31 | [Parameter(ParameterSetName = 'Query')] 32 | [string]$Contact_Name, 33 | 34 | [Parameter(ParameterSetName = 'Query')] 35 | [string]$Contact_Phone, 36 | 37 | [Parameter(ParameterSetName = 'Query')] 38 | [string]$Contact_Email, 39 | 40 | [Parameter(ParameterSetName = 'Query')] 41 | [uint64]$Tenant_Group_ID, 42 | 43 | [Parameter(ParameterSetName = 'Query')] 44 | [string]$Tenant_Group, 45 | 46 | [Parameter(ParameterSetName = 'Query')] 47 | [uint64]$Tenant_ID, 48 | 49 | [Parameter(ParameterSetName = 'Query')] 50 | [string]$Tenant, 51 | 52 | [Parameter(ParameterSetName = 'Query')] 53 | [string]$Status, 54 | 55 | [Parameter(ParameterSetName = 'Query')] 56 | [uint64]$Region_ID, 57 | 58 | [Parameter(ParameterSetName = 'Query')] 59 | [string]$Region, 60 | 61 | [Parameter(ParameterSetName = 'Query')] 62 | [uint16]$Limit, 63 | 64 | [Parameter(ParameterSetName = 'Query')] 65 | [uint16]$Offset, 66 | 67 | [switch]$Raw 68 | ) 69 | 70 | process { 71 | switch ($PSCmdlet.ParameterSetName) { 72 | 'ById' { 73 | foreach ($Site_ID in $ID) { 74 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'sites', $Site_Id)) 75 | 76 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName "Id" 77 | 78 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 79 | 80 | InvokeNetboxRequest -URI $URI -Raw:$Raw 81 | } 82 | } 83 | 84 | default { 85 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'sites')) 86 | 87 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters 88 | 89 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 90 | 91 | InvokeNetboxRequest -URI $URI -Raw:$Raw 92 | } 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /Functions/IPAM/Range/New-NetboxIPAMAddressRange.ps1: -------------------------------------------------------------------------------- 1 | 2 | 3 | function New-NetboxIPAMAddressRange { 4 | <# 5 | .SYNOPSIS 6 | Create a new IP address range to Netbox 7 | 8 | .DESCRIPTION 9 | Create a new IP address range to Netbox with a status of Active by default. The maximum supported 10 | size of an IP range is 2^32 - 1. 11 | 12 | .PARAMETER Start_Address 13 | Starting IPv4 or IPv6 address (with mask). The maximum supported size of an IP range is 2^32 - 1. 14 | 15 | .PARAMETER End_Address 16 | Ending IPv4 or IPv6 address (with mask). The maximum supported size of an IP range is 2^32 - 1. 17 | 18 | .PARAMETER Status 19 | Operational status of this range. Defaults to Active 20 | 21 | .PARAMETER Tenant 22 | Tenant ID 23 | 24 | .PARAMETER VRF 25 | VRF ID 26 | 27 | .PARAMETER Role 28 | Role such as backup, customer, development, etc... Defaults to nothing 29 | 30 | .PARAMETER Custom_Fields 31 | Custom field hash table. Will be validated by the API service 32 | 33 | .PARAMETER Description 34 | Description of IP address range 35 | 36 | .PARAMETER Comments 37 | Extra comments (markdown supported). 38 | 39 | .PARAMETER Tags 40 | One or more tags. 41 | 42 | .PARAMETER Mark_Utilized 43 | Treat as 100% utilized 44 | 45 | .PARAMETER Raw 46 | Return raw results from API service 47 | 48 | .EXAMPLE 49 | New-NetboxIPAMAddressRange -Start_Address 192.0.2.20/24 -End_Address 192.0.2.20/24 50 | 51 | Add new IP Address range from 192.0.2.20/24 to 192.0.2.20/24 with status active 52 | 53 | .NOTES 54 | https://netbox.neonet.org/static/docs/models/ipam/iprange/ 55 | #> 56 | 57 | [CmdletBinding(ConfirmImpact = 'Low', 58 | SupportsShouldProcess = $true)] 59 | [OutputType([pscustomobject])] 60 | param 61 | ( 62 | [Parameter(Mandatory = $true)] 63 | [string]$Start_Address, 64 | 65 | [Parameter(Mandatory = $true)] 66 | [string]$End_Address, 67 | 68 | [object]$Status = 'Active', 69 | 70 | [uint64]$Tenant, 71 | 72 | [uint64]$VRF, 73 | 74 | [object]$Role, 75 | 76 | [hashtable]$Custom_Fields, 77 | 78 | [string]$Description, 79 | 80 | [string]$Comments, 81 | 82 | [object[]]$Tags, 83 | 84 | [switch]$Mark_Utilized, 85 | 86 | [switch]$Raw 87 | ) 88 | 89 | process { 90 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-ranges')) 91 | $Method = 'POST' 92 | 93 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 94 | 95 | $URI = BuildNewURI -Segments $URIComponents.Segments 96 | 97 | if ($PSCmdlet.ShouldProcess($Start_Address, 'Create new IP address range')) { 98 | InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters -Raw:$Raw 99 | } 100 | } 101 | } 102 | 103 | 104 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NetboxPS 2 | ###### Powershell Netbox API module 3 | 4 | # Disclaimer 5 | This module is beta. Use it at your own risk. I have only added functions as I have needed them, so not everything is available. 6 | 7 | All functions are exported at the moment, including internal/private functions. 8 | 9 | # Description 10 | This module is a wrapper for the [Netbox](https://github.com/netbox-community/netbox) API. 11 | 12 | # Usage 13 | 1. Install module from the `netboxPS` folder 14 | 2. Import module 15 | 3. Connect to an API endpoint by using `Connect-NetboxAPI -Hostname netbox.example.com` 16 | 17 | ## Basic Commands 18 | 19 | ```powershell 20 | #Just adding a new IP 21 | New-NetboxIPAMAddress -Address 10.0.0.1/24 -Dns_name this.is.thedns.fqdn -Custom_Fields @{CustomFieldID="CustomFieldContent"} -Tenant 1 -Description "Description" 22 | 23 | #Creating a new VM, add an interface and assign Interface IP 24 | function New-NBVirtualMachine 25 | { 26 | [CmdletBinding()] 27 | [Alias()] 28 | [OutputType([int])] 29 | Param 30 | ( 31 | [string]$Name, 32 | [string]$Cluster, 33 | [string]$IP, 34 | [string]$tenant, 35 | [string]$VMNICName 36 | ) 37 | 38 | Begin 39 | { 40 | $NBCluster = Get-NetboxVirtualizationCluster -name $Cluster 41 | $NBTenant = Get-NetboxTenant -Name $tenant 42 | } 43 | Process 44 | { 45 | $vm = New-NetboxVirtualMachine -Name $Name -Cluster $NBCluster.id -Tenant $NBtenant.id 46 | $interface = Add-NetboxVirtualMachineInterface -Name $VMNICName -Virtual_Machine $vm.id 47 | 48 | 49 | $NBip = New-NetboxIPAMAddress -Address $IP -Tenant $NBtenant.id 50 | Set-NetboxIPAMAddress -Assigned_Object_Type virtualization.vminterface -Assigned_Object_Id $interface.id -id $NBip.id 51 | Set-NetboxVirtualMachine -Primary_IP4 $NBip.id -Id $vm.id 52 | } 53 | } 54 | 55 | ``` 56 | 57 | # Notes 58 | I started this project years ago with Powershell Studio using the built in deployment methods, learning Git, and learning PS best practices. So please forgive any "obvious" mistakes 😅 59 | Over time I have had to adjust my methods for deployment... change the design of functions, and refactor code as I learn new and better things. 60 | 61 | This was built out of a need at my job to interact with Netbox for automation. Only recently has it become a "public" project with other collaborators (which I truly appreciate!). 62 | I have done my best to ensure each function does exactly one thing according to the API. 63 | 64 | I will do my best to keep up, but please understand it is given attention as I can at work. As time permits, I will open issues for TODOs for things I have wanted to do for a while, just haven't had time or enough "need" to do them. 65 | 66 | # Contributing 67 | - Follow [Powershell Practice and Style Guidelines](https://poshcode.gitbook.io/powershell-practice-and-style/) when writing code 68 | - Use discussions for general questions 69 | - Open issues for bug fixes or enhancements 70 | - Submit all pull requests against the dev branch 71 | 72 | I am always open to suggestions for improvement with reasons and data to back up the suggestion. -------------------------------------------------------------------------------- /Functions/Helpers/InvokeNetboxRequest.ps1: -------------------------------------------------------------------------------- 1 | 2 | function InvokeNetboxRequest { 3 | [CmdletBinding()] 4 | param 5 | ( 6 | [Parameter(Mandatory = $true)] 7 | [System.UriBuilder]$URI, 8 | 9 | [Hashtable]$Headers = @{ 10 | }, 11 | 12 | [pscustomobject]$Body = $null, 13 | 14 | [ValidateRange(1, 65535)] 15 | [uint16]$Timeout = (Get-NetboxTimeout), 16 | 17 | [ValidateSet('GET', 'PATCH', 'PUT', 'POST', 'DELETE', 'OPTIONS', IgnoreCase = $true)] 18 | [string]$Method = 'GET', 19 | 20 | [switch]$Raw 21 | ) 22 | 23 | $creds = Get-NetboxCredential 24 | 25 | $Headers.Authorization = "Token {0}" -f $creds.GetNetworkCredential().Password 26 | 27 | $splat = @{ 28 | 'Method' = $Method 29 | 'Uri' = $URI.Uri.AbsoluteUri # This property auto generates the scheme, hostname, path, and query 30 | 'Headers' = $Headers 31 | 'TimeoutSec' = $Timeout 32 | 'ContentType' = 'application/json' 33 | 'ErrorAction' = 'Stop' 34 | 'Verbose' = $VerbosePreference 35 | } 36 | 37 | $splat += Get-NetboxInvokeParams 38 | 39 | if ($Body) { 40 | Write-Verbose "BODY: $($Body | ConvertTo-Json -Compress)" 41 | $null = $splat.Add('Body', ($Body | ConvertTo-Json -Compress)) 42 | } 43 | 44 | $result = Invoke-RestMethod @splat 45 | 46 | #region TODO: Handle errors a little more gracefully... 47 | 48 | <# 49 | try { 50 | Write-Verbose "Sending request..." 51 | $result = Invoke-RestMethod @splat 52 | Write-Verbose $result 53 | } catch { 54 | Write-Verbose "Caught exception" 55 | if ($_.Exception.psobject.properties.Name.contains('Response')) { 56 | Write-Verbose "Exception contains a response property" 57 | if ($Raw) { 58 | Write-Verbose "RAW provided...throwing raw exception" 59 | throw $_ 60 | } 61 | 62 | Write-Verbose "Converting response to object" 63 | $myError = GetNetboxAPIErrorBody -Response $_.Exception.Response | ConvertFrom-Json 64 | } else { 65 | Write-Verbose "No response property found" 66 | $myError = $_ 67 | } 68 | } 69 | 70 | Write-Verbose "MyError is $($myError.GetType().FullName)" 71 | 72 | if ($myError -is [Exception]) { 73 | throw $_ 74 | } elseif ($myError -is [pscustomobject]) { 75 | throw $myError.detail 76 | } 77 | #> 78 | 79 | #endregion TODO: Handle errors a little more gracefully... 80 | 81 | # If the user wants the raw value from the API... otherwise return only the actual result 82 | if ($Raw) { 83 | Write-Verbose "Returning raw result by choice" 84 | return $result 85 | } else { 86 | if ($result.psobject.Properties.Name.Contains('results')) { 87 | Write-Verbose "Found Results property on data, returning results directly" 88 | return $result.Results 89 | } else { 90 | Write-Verbose "Did NOT find results property on data, returning raw result" 91 | return $result 92 | } 93 | } 94 | } -------------------------------------------------------------------------------- /Functions/DCIM/Interfaces/Add-NetboxDCIMInterface.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Add-NetboxDCIMInterface { 3 | [CmdletBinding()] 4 | [OutputType([pscustomobject])] 5 | param 6 | ( 7 | [Parameter(Mandatory = $true)] 8 | [uint64]$Device, 9 | 10 | [Parameter(Mandatory = $true)] 11 | [string]$Name, 12 | 13 | [ValidateSet('virtual', 'bridge', 'lag', '100base-tx', '1000base-t', '2.5gbase-t', '5gbase-t', '10gbase-t', '10gbase-cx4', '1000base-x-gbic', '1000base-x-sfp', '10gbase-x-sfpp', '10gbase-x-xfp', '10gbase-x-xenpak', '10gbase-x-x2', '25gbase-x-sfp28', '50gbase-x-sfp56', '40gbase-x-qsfpp', '50gbase-x-sfp28', '100gbase-x-cfp', '100gbase-x-cfp2', '200gbase-x-cfp2', '100gbase-x-cfp4', '100gbase-x-cpak', '100gbase-x-qsfp28', '200gbase-x-qsfp56', '400gbase-x-qsfpdd', '400gbase-x-osfp', '1000base-kx', '10gbase-kr', '10gbase-kx4', '25gbase-kr', '40gbase-kr4', '50gbase-kr', '100gbase-kp4', '100gbase-kr2', '100gbase-kr4', 'ieee802.11a', 'ieee802.11g', 'ieee802.11n', 'ieee802.11ac', 'ieee802.11ad', 'ieee802.11ax', 'ieee802.11ay', 'ieee802.15.1', 'other-wireless', 'gsm', 'cdma', 'lte', 'sonet-oc3', 'sonet-oc12', 'sonet-oc48', 'sonet-oc192', 'sonet-oc768', 'sonet-oc1920', 'sonet-oc3840', '1gfc-sfp', '2gfc-sfp', '4gfc-sfp', '8gfc-sfpp', '16gfc-sfpp', '32gfc-sfp28', '64gfc-qsfpp', '128gfc-qsfp28', 'infiniband-sdr', 'infiniband-ddr', 'infiniband-qdr', 'infiniband-fdr10', 'infiniband-fdr', 'infiniband-edr', 'infiniband-hdr', 'infiniband-ndr', 'infiniband-xdr', 't1', 'e1', 't3', 'e3', 'xdsl', 'docsis', 'gpon', 'xg-pon', 'xgs-pon', 'ng-pon2', 'epon', '10g-epon', 'cisco-stackwise', 'cisco-stackwise-plus', 'cisco-flexstack', 'cisco-flexstack-plus', 'cisco-stackwise-80', 'cisco-stackwise-160', 'cisco-stackwise-320', 'cisco-stackwise-480', 'juniper-vcp', 'extreme-summitstack', 'extreme-summitstack-128', 'extreme-summitstack-256', 'extreme-summitstack-512', 'other', IgnoreCase = $true)] 14 | [string]$Type, 15 | 16 | [bool]$Enabled, 17 | 18 | [object]$Form_Factor, 19 | 20 | [uint16]$MTU, 21 | 22 | [string]$MAC_Address, 23 | 24 | [bool]$MGMT_Only, 25 | 26 | [uint64]$LAG, 27 | 28 | [string]$Description, 29 | 30 | [ValidateSet('Access', 'Tagged', 'Tagged All', '100', '200', '300', IgnoreCase = $true)] 31 | [string]$Mode, 32 | 33 | [ValidateRange(1, 4094)] 34 | [uint16]$Untagged_VLAN, 35 | 36 | [ValidateRange(1, 4094)] 37 | [uint16[]]$Tagged_VLANs 38 | ) 39 | 40 | if (-not [System.String]::IsNullOrWhiteSpace($Mode)) { 41 | $PSBoundParameters.Mode = switch ($Mode) { 42 | 'Access' { 43 | 100 44 | break 45 | } 46 | 47 | 'Tagged' { 48 | 200 49 | break 50 | } 51 | 52 | 'Tagged All' { 53 | 300 54 | break 55 | } 56 | 57 | default { 58 | $_ 59 | } 60 | } 61 | } 62 | 63 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'interfaces')) 64 | 65 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters 66 | 67 | $URI = BuildNewURI -Segments $URIComponents.Segments 68 | 69 | InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method POST 70 | } -------------------------------------------------------------------------------- /Functions/Tenancy/Tenants/Get-NetboxTenant.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxTenant { 3 | <# 4 | .SYNOPSIS 5 | Get a tenent from Netbox 6 | 7 | .DESCRIPTION 8 | A detailed description of the Get-NetboxTenant function. 9 | 10 | .PARAMETER Name 11 | The specific name of the tenant. Must match exactly as is defined in Netbox 12 | 13 | .PARAMETER Id 14 | The database ID of the tenant 15 | 16 | .PARAMETER Query 17 | A standard search query that will match one or more tenants. 18 | 19 | .PARAMETER Slug 20 | The specific slug of the tenant. Must match exactly as is defined in Netbox 21 | 22 | .PARAMETER Group 23 | The specific group as defined in Netbox. 24 | 25 | .PARAMETER GroupID 26 | The database ID of the group in Netbox 27 | 28 | .PARAMETER CustomFields 29 | Hashtable in the format @{"field_name" = "value"} to search 30 | 31 | .PARAMETER Limit 32 | Limit the number of results to this number 33 | 34 | .PARAMETER Offset 35 | Start the search at this index in results 36 | 37 | .PARAMETER Raw 38 | Return the unparsed data from the HTTP request 39 | 40 | .EXAMPLE 41 | PS C:\> Get-NetboxTenant 42 | 43 | .NOTES 44 | Additional information about the function. 45 | #> 46 | 47 | [CmdletBinding(DefaultParameterSetName = 'Query')] 48 | param 49 | ( 50 | [Parameter(ParameterSetName = 'Query', 51 | Position = 0)] 52 | [string]$Name, 53 | 54 | [Parameter(ParameterSetName = 'ByID')] 55 | [uint64[]]$Id, 56 | 57 | [Parameter(ParameterSetName = 'Query')] 58 | [string]$Query, 59 | 60 | [Parameter(ParameterSetName = 'Query')] 61 | [string]$Slug, 62 | 63 | [Parameter(ParameterSetName = 'Query')] 64 | [string]$Group, 65 | 66 | [Parameter(ParameterSetName = 'Query')] 67 | [uint64]$GroupID, 68 | 69 | [Parameter(ParameterSetName = 'Query')] 70 | [hashtable]$CustomFields, 71 | 72 | [Parameter(ParameterSetName = 'Query')] 73 | [uint16]$Limit, 74 | 75 | [Parameter(ParameterSetName = 'Query')] 76 | [uint16]$Offset, 77 | 78 | [switch]$Raw 79 | ) 80 | 81 | switch ($PSCmdlet.ParameterSetName) { 82 | 'ById' { 83 | foreach ($Tenant_ID in $Id) { 84 | $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'tenants', $Tenant_ID)) 85 | 86 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' 87 | 88 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 89 | 90 | InvokeNetboxRequest -URI $uri -Raw:$Raw 91 | } 92 | 93 | break 94 | } 95 | 96 | default { 97 | $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'tenants')) 98 | 99 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 100 | 101 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 102 | 103 | InvokeNetboxRequest -URI $uri -Raw:$Raw 104 | 105 | break 106 | } 107 | } 108 | } -------------------------------------------------------------------------------- /Functions/Tenancy/Contacts/Set-NetboxContact.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Set-NetboxContact { 3 | <# 4 | .SYNOPSIS 5 | Update a contact in Netbox 6 | 7 | .DESCRIPTION 8 | Updates a contact object in Netbox which can be linked to other objects 9 | 10 | .PARAMETER Id 11 | A description of the Id parameter. 12 | 13 | .PARAMETER Name 14 | The contacts full name, e.g "Leroy Jenkins" 15 | 16 | .PARAMETER Email 17 | Email address of the contact 18 | 19 | .PARAMETER Group 20 | Database ID of assigned group 21 | 22 | .PARAMETER Title 23 | Job title or other title related to the contact 24 | 25 | .PARAMETER Phone 26 | Telephone number 27 | 28 | .PARAMETER Address 29 | Physical address, usually mailing address 30 | 31 | .PARAMETER Description 32 | Short description of the contact 33 | 34 | .PARAMETER Comments 35 | Detailed comments. Markdown supported. 36 | 37 | .PARAMETER Link 38 | URI related to the contact 39 | 40 | .PARAMETER Custom_Fields 41 | A description of the Custom_Fields parameter. 42 | 43 | .PARAMETER Force 44 | A description of the Force parameter. 45 | 46 | .PARAMETER Raw 47 | A description of the Raw parameter. 48 | 49 | .EXAMPLE 50 | PS C:\> Set-NetboxContact -Id 10 -Name 'Leroy Jenkins' -Email 'leroy.jenkins@example.com' 51 | 52 | .NOTES 53 | Additional information about the function. 54 | #> 55 | 56 | [CmdletBinding(ConfirmImpact = 'Low', 57 | SupportsShouldProcess = $true)] 58 | [OutputType([pscustomobject])] 59 | param 60 | ( 61 | [Parameter(Mandatory = $true, 62 | ValueFromPipelineByPropertyName = $true)] 63 | [uint64[]]$Id, 64 | 65 | [ValidateLength(1, 100)] 66 | [string]$Name, 67 | 68 | [ValidateLength(0, 254)] 69 | [string]$Email, 70 | 71 | [uint64]$Group, 72 | 73 | [ValidateLength(0, 100)] 74 | [string]$Title, 75 | 76 | [ValidateLength(0, 50)] 77 | [string]$Phone, 78 | 79 | [ValidateLength(0, 200)] 80 | [string]$Address, 81 | 82 | [ValidateLength(0, 200)] 83 | [string]$Description, 84 | 85 | [string]$Comments, 86 | 87 | [ValidateLength(0, 200)] 88 | [string]$Link, 89 | 90 | [hashtable]$Custom_Fields, 91 | 92 | [switch]$Force, 93 | 94 | [switch]$Raw 95 | ) 96 | 97 | begin { 98 | $Method = 'PATCH' 99 | } 100 | 101 | process { 102 | foreach ($ContactId in $Id) { 103 | $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'contacts', $ContactId)) 104 | 105 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force' 106 | 107 | $URI = BuildNewURI -Segments $URIComponents.Segments 108 | 109 | $CurrentContact = Get-NetboxContact -Id $ContactId -ErrorAction Stop 110 | 111 | if ($Force -or $PSCmdlet.ShouldProcess($CurrentContact.Name, 'Update contact')) { 112 | InvokeNetboxRequest -URI $URI -Method $Method -Body $URIComponents.Parameters -Raw:$Raw 113 | } 114 | } 115 | } 116 | } 117 | 118 | 119 | 120 | 121 | -------------------------------------------------------------------------------- /Functions/IPAM/Address/Set-NetboxIPAMAddress.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Set-NetboxIPAMAddress { 3 | [CmdletBinding(ConfirmImpact = 'Medium', 4 | SupportsShouldProcess = $true)] 5 | param 6 | ( 7 | [Parameter(Mandatory = $true, 8 | ValueFromPipelineByPropertyName = $true)] 9 | [uint64[]]$Id, 10 | 11 | [string]$Address, 12 | 13 | [string]$Status, 14 | 15 | [uint64]$Tenant, 16 | 17 | [uint64]$VRF, 18 | 19 | [object]$Role, 20 | 21 | [uint64]$NAT_Inside, 22 | 23 | [hashtable]$Custom_Fields, 24 | 25 | [ValidateSet('dcim.interface', 'virtualization.vminterface', IgnoreCase = $true)] 26 | [string]$Assigned_Object_Type, 27 | 28 | [uint64]$Assigned_Object_Id, 29 | 30 | [string]$Description, 31 | 32 | [string]$Dns_name, 33 | 34 | [switch]$Force 35 | ) 36 | 37 | begin { 38 | # Write-Verbose "Validating enum properties" 39 | # $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-addresses', 0)) 40 | $Method = 'PATCH' 41 | # 42 | # # Value validation 43 | # $ModelDefinition = GetModelDefinitionFromURIPath -Segments $Segments -Method $Method 44 | # $EnumProperties = GetModelEnumProperties -ModelDefinition $ModelDefinition 45 | # 46 | # foreach ($Property in $EnumProperties.Keys) { 47 | # if ($PSBoundParameters.ContainsKey($Property)) { 48 | # Write-Verbose "Validating property [$Property] with value [$($PSBoundParameters.$Property)]" 49 | # $PSBoundParameters.$Property = ValidateValue -ModelDefinition $ModelDefinition -Property $Property -ProvidedValue $PSBoundParameters.$Property 50 | # } else { 51 | # Write-Verbose "User did not provide a value for [$Property]" 52 | # } 53 | # } 54 | # 55 | # Write-Verbose "Finished enum validation" 56 | } 57 | 58 | process { 59 | foreach ($IPId in $Id) { 60 | if ($PSBoundParameters.ContainsKey('Assigned_Object_Type') -or $PSBoundParameters.ContainsKey('Assigned_Object_Id')) { 61 | if ((-not [string]::IsNullOrWhiteSpace($Assigned_Object_Id)) -and [string]::IsNullOrWhiteSpace($Assigned_Object_Type)) { 62 | throw "Assigned_Object_Type is required when specifying Assigned_Object_Id" 63 | } 64 | elseif ((-not [string]::IsNullOrWhiteSpace($Assigned_Object_Type)) -and [string]::IsNullOrWhiteSpace($Assigned_Object_Id)) { 65 | throw "Assigned_Object_Id is required when specifying Assigned_Object_Type" 66 | } 67 | } 68 | 69 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'ip-addresses', $IPId)) 70 | 71 | Write-Verbose "Obtaining IP from ID $IPId" 72 | $CurrentIP = Get-NetboxIPAMAddress -Id $IPId -ErrorAction Stop 73 | 74 | if ($Force -or $PSCmdlet.ShouldProcess($CurrentIP.Address, 'Set')) { 75 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id', 'Force' 76 | 77 | $URI = BuildNewURI -Segments $URIComponents.Segments 78 | 79 | InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method $Method 80 | } 81 | } 82 | } 83 | } -------------------------------------------------------------------------------- /Functions/Tenancy/ContactAssignment/Get-NetboxContactAssignment.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxContactAssignment { 3 | <# 4 | .SYNOPSIS 5 | Get a contact Assignment from Netbox 6 | 7 | .DESCRIPTION 8 | A detailed description of the Get-NetboxContactAssignment function. 9 | 10 | .PARAMETER Name 11 | The specific name of the contact Assignment. Must match exactly as is defined in Netbox 12 | 13 | .PARAMETER Id 14 | The database ID of the contact Assignment 15 | 16 | .PARAMETER Content_Type_Id 17 | A description of the Content_Type_Id parameter. 18 | 19 | .PARAMETER Content_Type 20 | A description of the Content_Type parameter. 21 | 22 | .PARAMETER Object_Id 23 | A description of the Object_Id parameter. 24 | 25 | .PARAMETER Contact_Id 26 | A description of the Contact_Id parameter. 27 | 28 | .PARAMETER Role_Id 29 | A description of the Role_Id parameter. 30 | 31 | .PARAMETER Limit 32 | Limit the number of results to this number 33 | 34 | .PARAMETER Offset 35 | Start the search at this index in results 36 | 37 | .PARAMETER Raw 38 | Return the unparsed data from the HTTP request 39 | 40 | .EXAMPLE 41 | PS C:\> Get-NetboxContactAssignment 42 | 43 | .NOTES 44 | Additional information about the function. 45 | #> 46 | 47 | [CmdletBinding(DefaultParameterSetName = 'Query')] 48 | param 49 | ( 50 | [Parameter(ParameterSetName = 'Query', 51 | Position = 0)] 52 | [string]$Name, 53 | 54 | [Parameter(ParameterSetName = 'ByID')] 55 | [uint64[]]$Id, 56 | 57 | [Parameter(ParameterSetName = 'Query')] 58 | [uint64]$Content_Type_Id, 59 | 60 | [Parameter(ParameterSetName = 'Query')] 61 | [string]$Content_Type, 62 | 63 | [Parameter(ParameterSetName = 'Query')] 64 | [uint64]$Object_Id, 65 | 66 | [Parameter(ParameterSetName = 'Query')] 67 | [uint64]$Contact_Id, 68 | 69 | [Parameter(ParameterSetName = 'Query')] 70 | [uint64]$Role_Id, 71 | 72 | [Parameter(ParameterSetName = 'Query')] 73 | [uint16]$Limit, 74 | 75 | [Parameter(ParameterSetName = 'Query')] 76 | [uint16]$Offset, 77 | 78 | [switch]$Raw 79 | ) 80 | 81 | switch ($PSCmdlet.ParameterSetName) { 82 | 'ById' { 83 | foreach ($ContactAssignment_ID in $Id) { 84 | $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'contact-assignments', $ContactAssignment_ID)) 85 | 86 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' 87 | 88 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 89 | 90 | InvokeNetboxRequest -URI $uri -Raw:$Raw 91 | } 92 | 93 | break 94 | } 95 | 96 | default { 97 | $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'contact-assignments')) 98 | 99 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 100 | 101 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 102 | 103 | InvokeNetboxRequest -URI $uri -Raw:$Raw 104 | 105 | break 106 | } 107 | } 108 | } -------------------------------------------------------------------------------- /Functions/Tenancy/Contacts/Get-NetboxContact.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxContact { 3 | <# 4 | .SYNOPSIS 5 | Get a contact from Netbox 6 | 7 | .DESCRIPTION 8 | Obtain a contact or contacts from Netbox by ID or query 9 | 10 | .PARAMETER Name 11 | The specific name of the Contact. Must match exactly as is defined in Netbox 12 | 13 | .PARAMETER Id 14 | The database ID of the Contact 15 | 16 | .PARAMETER Query 17 | A standard search query that will match one or more Contacts. 18 | 19 | .PARAMETER Email 20 | Email address of the contact 21 | 22 | .PARAMETER Title 23 | Title of the contact 24 | 25 | .PARAMETER Phone 26 | Telephone number of the contact 27 | 28 | .PARAMETER Address 29 | Physical address of the contact 30 | 31 | .PARAMETER Group 32 | The specific group as defined in Netbox. 33 | 34 | .PARAMETER GroupID 35 | The database ID of the group in Netbox 36 | 37 | .PARAMETER Limit 38 | Limit the number of results to this number 39 | 40 | .PARAMETER Offset 41 | Start the search at this index in results 42 | 43 | .PARAMETER Raw 44 | Return the unparsed data from the HTTP request 45 | 46 | .EXAMPLE 47 | PS C:\> Get-NetboxContact 48 | 49 | .NOTES 50 | Additional information about the function. 51 | #> 52 | 53 | [CmdletBinding(DefaultParameterSetName = 'Query')] 54 | param 55 | ( 56 | [Parameter(ParameterSetName = 'Query', 57 | Position = 0)] 58 | [string]$Name, 59 | 60 | [Parameter(ParameterSetName = 'ByID')] 61 | [uint64[]]$Id, 62 | 63 | [Parameter(ParameterSetName = 'Query')] 64 | [string]$Query, 65 | 66 | [Parameter(ParameterSetName = 'Query')] 67 | [string]$Email, 68 | 69 | [Parameter(ParameterSetName = 'Query')] 70 | [string]$Title, 71 | 72 | [Parameter(ParameterSetName = 'Query')] 73 | [string]$Phone, 74 | 75 | [Parameter(ParameterSetName = 'Query')] 76 | [string]$Address, 77 | 78 | [Parameter(ParameterSetName = 'Query')] 79 | [string]$Group, 80 | 81 | [Parameter(ParameterSetName = 'Query')] 82 | [uint64]$GroupID, 83 | 84 | [Parameter(ParameterSetName = 'Query')] 85 | [uint16]$Limit, 86 | 87 | [Parameter(ParameterSetName = 'Query')] 88 | [uint16]$Offset, 89 | 90 | [switch]$Raw 91 | ) 92 | 93 | switch ($PSCmdlet.ParameterSetName) { 94 | 'ById' { 95 | foreach ($Contact_ID in $Id) { 96 | $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'contacts', $Contact_ID)) 97 | 98 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' 99 | 100 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 101 | 102 | InvokeNetboxRequest -URI $uri -Raw:$Raw 103 | } 104 | 105 | break 106 | } 107 | 108 | default { 109 | $Segments = [System.Collections.ArrayList]::new(@('tenancy', 'contacts')) 110 | 111 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 112 | 113 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 114 | 115 | InvokeNetboxRequest -URI $uri -Raw:$Raw 116 | 117 | break 118 | } 119 | } 120 | } -------------------------------------------------------------------------------- /Functions/Virtualization/VirtualMachine/Get-NetboxVirtualMachine.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxVirtualMachine { 3 | <# 4 | .SYNOPSIS 5 | Obtains virtual machines from Netbox. 6 | 7 | .DESCRIPTION 8 | Obtains one or more virtual machines based on provided filters. 9 | 10 | .PARAMETER Limit 11 | Number of results to return per page 12 | 13 | .PARAMETER Offset 14 | The initial index from which to return the results 15 | 16 | .PARAMETER Query 17 | A general query used to search for a VM 18 | 19 | .PARAMETER Name 20 | Name of the VM 21 | 22 | .PARAMETER Id 23 | Database ID of the VM 24 | 25 | .PARAMETER Status 26 | Status of the VM 27 | 28 | .PARAMETER Tenant 29 | String value of tenant 30 | 31 | .PARAMETER Tenant_ID 32 | Database ID of the tenant. 33 | 34 | .PARAMETER Platform 35 | String value of the platform 36 | 37 | .PARAMETER Platform_ID 38 | Database ID of the platform 39 | 40 | .PARAMETER Cluster_Group 41 | String value of the cluster group. 42 | 43 | .PARAMETER Cluster_Group_Id 44 | Database ID of the cluster group. 45 | 46 | .PARAMETER Cluster_Type 47 | String value of the Cluster type. 48 | 49 | .PARAMETER Cluster_Type_Id 50 | Database ID of the cluster type. 51 | 52 | .PARAMETER Cluster_Id 53 | Database ID of the cluster. 54 | 55 | .PARAMETER Site 56 | String value of the site. 57 | 58 | .PARAMETER Site_Id 59 | Database ID of the site. 60 | 61 | .PARAMETER Role 62 | String value of the role. 63 | 64 | .PARAMETER Role_Id 65 | Database ID of the role. 66 | 67 | .PARAMETER Raw 68 | A description of the Raw parameter. 69 | 70 | .PARAMETER TenantID 71 | Database ID of tenant 72 | 73 | .PARAMETER PlatformID 74 | Database ID of the platform 75 | 76 | .PARAMETER id__in 77 | Database IDs of VMs 78 | 79 | .EXAMPLE 80 | PS C:\> Get-NetboxVirtualMachine 81 | 82 | .NOTES 83 | Additional information about the function. 84 | #> 85 | 86 | [CmdletBinding()] 87 | param 88 | ( 89 | [Alias('q')] 90 | [string]$Query, 91 | 92 | [string]$Name, 93 | 94 | [uint64[]]$Id, 95 | 96 | [object]$Status, 97 | 98 | [string]$Tenant, 99 | 100 | [uint64]$Tenant_ID, 101 | 102 | [string]$Platform, 103 | 104 | [uint64]$Platform_ID, 105 | 106 | [string]$Cluster_Group, 107 | 108 | [uint64]$Cluster_Group_Id, 109 | 110 | [string]$Cluster_Type, 111 | 112 | [uint64]$Cluster_Type_Id, 113 | 114 | [uint64]$Cluster_Id, 115 | 116 | [string]$Site, 117 | 118 | [uint64]$Site_Id, 119 | 120 | [string]$Role, 121 | 122 | [uint64]$Role_Id, 123 | 124 | [uint16]$Limit, 125 | 126 | [uint16]$Offset, 127 | 128 | [switch]$Raw 129 | ) 130 | 131 | process { 132 | if ($null -ne $Status) { 133 | $PSBoundParameters.Status = ValidateVirtualizationChoice -ProvidedValue $Status -VirtualMachineStatus 134 | } 135 | 136 | $Segments = [System.Collections.ArrayList]::new(@('virtualization', 'virtual-machines')) 137 | 138 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 139 | 140 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 141 | 142 | InvokeNetboxRequest -URI $uri -Raw:$Raw 143 | } 144 | } -------------------------------------------------------------------------------- /Tests/DCIMChoices.json: -------------------------------------------------------------------------------- 1 | {"device:face":[{"value":0,"label":"Front"},{"value":1,"label":"Rear"}],"device:status":[{"value":1,"label":"Active"},{"value":0,"label":"Offline"},{"value":2,"label":"Planned"},{"value":3,"label":"Staged"},{"value":4,"label":"Failed"},{"value":5,"label":"Inventory"}],"console-port:connection_status":[{"value":false,"label":"Planned"},{"value":true,"label":"Connected"}],"interface:form_factor":[{"value":0,"label":"Virtual"},{"value":200,"label":"Link Aggregation Group (LAG)"},{"value":800,"label":"100BASE-TX (10/100ME)"},{"value":1000,"label":"1000BASE-T (1GE)"},{"value":1150,"label":"10GBASE-T (10GE)"},{"value":1170,"label":"10GBASE-CX4 (10GE)"},{"value":1050,"label":"GBIC (1GE)"},{"value":1100,"label":"SFP (1GE)"},{"value":1200,"label":"SFP+ (10GE)"},{"value":1300,"label":"XFP (10GE)"},{"value":1310,"label":"XENPAK (10GE)"},{"value":1320,"label":"X2 (10GE)"},{"value":1350,"label":"SFP28 (25GE)"},{"value":1400,"label":"QSFP+ (40GE)"},{"value":1500,"label":"CFP (100GE)"},{"value":1510,"label":"CFP2 (100GE)"},{"value":1520,"label":"CFP4 (100GE)"},{"value":1550,"label":"Cisco CPAK (100GE)"},{"value":1600,"label":"QSFP28 (100GE)"},{"value":2600,"label":"IEEE 802.11a"},{"value":2610,"label":"IEEE 802.11b/g"},{"value":2620,"label":"IEEE 802.11n"},{"value":2630,"label":"IEEE 802.11ac"},{"value":2640,"label":"IEEE 802.11ad"},{"value":3010,"label":"SFP (1GFC)"},{"value":3020,"label":"SFP (2GFC)"},{"value":3040,"label":"SFP (4GFC)"},{"value":3080,"label":"SFP+ (8GFC)"},{"value":3160,"label":"SFP+ (16GFC)"},{"value":4000,"label":"T1 (1.544 Mbps)"},{"value":4010,"label":"E1 (2.048 Mbps)"},{"value":4040,"label":"T3 (45 Mbps)"},{"value":4050,"label":"E3 (34 Mbps)"},{"value":5000,"label":"Cisco StackWise"},{"value":5050,"label":"Cisco StackWise Plus"},{"value":5100,"label":"Cisco FlexStack"},{"value":5150,"label":"Cisco FlexStack Plus"},{"value":5200,"label":"Juniper VCP"},{"value":32767,"label":"Other"}],"interface-connection:connection_status":[{"value":false,"label":"Planned"},{"value":true,"label":"Connected"}],"interface-template:form_factor":[{"value":0,"label":"Virtual"},{"value":200,"label":"Link Aggregation Group (LAG)"},{"value":800,"label":"100BASE-TX (10/100ME)"},{"value":1000,"label":"1000BASE-T (1GE)"},{"value":1150,"label":"10GBASE-T (10GE)"},{"value":1170,"label":"10GBASE-CX4 (10GE)"},{"value":1050,"label":"GBIC (1GE)"},{"value":1100,"label":"SFP (1GE)"},{"value":1200,"label":"SFP+ (10GE)"},{"value":1300,"label":"XFP (10GE)"},{"value":1310,"label":"XENPAK (10GE)"},{"value":1320,"label":"X2 (10GE)"},{"value":1350,"label":"SFP28 (25GE)"},{"value":1400,"label":"QSFP+ (40GE)"},{"value":1500,"label":"CFP (100GE)"},{"value":1510,"label":"CFP2 (100GE)"},{"value":1520,"label":"CFP4 (100GE)"},{"value":1550,"label":"Cisco CPAK (100GE)"},{"value":1600,"label":"QSFP28 (100GE)"},{"value":2600,"label":"IEEE 802.11a"},{"value":2610,"label":"IEEE 802.11b/g"},{"value":2620,"label":"IEEE 802.11n"},{"value":2630,"label":"IEEE 802.11ac"},{"value":2640,"label":"IEEE 802.11ad"},{"value":3010,"label":"SFP (1GFC)"},{"value":3020,"label":"SFP (2GFC)"},{"value":3040,"label":"SFP (4GFC)"},{"value":3080,"label":"SFP+ (8GFC)"},{"value":3160,"label":"SFP+ (16GFC)"},{"value":4000,"label":"T1 (1.544 Mbps)"},{"value":4010,"label":"E1 (2.048 Mbps)"},{"value":4040,"label":"T3 (45 Mbps)"},{"value":4050,"label":"E3 (34 Mbps)"},{"value":5000,"label":"Cisco StackWise"},{"value":5050,"label":"Cisco StackWise Plus"},{"value":5100,"label":"Cisco FlexStack"},{"value":5150,"label":"Cisco FlexStack Plus"},{"value":5200,"label":"Juniper VCP"},{"value":32767,"label":"Other"}],"power-port:connection_status":[{"value":false,"label":"Planned"},{"value":true,"label":"Connected"}],"rack:type":[{"value":100,"label":"2-post frame"},{"value":200,"label":"4-post frame"},{"value":300,"label":"4-post cabinet"},{"value":1000,"label":"Wall-mounted frame"},{"value":1100,"label":"Wall-mounted cabinet"}],"rack:width":[{"value":19,"label":"19 inches"},{"value":23,"label":"23 inches"}]} 2 | -------------------------------------------------------------------------------- /Functions/DCIM/Interfaces/Set-NetboxDCIMInterface.ps1: -------------------------------------------------------------------------------- 1 | function Set-NetboxDCIMInterface { 2 | [CmdletBinding(ConfirmImpact = 'Medium', 3 | SupportsShouldProcess = $true)] 4 | [OutputType([pscustomobject])] 5 | param 6 | ( 7 | [Parameter(Mandatory = $true, 8 | ValueFromPipelineByPropertyName = $true)] 9 | [uint64[]]$Id, 10 | 11 | [uint64]$Device, 12 | 13 | [string]$Name, 14 | 15 | [bool]$Enabled, 16 | 17 | [object]$Form_Factor, 18 | 19 | [ValidateSet('virtual', 'bridge', 'lag', '100base-tx', '1000base-t', '2.5gbase-t', '5gbase-t', '10gbase-t', '10gbase-cx4', '1000base-x-gbic', '1000base-x-sfp', '10gbase-x-sfpp', '10gbase-x-xfp', '10gbase-x-xenpak', '10gbase-x-x2', '25gbase-x-sfp28', '50gbase-x-sfp56', '40gbase-x-qsfpp', '50gbase-x-sfp28', '100gbase-x-cfp', '100gbase-x-cfp2', '200gbase-x-cfp2', '100gbase-x-cfp4', '100gbase-x-cpak', '100gbase-x-qsfp28', '200gbase-x-qsfp56', '400gbase-x-qsfpdd', '400gbase-x-osfp', '1000base-kx', '10gbase-kr', '10gbase-kx4', '25gbase-kr', '40gbase-kr4', '50gbase-kr', '100gbase-kp4', '100gbase-kr2', '100gbase-kr4', 'ieee802.11a', 'ieee802.11g', 'ieee802.11n', 'ieee802.11ac', 'ieee802.11ad', 'ieee802.11ax', 'ieee802.11ay', 'ieee802.15.1', 'other-wireless', 'gsm', 'cdma', 'lte', 'sonet-oc3', 'sonet-oc12', 'sonet-oc48', 'sonet-oc192', 'sonet-oc768', 'sonet-oc1920', 'sonet-oc3840', '1gfc-sfp', '2gfc-sfp', '4gfc-sfp', '8gfc-sfpp', '16gfc-sfpp', '32gfc-sfp28', '64gfc-qsfpp', '128gfc-qsfp28', 'infiniband-sdr', 'infiniband-ddr', 'infiniband-qdr', 'infiniband-fdr10', 'infiniband-fdr', 'infiniband-edr', 'infiniband-hdr', 'infiniband-ndr', 'infiniband-xdr', 't1', 'e1', 't3', 'e3', 'xdsl', 'docsis', 'gpon', 'xg-pon', 'xgs-pon', 'ng-pon2', 'epon', '10g-epon', 'cisco-stackwise', 'cisco-stackwise-plus', 'cisco-flexstack', 'cisco-flexstack-plus', 'cisco-stackwise-80', 'cisco-stackwise-160', 'cisco-stackwise-320', 'cisco-stackwise-480', 'juniper-vcp', 'extreme-summitstack', 'extreme-summitstack-128', 'extreme-summitstack-256', 'extreme-summitstack-512', 'other', IgnoreCase = $true)] 20 | [string]$Type, 21 | 22 | [uint16]$MTU, 23 | 24 | [string]$MAC_Address, 25 | 26 | [bool]$MGMT_Only, 27 | 28 | [uint64]$LAG, 29 | 30 | [string]$Description, 31 | 32 | [ValidateSet('Access', 'Tagged', 'Tagged All', '100', '200', '300', IgnoreCase = $true)] 33 | [string]$Mode, 34 | 35 | [ValidateRange(1, 4094)] 36 | [uint16]$Untagged_VLAN, 37 | 38 | [ValidateRange(1, 4094)] 39 | [uint16[]]$Tagged_VLANs, 40 | 41 | [switch]$Force 42 | ) 43 | 44 | begin { 45 | if (-not [System.String]::IsNullOrWhiteSpace($Mode)) { 46 | $PSBoundParameters.Mode = switch ($Mode) { 47 | 'Access' { 48 | 100 49 | break 50 | } 51 | 52 | 'Tagged' { 53 | 200 54 | break 55 | } 56 | 57 | 'Tagged All' { 58 | 300 59 | break 60 | } 61 | 62 | default { 63 | $_ 64 | } 65 | } 66 | } 67 | } 68 | 69 | process { 70 | foreach ($InterfaceId in $Id) { 71 | $CurrentInterface = Get-NetboxDCIMInterface -Id $InterfaceId -ErrorAction Stop 72 | 73 | $Segments = [System.Collections.ArrayList]::new(@('dcim', 'interfaces', $CurrentInterface.Id)) 74 | 75 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' 76 | 77 | $URI = BuildNewURI -Segments $Segments 78 | 79 | if ($Force -or $pscmdlet.ShouldProcess("Interface ID $($CurrentInterface.Id)", "Set")) { 80 | InvokeNetboxRequest -URI $URI -Body $URIComponents.Parameters -Method PATCH 81 | } 82 | } 83 | } 84 | 85 | end { 86 | 87 | } 88 | } -------------------------------------------------------------------------------- /Functions/Circuits/Circuits/Get-NetboxCircuit.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxCircuit { 3 | <# 4 | .SYNOPSIS 5 | Gets one or more circuits 6 | 7 | .DESCRIPTION 8 | A detailed description of the Get-NetboxCircuit function. 9 | 10 | .PARAMETER Id 11 | Database ID of circuit. This will query for exactly the IDs provided 12 | 13 | .PARAMETER CID 14 | Circuit ID 15 | 16 | .PARAMETER InstallDate 17 | Date of installation 18 | 19 | .PARAMETER CommitRate 20 | Committed rate in Kbps 21 | 22 | .PARAMETER Query 23 | A raw search query... As if you were searching the web site 24 | 25 | .PARAMETER Provider 26 | The name or ID of the provider. Provide either [string] or [uint64]. String will search provider names, integer will search database IDs 27 | 28 | .PARAMETER Type 29 | Type of circuit. Provide either [string] or [uint64]. String will search provider type names, integer will search database IDs 30 | 31 | .PARAMETER Site 32 | Location/site of circuit. Provide either [string] or [uint64]. String will search site names, integer will search database IDs 33 | 34 | .PARAMETER Tenant 35 | Tenant assigned to circuit. Provide either [string] or [uint64]. String will search tenant names, integer will search database IDs 36 | 37 | .PARAMETER Limit 38 | A description of the Limit parameter. 39 | 40 | .PARAMETER Offset 41 | A description of the Offset parameter. 42 | 43 | .PARAMETER Raw 44 | A description of the Raw parameter. 45 | 46 | .PARAMETER ID__IN 47 | Multiple unique DB IDs to retrieve 48 | 49 | .EXAMPLE 50 | PS C:\> Get-NetboxCircuit 51 | 52 | .NOTES 53 | Additional information about the function. 54 | #> 55 | 56 | [CmdletBinding(DefaultParameterSetName = 'Query')] 57 | param 58 | ( 59 | [Parameter(ParameterSetName = 'ById')] 60 | [uint64[]]$Id, 61 | 62 | [Parameter(ParameterSetName = 'Query')] 63 | [string]$CID, 64 | 65 | [Parameter(ParameterSetName = 'Query')] 66 | [datetime]$InstallDate, 67 | 68 | [Parameter(ParameterSetName = 'Query')] 69 | [uint64]$CommitRate, 70 | 71 | [Parameter(ParameterSetName = 'Query')] 72 | [string]$Query, 73 | 74 | [Parameter(ParameterSetName = 'Query')] 75 | [object]$Provider, 76 | 77 | [Parameter(ParameterSetName = 'Query')] 78 | [object]$Type, 79 | 80 | [Parameter(ParameterSetName = 'Query')] 81 | [string]$Site, 82 | 83 | [Parameter(ParameterSetName = 'Query')] 84 | [string]$Tenant, 85 | 86 | [Parameter(ParameterSetName = 'Query')] 87 | [uint16]$Limit, 88 | 89 | [Parameter(ParameterSetName = 'Query')] 90 | [uint16]$Offset, 91 | 92 | [switch]$Raw 93 | ) 94 | 95 | process { 96 | switch ($PSCmdlet.ParameterSetName) { 97 | 'ById' { 98 | foreach ($i in $ID) { 99 | $Segments = [System.Collections.ArrayList]::new(@('circuits', 'circuits', $i)) 100 | 101 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters -SkipParameterByName "Id" 102 | 103 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 104 | 105 | InvokeNetboxRequest -URI $URI -Raw:$Raw 106 | } 107 | } 108 | 109 | default { 110 | $Segments = [System.Collections.ArrayList]::new(@('circuits', 'circuits')) 111 | 112 | $URIComponents = BuildURIComponents -URISegments $Segments.Clone() -ParametersDictionary $PSBoundParameters 113 | 114 | $URI = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 115 | 116 | InvokeNetboxRequest -URI $URI -Raw:$Raw 117 | } 118 | } 119 | } 120 | } -------------------------------------------------------------------------------- /Tests/DCIM.Platforms.Tests.ps1: -------------------------------------------------------------------------------- 1 | 2 | [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingConvertToSecureStringWithPlainText", "")] 3 | param 4 | ( 5 | ) 6 | Import-Module Pester 7 | Remove-Module NetboxPS -Force -ErrorAction SilentlyContinue 8 | 9 | $ModulePath = "$PSScriptRoot\..\dist\NetboxPS.psd1" 10 | 11 | if (Test-Path $ModulePath) { 12 | Import-Module $ModulePath -ErrorAction Stop 13 | } 14 | 15 | Describe -Name "DCIM Platforms Tests" -Tag 'DCIM', 'platforms' -Fixture { 16 | Mock -CommandName 'CheckNetboxIsConnected' -Verifiable -ModuleName 'NetboxPS' -MockWith { 17 | return $true 18 | } 19 | 20 | Mock -CommandName 'Invoke-RestMethod' -Verifiable -ModuleName 'NetboxPS' -MockWith { 21 | # Return a hashtable of the items we would normally pass to Invoke-RestMethod 22 | return [ordered]@{ 23 | 'Method' = $Method 24 | 'Uri' = $Uri 25 | 'Headers' = $Headers 26 | 'Timeout' = $Timeout 27 | 'ContentType' = $ContentType 28 | 'Body' = $Body 29 | } 30 | } 31 | 32 | Mock -CommandName 'Get-NetboxCredential' -Verifiable -ModuleName 'NetboxPS' -MockWith { 33 | return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force)) 34 | } 35 | 36 | Mock -CommandName 'Get-NetboxHostname' -Verifiable -ModuleName 'NetboxPS' -MockWith { 37 | return 'netbox.domain.com' 38 | } 39 | 40 | InModuleScope -ModuleName 'NetboxPS' -ScriptBlock { 41 | Context -Name "Get-NetboxDCIMPlatform" -Fixture { 42 | It "Should request the default number of platforms" { 43 | $Result = Get-NetboxDCIMPlatform 44 | 45 | Assert-VerifiableMock 46 | Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly 47 | 48 | $Result.Method | Should -Be 'GET' 49 | $Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/platforms/' 50 | $Result.Headers.Keys.Count | Should -BeExactly 1 51 | } 52 | 53 | It "Should request with a limit and offset" { 54 | $Result = Get-NetboxDCIMPlatform -Limit 10 -Offset 100 55 | 56 | Assert-VerifiableMock 57 | Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly 58 | 59 | $Result.Method | Should -Be 'GET' 60 | $Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/platforms/?offset=100&limit=10' 61 | $Result.Headers.Keys.Count | Should -BeExactly 1 62 | } 63 | 64 | It "Should request with a platform name" { 65 | $Result = Get-NetboxDCIMPlatform -Name "Windows Server 2016" 66 | 67 | Assert-VerifiableMock 68 | Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly 69 | 70 | $Result.Method | Should -Be 'GET' 71 | $Result.Uri | Should -Be 'https://netbox.domain.com/api/dcim/platforms/?name=Windows+Server+2016' 72 | $Result.Headers.Keys.Count | Should -BeExactly 1 73 | } 74 | 75 | It "Should request a platform by manufacturer" { 76 | $Result = Get-NetboxDCIMPlatform -Manufacturer 'Cisco' 77 | 78 | Assert-VerifiableMock 79 | Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly 80 | 81 | $Result.Method | Should -Be 'GET' 82 | $Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/platforms/?manufacturer=Cisco' 83 | $Result.Headers.Keys.Count | Should -BeExactly 1 84 | } 85 | 86 | It "Should request a platform by ID" { 87 | $Result = Get-NetboxDCIMPlatform -Id 10 88 | 89 | Assert-VerifiableMock 90 | Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 1 -Scope 'It' -Exactly 91 | 92 | $Result.Method | Should -Be 'GET' 93 | $Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/platforms/10/' 94 | $Result.Headers.Keys.Count | Should -BeExactly 1 95 | } 96 | 97 | It "Should request multiple platforms by ID" { 98 | $Result = Get-NetboxDCIMPlatform -Id 10, 20 99 | 100 | Assert-VerifiableMock 101 | Assert-MockCalled -CommandName 'Invoke-RestMethod' -Times 2 -Scope 'It' -Exactly 102 | 103 | $Result.Method | Should -Be 'GET', 'GET' 104 | $Result.Uri | Should -BeExactly 'https://netbox.domain.com/api/dcim/platforms/10/', 'https://netbox.domain.com/api/dcim/platforms/20/' 105 | $Result.Headers.Keys.Count | Should -BeExactly 2 106 | } 107 | } 108 | } 109 | } 110 | 111 | 112 | 113 | 114 | -------------------------------------------------------------------------------- /Functions/IPAM/Prefix/Get-NetboxIPAMPrefix.ps1: -------------------------------------------------------------------------------- 1 | 2 | function Get-NetboxIPAMPrefix { 3 | <# 4 | .SYNOPSIS 5 | A brief description of the Get-NetboxIPAMPrefix function. 6 | 7 | .DESCRIPTION 8 | A detailed description of the Get-NetboxIPAMPrefix function. 9 | 10 | .PARAMETER Query 11 | A description of the Query parameter. 12 | 13 | .PARAMETER Id 14 | A description of the Id parameter. 15 | 16 | .PARAMETER Limit 17 | A description of the Limit parameter. 18 | 19 | .PARAMETER Offset 20 | A description of the Offset parameter. 21 | 22 | .PARAMETER Family 23 | A description of the Family parameter. 24 | 25 | .PARAMETER Is_Pool 26 | A description of the Is_Pool parameter. 27 | 28 | .PARAMETER Within 29 | Should be a CIDR notation prefix such as '10.0.0.0/16' 30 | 31 | .PARAMETER Within_Include 32 | Should be a CIDR notation prefix such as '10.0.0.0/16' 33 | 34 | .PARAMETER Contains 35 | A description of the Contains parameter. 36 | 37 | .PARAMETER Mask_Length 38 | CIDR mask length value 39 | 40 | .PARAMETER VRF 41 | A description of the VRF parameter. 42 | 43 | .PARAMETER VRF_Id 44 | A description of the VRF_Id parameter. 45 | 46 | .PARAMETER Tenant 47 | A description of the Tenant parameter. 48 | 49 | .PARAMETER Tenant_Id 50 | A description of the Tenant_Id parameter. 51 | 52 | .PARAMETER Site 53 | A description of the Site parameter. 54 | 55 | .PARAMETER Site_Id 56 | A description of the Site_Id parameter. 57 | 58 | .PARAMETER Vlan_VId 59 | A description of the Vlan_VId parameter. 60 | 61 | .PARAMETER Vlan_Id 62 | A description of the Vlan_Id parameter. 63 | 64 | .PARAMETER Status 65 | A description of the Status parameter. 66 | 67 | .PARAMETER Role 68 | A description of the Role parameter. 69 | 70 | .PARAMETER Role_Id 71 | A description of the Role_Id parameter. 72 | 73 | .PARAMETER Raw 74 | A description of the Raw parameter. 75 | 76 | .EXAMPLE 77 | PS C:\> Get-NetboxIPAMPrefix 78 | 79 | .NOTES 80 | Additional information about the function. 81 | #> 82 | 83 | [CmdletBinding(DefaultParameterSetName = 'Query')] 84 | param 85 | ( 86 | [Parameter(ParameterSetName = 'Query', 87 | Position = 0)] 88 | [string]$Prefix, 89 | 90 | [Parameter(ParameterSetName = 'Query')] 91 | [string]$Query, 92 | 93 | [Parameter(ParameterSetName = 'ByID')] 94 | [uint64[]]$Id, 95 | 96 | [Parameter(ParameterSetName = 'Query')] 97 | [object]$Family, 98 | 99 | [Parameter(ParameterSetName = 'Query')] 100 | [boolean]$Is_Pool, 101 | 102 | [Parameter(ParameterSetName = 'Query')] 103 | [string]$Within, 104 | 105 | [Parameter(ParameterSetName = 'Query')] 106 | [string]$Within_Include, 107 | 108 | [Parameter(ParameterSetName = 'Query')] 109 | [string]$Contains, 110 | 111 | [Parameter(ParameterSetName = 'Query')] 112 | [ValidateRange(0, 127)] 113 | [byte]$Mask_Length, 114 | 115 | [Parameter(ParameterSetName = 'Query')] 116 | [string]$VRF, 117 | 118 | [Parameter(ParameterSetName = 'Query')] 119 | [uint64]$VRF_Id, 120 | 121 | [Parameter(ParameterSetName = 'Query')] 122 | [string]$Tenant, 123 | 124 | [Parameter(ParameterSetName = 'Query')] 125 | [uint64]$Tenant_Id, 126 | 127 | [Parameter(ParameterSetName = 'Query')] 128 | [string]$Site, 129 | 130 | [Parameter(ParameterSetName = 'Query')] 131 | [uint64]$Site_Id, 132 | 133 | [Parameter(ParameterSetName = 'Query')] 134 | [string]$Vlan_VId, 135 | 136 | [Parameter(ParameterSetName = 'Query')] 137 | [uint64]$Vlan_Id, 138 | 139 | [Parameter(ParameterSetName = 'Query')] 140 | [object]$Status, 141 | 142 | [Parameter(ParameterSetName = 'Query')] 143 | [string]$Role, 144 | 145 | [Parameter(ParameterSetName = 'Query')] 146 | [uint64]$Role_Id, 147 | 148 | [Parameter(ParameterSetName = 'Query')] 149 | [uint16]$Limit, 150 | 151 | [Parameter(ParameterSetName = 'Query')] 152 | [uint16]$Offset, 153 | 154 | [switch]$Raw 155 | ) 156 | 157 | # if ($null -ne $Family) { 158 | # $PSBoundParameters.Family = ValidateIPAMChoice -ProvidedValue $Family -PrefixFamily 159 | # } 160 | # 161 | # if ($null -ne $Status) { 162 | # $PSBoundParameters.Status = ValidateIPAMChoice -ProvidedValue $Status -PrefixStatus 163 | # } 164 | 165 | switch ($PSCmdlet.ParameterSetName) { 166 | 'ById' { 167 | foreach ($Prefix_ID in $Id) { 168 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'prefixes', $Prefix_ID)) 169 | 170 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters -SkipParameterByName 'Id' 171 | 172 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 173 | 174 | InvokeNetboxRequest -URI $uri -Raw:$Raw 175 | } 176 | 177 | break 178 | } 179 | 180 | default { 181 | $Segments = [System.Collections.ArrayList]::new(@('ipam', 'prefixes')) 182 | 183 | $URIComponents = BuildURIComponents -URISegments $Segments -ParametersDictionary $PSBoundParameters 184 | 185 | $uri = BuildNewURI -Segments $URIComponents.Segments -Parameters $URIComponents.Parameters 186 | 187 | InvokeNetboxRequest -URI $uri -Raw:$Raw 188 | 189 | break 190 | } 191 | } 192 | } -------------------------------------------------------------------------------- /Functions/Setup/Connect-NetboxAPI.ps1: -------------------------------------------------------------------------------- 1 | function Connect-NetboxAPI { 2 | <# 3 | .SYNOPSIS 4 | Connects to the Netbox API and ensures Credential work properly 5 | 6 | .DESCRIPTION 7 | Connects to the Netbox API and ensures Credential work properly 8 | 9 | .PARAMETER Hostname 10 | The hostname for the resource such as netbox.domain.com 11 | 12 | .PARAMETER Credential 13 | Credential object containing the API key in the password. Username is not applicable 14 | 15 | .PARAMETER Scheme 16 | Scheme for the URI such as HTTP or HTTPS. Defaults to HTTPS 17 | 18 | .PARAMETER Port 19 | Port for the resource. Value between 1-65535 20 | 21 | .PARAMETER URI 22 | The full URI for the resource such as "https://netbox.domain.com:8443" 23 | 24 | .PARAMETER SkipCertificateCheck 25 | A description of the SkipCertificateCheck parameter. 26 | 27 | .PARAMETER TimeoutSeconds 28 | The number of seconds before the HTTP call times out. Defaults to 30 seconds 29 | 30 | .EXAMPLE 31 | PS C:\> Connect-NetboxAPI -Hostname "netbox.domain.com" 32 | 33 | This will prompt for Credential, then proceed to attempt a connection to Netbox 34 | 35 | .NOTES 36 | Additional information about the function. 37 | #> 38 | 39 | [CmdletBinding(DefaultParameterSetName = 'Manual')] 40 | param 41 | ( 42 | [Parameter(ParameterSetName = 'Manual', 43 | Mandatory = $true)] 44 | [string]$Hostname, 45 | 46 | [Parameter(Mandatory = $false)] 47 | [pscredential]$Credential, 48 | 49 | [Parameter(ParameterSetName = 'Manual')] 50 | [ValidateSet('https', 'http', IgnoreCase = $true)] 51 | [string]$Scheme = 'https', 52 | 53 | [Parameter(ParameterSetName = 'Manual')] 54 | [uint16]$Port = 443, 55 | 56 | [Parameter(ParameterSetName = 'URI', 57 | Mandatory = $true)] 58 | [string]$URI, 59 | 60 | [Parameter(Mandatory = $false)] 61 | [switch]$SkipCertificateCheck = $false, 62 | 63 | [ValidateNotNullOrEmpty()] 64 | [ValidateRange(1, 65535)] 65 | [uint16]$TimeoutSeconds = 30 66 | ) 67 | 68 | if (-not $Credential) { 69 | try { 70 | $Credential = Get-NetboxCredential -ErrorAction Stop 71 | } catch { 72 | # Credentials are not set... Try to obtain from the user 73 | if (-not ($Credential = Get-Credential -UserName 'username-not-applicable' -Message "Enter token for Netbox")) { 74 | throw "Token is necessary to connect to a Netbox API." 75 | } 76 | } 77 | } 78 | 79 | $invokeParams = @{ SkipCertificateCheck = $SkipCertificateCheck; } 80 | 81 | if ("Desktop" -eq $PSVersionTable.PsEdition) { 82 | #Remove -SkipCertificateCheck from Invoke Parameter (not supported <= PS 5) 83 | $invokeParams.remove("SkipCertificateCheck") 84 | } 85 | 86 | #for PowerShell (<=) 5 (Desktop), Enable TLS 1.1, 1.2 and Disable SSL chain trust 87 | if ("Desktop" -eq $PSVersionTable.PsEdition) { 88 | #Add System.web (Need for ParseQueryString) 89 | Add-Type -AssemblyName System.Web 90 | #Enable TLS 1.1 and 1.2 91 | Set-NetboxCipherSSL 92 | if ($SkipCertificateCheck) { 93 | #Disable SSL chain trust... 94 | Set-NetboxuntrustedSSL 95 | } 96 | } 97 | 98 | switch ($PSCmdlet.ParameterSetName) { 99 | 'Manual' { 100 | $uriBuilder = [System.UriBuilder]::new($Scheme, $Hostname, $Port) 101 | } 102 | 103 | 'URI' { 104 | $uriBuilder = [System.UriBuilder]::new($URI) 105 | if ([string]::IsNullOrWhiteSpace($uriBuilder.Host)) { 106 | throw "URI appears to be invalid. Must be in format [host.name], [scheme://host.name], or [scheme://host.name:port]" 107 | } 108 | } 109 | } 110 | 111 | $null = Set-NetboxHostName -Hostname $uriBuilder.Host 112 | $null = Set-NetboxCredential -Credential $Credential 113 | $null = Set-NetboxHostScheme -Scheme $uriBuilder.Scheme 114 | $null = Set-NetboxHostPort -Port $uriBuilder.Port 115 | $null = Set-NetboxInvokeParams -invokeParams $invokeParams 116 | $null = Set-NetboxTimeout -TimeoutSeconds $TimeoutSeconds 117 | 118 | try { 119 | Write-Verbose "Verifying API connectivity..." 120 | $null = VerifyAPIConnectivity 121 | } catch { 122 | Write-Verbose "Failed to connect. Generating error" 123 | Write-Verbose $_.Exception.Message 124 | if (($_.Exception.Response) -and ($_.Exception.Response.StatusCode -eq 403)) { 125 | throw "Invalid token" 126 | } else { 127 | throw $_ 128 | } 129 | } 130 | 131 | # Write-Verbose "Caching API definition" 132 | # $script:NetboxConfig.APIDefinition = Get-NetboxAPIDefinition 133 | # 134 | # if ([version]$script:NetboxConfig.APIDefinition.info.version -lt 2.8) { 135 | # $Script:NetboxConfig.Connected = $false 136 | # throw "Netbox version is incompatible with this PS module. Requires >=2.8.*, found version $($script:NetboxConfig.APIDefinition.info.version)" 137 | # } 138 | 139 | Write-Verbose "Checking Netbox version compatibility" 140 | $script:NetboxConfig.NetboxVersion = Get-NetboxVersion 141 | if ([version]$script:NetboxConfig.NetboxVersion.'netbox-version' -lt 2.8) { 142 | $Script:NetboxConfig.Connected = $false 143 | throw "Netbox version is incompatible with this PS module. Requires >=2.8.*, found version $($script:NetboxConfig.NetboxVersion.'netbox-version')" 144 | } else { 145 | Write-Verbose "Found compatible version [$($script:NetboxConfig.NetboxVersion.'netbox-version')]!" 146 | } 147 | 148 | $script:NetboxConfig.Connected = $true 149 | Write-Verbose "Successfully connected!" 150 | 151 | $script:NetboxConfig.ContentTypes = Get-NetboxContentType -Limit 500 152 | 153 | Write-Verbose "Connection process completed" 154 | } -------------------------------------------------------------------------------- /deploy.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Concatenate files into single PSM1 and PSD1 files 4 | 5 | .DESCRIPTION 6 | Concatenate all ps1 files in the Functions directory, plus the root PSM1, 7 | into a single PSM1 file in the NetboxPS directory. 8 | 9 | By default, this script will increment version by 0.0.1 10 | 11 | .PARAMETER SkipVersion 12 | Do not increment the version. 13 | 14 | .PARAMETER VersionIncrease 15 | Increase the version by a user defined amount 16 | 17 | .PARAMETER NewVersion 18 | Override the new version with this version 19 | 20 | .PARAMETER Environment 21 | A description of the Environment parameter. 22 | 23 | .PARAMETER ResetCurrentEnvironment 24 | A description of the ResetCurrentEnvironment parameter. 25 | 26 | .EXAMPLE 27 | Use all defaults and concatenate all files 28 | 29 | .\deploy.ps1 30 | 31 | .EXAMPLE 32 | Increment the version by 0.2.0. Given version 1.2.0, the resulting version will be 1.4.0 33 | 34 | .\deploy.ps1 -VersionIncrease 0.2.0 35 | #> 36 | [CmdletBinding(DefaultParameterSetName = 'IncreaseVersion')] 37 | [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingWriteHost", "")] 38 | [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "")] 39 | param 40 | ( 41 | [Parameter(ParameterSetName = 'SkipVersion')] 42 | [switch]$SkipVersion, 43 | 44 | [Parameter(ParameterSetName = 'IncreaseVersion')] 45 | [version]$VersionIncrease = "0.0.1", 46 | 47 | [Parameter(ParameterSetName = 'SetVersion')] 48 | [version]$NewVersion, 49 | 50 | [ValidateSet('dev', 'development', 'prod', 'production', IgnoreCase = $true)] 51 | [string]$Environment = 'development', 52 | [switch]$ResetCurrentEnvironment 53 | ) 54 | 55 | Write-Host "Beginning deployment" -ForegroundColor Green 56 | Write-Host "Importing required modules" -ForegroundColor Green 57 | 58 | Import-Module "PSScriptAnalyzer", "Microsoft.PowerShell.Utility" -ErrorAction Stop 59 | 60 | 61 | $ModuleName = 'NetboxPS' 62 | $ConcatenatedFilePath = "$PSScriptRoot\concatenated.ps1" 63 | $FunctionPath = "$PSScriptRoot\Functions" 64 | $OutputDirectory = "$PSScriptRoot\$ModuleName" 65 | $PSD1OutputPath = "$OutputDirectory\$ModuleName.psd1" 66 | $PSM1OutputPath = "$OutputDirectory\$ModuleName.psm1" 67 | $PS1FunctionFiles = Get-ChildItem $FunctionPath -Filter "*.ps1" -Recurse | Sort-Object Name 68 | 69 | 70 | Write-Host "Removing whitespace from files" -ForegroundColor Green 71 | Invoke-ScriptAnalyzer -Path $FunctionPath -IncludeRule 'PSAvoidTrailingWhitespace' -Recurse -Fix 72 | 73 | 74 | Write-Host "Concatenating [$($PS1FunctionFiles.Count)] PS1 files from $FunctionPath" 75 | 76 | "" | Out-File -FilePath $ConcatenatedFilePath -Encoding utf8 77 | 78 | $Counter = 0 79 | foreach ($File in $PS1FunctionFiles) { 80 | $Counter++ 81 | 82 | try { 83 | Write-Host (" Adding file {0:D2}/{1:D2}: $($File.Name)" -f $Counter, $PS1FunctionFiles.Count) 84 | 85 | "`r`n#region File $($File.Name)`r`n" | Out-File -FilePath $ConcatenatedFilePath -Encoding utf8 -Append -ErrorAction Stop 86 | 87 | Get-Content $File.FullName -Encoding UTF8 -ErrorAction Stop | Out-File -FilePath $ConcatenatedFilePath -Encoding utf8 -Append -ErrorAction Stop 88 | 89 | "`r`n#endregion" | Out-File -FilePath $ConcatenatedFilePath -Encoding utf8 -Append -ErrorAction Stop 90 | } catch { 91 | Write-Host "FAILED TO WRITE CONCATENATED FILE: $($_.Exception.Message): $($_.TargetObject)" -ForegroundColor Red 92 | return 93 | } 94 | } 95 | 96 | "" | Out-File -FilePath $ConcatenatedFilePath -Encoding utf8 -Append 97 | 98 | 99 | Write-Host " Adding psm1" 100 | Get-Content "$PSScriptRoot\$ModuleName.psm1" | Out-File -FilePath $ConcatenatedFilePath -Encoding UTF8 -Append 101 | 102 | 103 | $PSDManifest = Import-PowerShellDataFile -Path "$PSScriptRoot\$ModuleName.psd1" 104 | # Get the version from the PSD1 105 | [version]$CurrentVersion = $PSDManifest.ModuleVersion 106 | 107 | $UpdateModuleManifestSplat = @{ 108 | Path = "$PSScriptRoot\$ModuleName.psd1" 109 | ErrorAction = 'Stop' 110 | } 111 | 112 | if ($Environment -ilike 'dev*') { 113 | Write-Host "Exporting all functions for development" 114 | $UpdateModuleManifestSplat['FunctionsToExport'] = $PS1FunctionFiles.BaseName 115 | } else { 116 | $UpdateModuleManifestSplat['FunctionsToExport'] = ($PS1FunctionFiles.BaseName | Where-Object { $_ -like '*-*' }) 117 | } 118 | 119 | 120 | Write-Host "Comparing versions" 121 | switch ($PSCmdlet.ParameterSetName) { 122 | "SkipVersion" { 123 | # Dont do anything with the PSD 124 | Write-Host " Skipping version update, maintaining version [$CurrentVersion]" 125 | 126 | break 127 | } 128 | 129 | "IncreaseVersion" { 130 | # Calculate the new version 131 | [version]$NewVersion = "{0}.{1}.{2}" -f ($CurrentVersion.Major + $VersionIncrease.Major), ($CurrentVersion.Minor + $VersionIncrease.Minor), ($CurrentVersion.Build + $VersionIncrease.Build) 132 | 133 | Write-Host " Updating version from [$CurrentVersion] to [$NewVersion]" 134 | 135 | # Replace the version number in the content 136 | $UpdateModuleManifestSplat['ModuleVersion'] = $NewVersion 137 | 138 | break 139 | } 140 | 141 | "SetVersion" { 142 | Write-Host " Updating version from [$CurrentVersion] to [$NewVersion]" 143 | 144 | # Replace the version number in the content 145 | $UpdateModuleManifestSplat['ModuleVersion'] = $NewVersion 146 | 147 | break 148 | } 149 | } 150 | 151 | 152 | Write-Host "Updating Module Manifest" 153 | Update-ModuleManifest @UpdateModuleManifestSplat 154 | 155 | Write-Host " Removing trailing whitespaces from psd1" 156 | Invoke-ScriptAnalyzer -Path $UpdateModuleManifestSplat.Path -IncludeRule 'PSAvoidTrailingWhitespace' -Fix 157 | 158 | 159 | if (-not (Test-Path $OutputDirectory)) { 160 | try { 161 | Write-Warning "Creating output directory [$OutputDirectory]" 162 | $null = New-Item -Path $OutputDirectory -ItemType Directory -Force 163 | } catch { 164 | throw "Failed to create output directory [$OutputDirectory]: $($_.Exception.Message)" 165 | } 166 | } 167 | 168 | 169 | Write-Host " Copying psd1" 170 | Copy-Item -Path "$PSScriptRoot\$ModuleName.psd1" -Destination $PSD1OutputPath -Force 171 | 172 | Write-Host " Copying psm1" 173 | Copy-Item -Path $ConcatenatedFilePath -Destination $PSM1OutputPath -Force 174 | 175 | 176 | Write-Host "Deployment complete" -ForegroundColor Green 177 | if ($ResetCurrentEnvironment) { 178 | Write-Warning "Running commands to reset current environment" 179 | if (Get-Module 'NetboxPS') { 180 | Remove-Module NetboxPS -Force 181 | } 182 | 183 | Write-Host " Reimporting module" 184 | Import-Module $PSM1OutputPath, $PSD1OutputPath -Force -ErrorAction Stop 185 | 186 | Write-Host "Reset complete" -ForegroundColor Green 187 | } 188 | --------------------------------------------------------------------------------