-
I am really struggling with how to craft a PowerShell script that can create a power cable through the REST API. Given the nested values that seem to be needed, I am trying the following: Invoke-RestMethod -Uri $api_base_url/dcim/cables/ -Method Post -Headers $headers -Body '{"type": "power", "status": "connected", "a_terminations": {"object_type": "dcim.powerport", "object": {"device": {"name": "F05 PDU B"}, "name": "Power Port 1"}}, "b_terminations": {"object_type": "dcim.powerport", "object": {"name": "230-F05-B"}}}' However, I get the following error:
I figure either I am overcomplicating how I can use PowerShell to create cables through the REST API, or I am absolutely missing something. Can anyone tell me what I am doing wrong / what a working way to create cables via PowerShell and the REST API is? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I was both under and overcomplicating it. So once I figured out my method above, I found it was bad because you don't supply the names of devices / power feeds / etc, you need to supply their object IDs. I don't want to have to remember what a named object's ID is all the time, so I came up with this. $getPowerFeeds = (Invoke-RestMethod -Uri $api_base_url/dcim/power-feeds/ -Headers $headers).results
$getPowerPorts = (Invoke-RestMethod -Uri $api_base_url/dcim/power-ports/ -Headers $headers).results
$PDU_Name = "A01 PDU A"
$PowerFeed_Name = "230-A01-A"
$PowerPort_ID = ($getPowerPorts | Where-Object {$_.device.name -eq $PDU_Name} | Select-Object ID).ID
$PDU_ID = ($getPowerPorts | Where-Object {$_.device.name -eq $PDU_Name} | Select-Object Device).Device.ID
$PowerFeed_ID = ($getPowerFeeds | Where-Object {$_.name -eq $PowerFeed_Name} | Select-Object ID).ID
# Create Cable
$hash = [ordered]@{
type = "power";
status = "connected";
a_terminations = @(
@{
object_type = "dcim.powerport";
object_id = $PowerPort_ID;
object = [ordered]@{
id = $PowerPort_ID;
device = [ordered]@{
id = $PDU_ID;
}
}
}
)
b_terminations = @(
@{
object_type = "dcim.powerfeed";
object_id = $PowerFeed_ID;
object = [ordered]@{
name = $PowerFeed_ID;
}
}
)
}
$json = $hash | ConvertTo-Json -Depth 99
$setCable = Invoke-RestMethod -Uri $api_base_url/dcim/cables/ -Method Post -Headers $headers -Body $json Since we use unique names for our power feeds and devices (in this case, PDUs) the only thing that needs to be set is the variables for $PDU_Name and $PowerFeed_Name. This also means later I can import a CSV with a bunch of PDU and Power Feed names and bulk create all of the power connections for them. |
Beta Was this translation helpful? Give feedback.
I was both under and overcomplicating it.
So once I figured out my method above, I found it was bad because you don't supply the names of devices / power feeds / etc, you need to supply their object IDs. I don't want to have to remember what a named object's ID is all the time, so I came up with this.