cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2539
Views
9
Helpful
3
Replies

Powershell example using Invoke-RestMethod for PI NBI Write

lusbyr
Level 1
Level 1

Community,

I am looking for help in developing a method to update a User Defined Field (UDF) in Prime Infrastructure via the API using the PUT devices/bulkImport method.

I have tried putting together something using tidbits of information garnered from PI API documentation, Google, CCO, etc and I am not having a lot of luck.  Lots of examples on how to read data via PI.  No good examples of how to write data to PI via Powershell.

This is a simple script to change a UDF on a single device in PI, identified by it's IP address.

Here are the salient portions of my script:

Ignore-SSLCertificates

$username = "nbi_api"

$password = "Password123"

$myserver = "pi32-test-01.network.com"

$ipaddr = "10.75.62.64"

$basicAuth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("nbi_api:Password123")))

$my_xml = @"

<?xml version="1.0" ?>

  <devicesImport>

  <devices>

  <device>

  <ipAddress>$ipaddr</ipAddress>

   <udfs>

   <udf>

   <name>CSC</name>

   <value>Yes</value>

   </udf>

   </udfs>

  </device>

  </devices>

</devicesImport>

"@

uri = "https://$myserver/webacs/api/v3/op/devices/bulkImport"

$response = Invoke-RestMethod -uri $uri -headers @{Authorization=("Basic {0}" -f $basicAuth)} -DisableKeepAlive -body $my_xml -Method Put

Any help would be appreciated.  I keep getting a 415 error from the PI server.

Thanks.

3 Replies 3

lusbyr
Level 1
Level 1

Community,

Hunting the internet further I found the issue.  I needed to find a way to get more information from Powershell errors.

It told me the ContentType was incorrect.  It was expecting the -ContentType 'text/xml'

Once I set this, the Invoke-RestMethod worked with PUT and the PI server returned a job number as expected.

However, the PI server indicated the job failed.  I can only capture a portion of the error message.  It is griping about the snmp_version.  However, I am not trying to change the snmp_version, just a UDF.

FAILED_Add Bulk Import Job Failed, Incorrect Input file. Line 2: Invalid value : '' for snmp_version Possible Values are : 1 , 2c , 3.-INVENTORY

I have no idea why it is throwing this error message.  Can the community help in properly formatting the XML input?

Thanks.

The op/devices/bulkImport resource expects a full set of device credentials to be sent in the request payload, even in the case of updating a UDF.  The recommended workflow is to use op/devices/exportDevices to export the device(s) in question, use the response from that resource as a baseline to make your UDF changes, then PUT that to op/devices/bulkImport.  Let me know if you run into any further issues.

Spencer,

Thanks for the reply.  What I discovered was that the /op/devices/bulkImport requires a MINIMUM of three entries being passed to it to make an update to the UDF.  IP address, SNMP version and SNMP community.  The API documentation only indicates the IP address is mandatory.  I found this not to be the case.  All three are required to make a successful update to the UDF.

Posting this here so other community members can see how I resolved the issue.

Thanks.