Use Microsoft Visual Basic (vbs) to write to PI with PI Web API Example

Blog Post created by rsun on Jan 20, 2017

I recently worked with a customer that wanted to use vbs to write to PI with PI Web API. I noticed there is not much example about this in the PI Square community, so I decided to share it. It might seem a bit old school. Please forgive me if you don't like it.


To set it up, you will need the PI Web API link with something like this "https://<YOUR PI WEB API MACHINE>/piwebapi/streams/<WEBID>/value"

The PI Web API Help file https://<YOUR PI WEB API MACHINE>/piwebapi/help/controllers/stream/actions/updatevalue included the detail of the controller that I am going to use.

To verify you got the right link, it must return the following format in your web browser


"Timestamp": "2017-01-20T16:53:36Z", 

"Value": 56.9878845,  

"UnitsAbbreviation": "",  

"Good": true,  

"Questionable": false, 

"Substituted": false



You should also verify you get a 202 status code with Fiddle. You can download here Thanks to Daphne Ng to show us how to use fiddler with a video

If you see a 401 error, don't worry, it means you have issue with authenticate. To avoid this authentication issue, I change the configuration database > OSIsoft > PI Web API > MachineName > System Configuration > AuthenticationMethods Attribute to Anonymous

I also have the following setup, please see the screenshot:



Warning! if you have PI Coresight, your index search might not work after switching to Anonymous. I workaround this issue by install another PI Web API on another machine, leave the existing PI Web API untouched.


Below is the vbs code, you will need to replace the PI Web API link with the link you have, specify the timestamp and value, remember to save the file with .vbs format.


On Error Resume Next


Dim o

Set o = CreateObject("MSXML2.ServerXMLHTTP")

o.setOption 2, SXH_SERVER_CERT_IGNORE_ALL_SERVER_ERRORS "POST", "https://<YOUR PI WEB API MACHINE>/piwebapi/streams/<WEBID>/value", False

o.SetRequestHeader "Content-Type", "application/json"

o.send "{""Timestamp"": ""2017-01-20T00:06:00"", ""Value"": ""100.0""}"



If Err.Number <> 0 Then

msgbox "Send didn't work " & Err.Number



Wscript.echo o.getResponseHeader("Content-Length")

Wscript.echo o.Status & vbTab & o.StatusText

end if



After you saved the code into vbs format, you can double click the file and data should write to PI.


I will post another version of the vbs code with Basic Authorization later. However, I have no idea how to set it to work with Kerberos. If you see this post, it will be great if you could let me know how to fill the http request in fiddler to get Kerberos to work.

This is my first blog post in PI Square. I hope you will find it useful.