Automating PI Archive Maintenance with PowerShell: Pitfalls and Solutions
Migrating PI archive reprocessing scripts from batch files to PowerShell can cause unexpected issues due to argument handling differences. This post explores common problems, workarounds, and introduces OSIsoft's official PowerShell Tools for more reliable automation.
Roshan Soni
Automating PI Archive Maintenance with PowerShell: Common Pitfalls and Solutions
Maintaining PI archives efficiently is key to ensuring optimal performance and data integrity within your OSIsoft PI System. Traditionally, many administrators have relied on batch scripts to automate archive-related tasks such as reprocessing using the piarchss utility. However, with the increasing adoption of PowerShell for automation, users often encounter unexpected hurdles—specifically when executing native PI utilities like piarchss.exe.
In this post, we’ll explore common challenges faced when migrating archive maintenance scripts from classic batch files to PowerShell, why they occur, and the most effective workarounds available. We’ll also touch on the modern solution: PowerShell Tools for the PI System.
The Problem: PowerShell Argument Handling
A typical batch script for archive reprocessing might look like this:
c:\pi\adm\piartool -au %1
move %1 %1.temp
move %1.ann %1.temp.ann
c:\pi\bin\piarchss -if %1.temp -of %1
c:\pi\adm\piartool -ar %1
When attempting to replicate this flow in PowerShell, many users find that the core piarchss reprocessing command does not work as expected. For instance, a direct call like:
c:\pi\bin\piarchss -if $tempname -of $orgname
…may execute but does not process the arguments correctly. No output or result is produced, even though the same command runs fine from a CMD prompt. This leads to confusion and script failures.
Why Does This Happen?
PowerShell handles argument parsing differently than CMD. Native executables—especially those with complex service/executable roles like piarchss—may not interpret arguments as intended if passed directly. As a result, services may fail to launch, logging errors such as Event Viewer Error 1063 (Unable to start Service Control Dispatcher) or similar in the PI Message Log.
Workarounds and Solutions
Several strategies have emerged to overcome this limitation:
1. Use Start-Process with ArgumentList
The most reliable and idiomatic PowerShell approach is to invoke external programs using Start-Process with the -Wait flag. This ensures proper argument passing and synchronous execution:
$argList = "-if `"$tempname`" -of `"$orgname`""
Start-Process -FilePath 'C:\pi\bin\piarchss.exe' -ArgumentList $argList -Wait
This approach escapes arguments correctly and ensures that PowerShell waits for the process to finish.
2. Use cmd /c to Proxy the Command
By forcing the command to run in a CMD environment, you gain back traditional batch-style argument handling:
$allArgs = "-if $tempname -of $orgname"
cmd /c "c:\pi\bin\piarchss $allArgs"
This is particularly helpful for utilities not "PowerShell-aware."
3. Directly Invoke with [Diagnostics.Process]::Start()
Some advanced users opt to start the process manually, which can sometimes succeed where PowerShell command-line invocation fails:
$args = "-if `"$tempname`" -of `"$orgname`""
[Diagnostics.Process]::Start('C:\pi\bin\piarchss.exe', $args)
4. Monitor System Logs for Errors
Given the hybrid service/executable nature of some PI utilities, always check the Event Viewer and PI Message Log for relevant errors if execution fails.
5. Use OSIsoft PowerShell Tools
As of recently, OSIsoft has published official PowerShell Tools for the PI System—available from their Download Center. These cmdlets are designed to interact with the PI System natively from PowerShell, reducing reliance on wrapping CLI utilities and prone-to-break workarounds. If your version of PI Data Archive supports it, this should be your go-to method for future automation efforts.
Further Reading: Jay Lakumb's introduction to PowerShell Tools for PI System
Conclusion
PowerShell is a fantastic automation language but does have quirks—especially when dealing with legacy native executables that expect arguments in a specific format. When automating PI Archive reprocessing, Start-Process, cmd /c proxies, and awareness of system logging will help you circumvent most issues. But for a sustainable, future-proof solution, consider leveraging OSIsoft’s PowerShell Tools.
Have you transitioned your PI maintenance scripts to PowerShell? What challenges or clever workarounds have you discovered? Share your experiences below!
Tags
About Roshan Soni
Expert in PI System implementation, industrial automation, and data management. Passionate about helping organizations maximize the value of their process data through innovative solutions and best practices.
No comments yet
Be the first to share your thoughts on this article.
Related Articles
Enhancing PI ProcessBook Trends with Banding and Zones: User Needs, Workarounds, and the Road Ahead
A look at the user demand for trend banding/zoning in OSIsoft PI ProcessBook, current VBA workarounds, UI challenges, and how future PI Vision releases aim to address these visualization needs.
Roshan Soni
Migrating PIAdvCalcFilVal Uptime Calculations from PI DataLink to PI OLEDB
Learn how to translate PI DataLink's PIAdvCalcFilVal advanced calculations—like counting uptime based on conditions—into efficient PI OLEDB SQL queries. Explore three practical approaches using PIAVG, PIINTERP, and PICOunt tables, and get tips for validation and accuracy.
Roshan Soni
Understanding PI Web API WebID Encoding: Can You Generate WebIDs Client-Side?
Curious about how PI Web API generates WebIDs and whether you can encode them client-side using GUIDs or paths? This article explores the encoding mechanisms, current documentation, and best practices for handling WebIDs in your applications.
Roshan Soni