Powershell script to add and remove devices via API

2,389 views
Skip to first unread message

Brandon Strohmeyer

unread,
May 18, 2017, 5:07:11 PM5/18/17
to NetBox
Hi All, 

Having limited experience working with APIs, I ended up having a little trouble in getting up and running with Netbox's. I wanted to share a working example of interfacing with the Netbox API to hopefully help somebody else out there. Beware that it's not the most advanced or polished thing, but I went from zero powershell or API knowledge to this in the space of an afternoon. Some values are hard coded and should be changed to suit your environment. 

It may seem like an obvious thing to most, but I would suggest updating the API documentation to differentiate which fields require a PK and which can accept a unicode string. 

Big thanks for opening up a writable API!

# Netbox Windows Startup v1.0
#
# This script is designed to add new Devices and IP Addresses on startup.
# It is expected to be paired with a shutdown script to remove Netbox 
# inventory records on shutdown. The intended usage is in a dynamic
# environment where VMs are created and destroyed automatically. The
# script will also add VMs to a manually defined parent. 

# Configure physical site ID
#
#  Site   ID  
# ------ ---- 
#  ABC     1  
#  DEF     2    
#
$site = "1"

# Configure parent device by hostname
$parentName = "exampleparent"

# Configure API base url using format "http://example.com/api"

### End Configuration ###

# Set IPv4 variables
$cidr = (Get-NetIPAddress -InterfaceAlias Ethernet -AddressFamily IPv4 -IncludeAllCompartments).IPAddress`
+ "/" + `
(Get-NetIPAddress -InterfaceAlias Ethernet -AddressFamily IPv4 -IncludeAllCompartments).PrefixLength

# Set API Headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", 'Token <token>')
$headers.Add("Content-Type", 'application/json')
$headers.Add("Accept", 'application/json')

# Create IP Address record
$ipaddr = @{
    address=$cidr
    description='auto-managed'
}
$ipaddrJson = $ipaddr | ConvertTo-Json
$setIP = Invoke-RestMethod -Uri $api_base_url/ipam/ip-addresses/ -Method Post -Headers $headers -Body $ipaddrJson

# Create Device record
$ip4PK = $setIP.id
$device = @{
    name=$env:computername
    device_role=12
    device_type=32
    site=$site
    primary_ip4=$ip4PK
    tenant=12
    comments='auto-managed'
}
$deviceJson = $device | ConvertTo-Json
$setDevice = Invoke-RestMethod -Uri $api_base_url/dcim/devices/ -Method Post -Headers $headers -Body $deviceJson

# Update interface record
$devicePK = $setDevice.id
$interfacePK = (Invoke-RestMethod -Uri $api_base_url/dcim/interfaces/?device_id=$devicePK -Headers $headers).results.id
$interface = @{
    address=$cidr
    interface=$interfacePK
}
$interfaceJson = $interface | ConvertTo-Json
$setInterface = Invoke-RestMethod -Uri $api_base_url/ipam/ip-addresses/$ip4PK/ -Method Patch -Headers $headers -Body $interfaceJson

# Create Device Bay in pool
$parentNamePK = (Invoke-RestMethod -Uri $api_base_url/dcim/devices/?name=$parentName -Headers $headers).results.id
$bay = @{
    device=$parentNamePK
    name=$devicePK
    installed_device=$devicePK
}
$bayJson = $bay | ConvertTo-Json
$setBay = Invoke-RestMethod -Uri $api_base_url/dcim/device-bays/ -Method Post -Headers $headers -Body $bayJson

# Write summary to console
Write-Host "
Netbox records created!
 Device Name: $env:computername 
 Device ID: $devicePK
 IPv4 Addres: $cidr
 IPv4 ID: $ip4PK
"

 
# Netbox Windows Shutdown v1.0
# This script is designed to remove netbox inventory records
# on host shutdown. It is intended to be paired with a Startup
# script to add records on host start. 

# Configure API base url

### End Configuration ###

# Set API Headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", 'Token <token>')
$headers.Add("Content-Type", 'application/json')
$headers.Add("Accept", 'application/json')

# Fetch information about host
$getDeviceInfo = (Invoke-RestMethod -Uri $api_base_url/dcim/devices/?name=$env:computername -Headers $headers).results

# Delete IPv4 record from Netbox
$primary_ipPK = $getDeviceInfo.primary_ip.id 
Invoke-RestMethod -Uri $api_base_url/ipam/ip-addresses/$primary_ipPK/ -Method Delete -Headers $headers

# Delete device record from Netbox
$devicePK = $getDeviceInfo.id
Invoke-RestMethod -Uri $api_base_url/dcim/devices/$devicePK/ -Method Delete -Headers $headers

# Delete device bay record from Netbox
Invoke-RestMethod -Uri $api_base_url/dcim/device-bays/$bayPK/ -Method Delete -Headers $headers

# Write summary to console
Write-Host "Netbox records deleted!
 Device ID: $devicePK
 IPv4 ID: $primary_ipPK
"


Reply all
Reply to author
Forward
0 new messages