RJKSolutions

Get-Content "Rhys' PI and PowerShell mash up" -Filter "Section='Connections'" | Set-Content "Part 1 Blog Post"

Blog Post created by RJKSolutions on Jul 19, 2013

Ever wondered how you use PowerShell to connect to the PI System?
Too busy to learn another data access method?
Not had any real exposure to PowerShell thus far?

 

Then read on as I introduce you to connecting to the PI Server and AF Server using the OSIsoft PowerShell Tools for the PI System.

 

This blog does not explain the specifics of PowerShell but merely shows you how to use PowerShell with the PI System. For details of PowerShell itself I suggest you ask our mutual colleagues; Google and Bing.

 

I am by no means a PowerShell guru, just a programming nomad who has currently settled in the land of PowerShell until I am ready for my next journey. I set myself a goal at the beginning of the year to understand PowerShell and happy to have achieved that so far. In fact, I’ve used it in projects already to save hours/days (and large amounts of $’s, £’s …) of work.

 

Feedback most welcome!
Suggestions for further PowerShell topics most welcome too!
Nomination for OSIsoft vCampus All-Star 2013 a must!   (I’m starting early this year…)

 

Adding the OSIsoft PowerShell snapin.

 

We can’t do anything without the Powershell Snapin that provides the CmdLets we need.

 

Snapins are added using the “Add-PSSnapin” CmdLet; opposite CmdLets tend to exist so in this instance there is a “Remove-PSSnapin” CmdLet too:

 

 

 
Add-PSSnapin "OSIsoft.PowerShell"
Remove-PSSnapin "OSIsoft.PowerShell"

 

 

Now of course that Snapin could already have been added which means we’ll get some ugly red text thrown at us, so we’ll handle that explicitly:

 

 

 
if ((Get-PSSnapin "OSIsoft.PowerShell" -ErrorAction SilentlyContinue) -eq $null) { Add-PSSnapin "OSIsoft.PowerShell" }

 

 

With the snapin loaded we have access to a wealth of PI System CmdLets that pretty much cover most aspects of the PI System from a management perspective. What’s more is the abstraction that the CmdLets provide mean scripts produced can easily be supported by any PowerShell scripters without needing to have the deep knowledge of the PI System SDKs.

 

Connecting to a PI Server.

 

Connecting to a PI Server is extremely simple and achieved with 2 CmdLets; Get-PIServer and Connect-PIServer (Disconnect-PIServer is available too but we don’t need that right now).

 

This is the code required using those 2 CmdLets to connect to a PI Server named “vCampusDemo”:

 

 

 
[OSIsoft.SMT.PIServer]$PI = Get-PIServer -Name "vCampusDemo" | Connect-PIServer

 

 

How beautiful does that look? Extremely.

 

Once you have your PIServer object the world is your oyster as to what you want to do with it, for example checking the version of the PI Server:

 

 

 
Write-Host $PI.Version

 

 

There are endless things you’ll want to do to that PI Server, but I’ll cover that in some further blogs. For now, let’s just concentrate on getting you connected.
If you’re working with a PI Collective and have a connection preference then that is already covered, just specify your preference when connecting:

 

 

 
[OSIsoft.SMT.PIServer]$PI = Get-PIServer -Name "vCampusDemo" | Connect-PIServer -ConnectionPreference RequirePrimary

 

 

One neat parameter of the Connect-PIServer CmdLet I found was “-AcceptServerIDChange”. It is pretty self-explanatory and works great – if you have a PI Server that recently changed Server Id and you’re connecting then it will automatically accept the new Server Id, which will be updated in the PI-SDK registry.

 

Connecting to an AF Server (PI System).

 

Can’t be as easy as connecting to a PI Server, can it? Yes, yes it can. The same CmdLet pattern is available for an AF Server; Get-AFServer, Connect-AFServer, Disconnect-AFServer.

 

 

 
[OSIsoft.AF.PISystem]$AF = Get-AFServer -Name "vCampusDemoAF" | Connect-AFServer

 

 

Just as beautiful as the PI Server connection I think you’ll agree. Same as with the PI Server, connection preference for an AF Collective is supported:

 

 

 
[OSIsoft.AF.PISystem]$AF = Get-AFServer -Name "vCampusDemoAF" | Connect-AFServer -ConnectionPreference RequirePrimary

 

 

Connection error handling

 

With both the PI Server and AF Server we have just connected with no regards for how we want to handle exceptions, for example a PI Server doesn’t exist, or an AF Server connection was rejected.

 

For both our connections to the PI Server and AF Server we can have the connection attempts silently executed and then make our own checks on the objects returned.

 

PI Server connection becomes:

 

 

 
[OSIsoft.SMT.PIServer]$PI = Get-PIServer -Name "vCampusDemo" -ErrorAction SilentlyContinue | Connect-PIServer -ErrorAction SilentlyContinue

if ($PI -eq $null) { Write-Host "Uh oh, no connection the PI Server." -ForegroundColor Red }

 

 

Then, you guessed it; we can do the same thing for the AF Server connection:

 

 

 
[OSIsoft.AF.PISystem]$AF = Get-AFServer -Name "vCampusDemoAF" -ErrorAction SilentlyContinue | Connect-AFServer -ErrorAction SilentlyContinue

if ($AF -eq $null) { Write-Host "Uh oh, no connection to the AF Server." -ForegroundColor Red }

 

 

There is currently a bug with the “Get-PIServer” CmdLet that means it will throw an error even if you specify “SilentlyContinue” as the ErrorAction. This is being addressed by OSIsoft.

 

 

 

What’s next?

 

With the easy bit of connecting out of the way the next set of blogs on PowerShell with the PI System will focus on some management aspects of both systems. They will include some PI Server audits (PI Mappings/Trusts/Firewall), archive management, PI Interface manipulation (PI Module Database + remote command file edits) … Beyond those blog posts we’ll switch our attention to the AF Server.

Outcomes