3 Replies Latest reply on Dec 16, 2013 4:46 PM by mhamel

    How to use PIConfig with .Net (C#)

    pthivierge

      Did you ever wonder how to control PIConfig from a .Net application ?  

       

      There is a way to do this by using the Process Class.  I wrote a really basic exemple here that executes the pisysdump.dif.  Like I said, it is so simple that it has to be run in the PI dat folder and all path are hard coded.  (so for me it is really bad code but I wanted to share it anyway).  Let me know if this helps and also ideas you have to use it... we could shurely improve it.

       

       To use it simply use the command line and call "program.exe" in the pi\dat folder.

       

       

       
      using System;
      using System.Diagnostics;
      using System.IO;
      
      namespace PIConfigSimpleConsoleApplication
      {
          class Program
          {
              static void Main(string[] args)
              {
                 // configures the file to pass to PIConfig
                  var fileInfo = new FileInfo("pisysdump.dif");
                  Process process = new Process();
                  
                  // Configure the process using the StartInfo properties.
                  process.StartInfo.FileName = @"piconfig.exe";
                  process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                  process.StartInfo.RedirectStandardInput = true;
                  process.StartInfo.RedirectStandardOutput = true;
                  process.StartInfo.UseShellExecute = false;
                  process.Start();           
      
                  using (var streamReader = new StreamReader(fileInfo.FullName))
                  {
                      process.StandardInput.Write(streamReader.ReadToEnd());
                      process.StandardInput.Flush();
                  }
                  process.StandardInput.Close();
                  Console.Write(process.StandardOutput.ReadToEnd());
      
                  process.WaitForExit();// Waits here for the PIConfig process to exit.
      
              }
          }
      }
      

       

       

       Sources: 

       

      http://stackoverflow.com/questions/181719/how-to-start-a-process-from-c

       

      http://stackoverflow.com/questions/2956274/how-to-pass-filename-to-standardinput-process-in-c

        • Re: How to use PIConfig with .Net (C#)
          mhamel

          @Patrice: This is a great explanation but you could do it faster by doing the same directly with PowerShell and you won't have the need to compile.

           

          Look for my blog in the coming weeks, I will share some code snippets on how to invoke pre-written piconfig scripts with the command-line utility or build ones dynamically. In my case, this is useful if I need to interact with the result of the script to write a new one.

           

          I am giving you a sneak peek of what it looks like.

           

           

           
          # Execute a PI Config script to obtain the Server ID
          # Use the -pif switch to avoid deleting the piconfig script file.
          $outputFileContent = Invoke-PIConfigHelper_PIConfigScript -f "GetPIServerID.dif" -pif -dbgl $DBGLevel          
          
          foreach($line in $outputFileContent)
          {
               $tokens = $line.Split(",")     
               # Stop at the first line
               break
          }                    
          $PIServerIDBefore = $tokens[1]
          
          # Verbose if debug level >= 2
          $msg = "PI Server ID found is {0}" -f $PIServerIDBefore
          Write-PIConfigHelper_LogMessage $msg "Info" $fn -dbgl 2                    
          
          # Create a new guid.
          $newServerId = ([guid]::NewGuid()).ToString()
          
          # The following script will be sent to piconfig execute against the remote source server
          [string]$PIConfigScript = "@tabl pisys,piserver" + "`r`n" `
                                        + "@mode ed"  + "`r`n" `
                                        + "@istr name,serverid"  + "`r`n" `
                                        + $computerName + "," + $newServerId + "`r`n" `
                                        + "@ends" + "`r`n" `
                                        + "@bye"
          
          # Verbose.
          $msg = "Try to change PI Server ID from {0} to {1}" -f $PIServerIDBefore,$newServerId
          Write-PIConfigHelper_LogMessage $msg "info" $fn -dbgl $DBGLevel -rdbgl 2
               
          # Execute a PI Config script to obtain the Server ID
          $outputFileContent = Invoke-PIConfigHelper_PIConfigScript -pcs $PIConfigScript -dbgl $DBGLevel          
                    
          foreach($line in $outputFileContent)
          {
               $tokens = $line.Split(",")     
               # Stop at the first line
               break
          }                    
          $PIServerIDAfter = $tokens[1]