Powershell API functions for Netbox
This repository will keep updating as the functions I use internally at work start growing.
At this moment it is mostly used for Synchronization activities to retrieve data and put it somewhere else, but when we start making changes to objects this will be further updated.
Install-Module -Name PSNetboxFunctionsThis will also install Dependency
- PSLoggingFunctions (https://github.com/rakelord/PSLoggingFunctions)
- PSHelpFunctions (https://github.com/rakelord/PSHelpFunctions)
Just run the OfflineInstallation.ps1
PS: You need to download all the files into the same directory and then run the script, they will then be copied to the Users PSModuleDirectory.
First you are going to have to create a API Token which is explained in Netbox official API documentation.
- -LogToFile parameter is connected to the PSLoggingFunctions module and is used for easy logging.
Connect-NetboxAPI -Url "<YOUR NETBOX URL>" -Token "<API TOKEN>" -LogToFile "<True/False>"You will retrieve a global variable in your script called '$netboxAuthenticationHeader' the variable can be used with your own Invoke-RestMethod commands if needed, otherwise you can use the module functions which has this variable implemented.
The parameter APIEndpoint is based on the Netbox documentation, so for example if you want to retrieve all devices from dcim you set it to '/api/dcim/devices/' if you want to get tenants you type '/api/tenancy/tenants/'
API Permissions need to be set for the objects you want to retrieve!
Get-NetboxObjects -APIEndpoint "<API ENDPOINT>" -LogToFile "<True/False>"API Permissions need to be set for the objects you want to delete!
#Example deleting a Tenant with ID 235
Remove-NetboxObject -APIEndpoint "/api/tenancy/tenants/" -ObjectID "235" -LogToFile $True# If you want to add something to the customer value that does not already exist, for example a customer id.
$customAddition = @{
custom_fields = @{
customer_id = "123123"
}
}
New-NetboxTenant -tenantName "TurboTenant" -tags ("tag123","cooltenant") -objectData $customAddition -LogToFile $false
#if you have nothing extra to add just skip objectData and if there are no tags, you can skip that aswell.
New-NetboxTenant -tenantName "TurboTenant" -LogToFile $false# If you want to add a value to the site that does not already exist, for example a customer id.
$customAddition = @{
custom_fields = @{
customer_id = "123123"
}
}
New-NetboxSite -siteName "My-road 12B" -tags ("tag123","coolsite") -objectData $customAddition -LogToFile $false
# if you have nothing extra to add just skip objectData and if there are no tags, you can skip that aswell.
New-NetboxSite -siteName "My-road 12B" -LogToFile $false# Update the Tenants name and add a tag called syncedtenant (must exist a tag with this name)
Update-NetboxTenant -tenantObject $NetboxTenant -tenantName "NewTenantName" -newTags ("syncedtenant") -LogToFile $True
# Update the customer with your own custom value
$customAddition = @{
custom_fields = @{
customer_id = "123123"
}
}
Update-NetboxTenant -tenantObject $NetboxTenant -newData $customAddition -LogToFile $True