convert C# function to Powershell function

Welcome Forums General PowerShell Q&A convert C# function to Powershell function

Viewing 0 reply threads
  • Author
    • #5449
      Topics: 1562
      Replies: 0
      Points: 1
      Rank: Member

      by bertl81 at 2013-01-17 11:55:20


      First of all, i would like to say hi! 🙂
      Thats my First Post and i’ll hope that someone can help me….

      I’ll try to convert a C# function to Powershell
      Thats the Code from C#
      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Linq;
      using DocumentFormat.OpenXml.Packaging;
      using DocumentFormat.OpenXml.Wordprocessing;
      using DocumentFormat.OpenXml;

      namespace OpenXMLCmdlet
      public class Get_OpenXMLsdtBlock: System.Management.Automation.PSCmdlet
      [System.Management.Automation.Parameter(Position = 0, Mandatory = true)]
      public System.IO.FileInfo Document;

      protected override void ProcessRecord()
      using (WordprocessingDocument doc = WordprocessingDocument.Open(Document.FullName, true))
      MainDocumentPart mainDocumentPart = doc.MainDocumentPart;
      List<SdtBlock> sdtList = mainDocumentPart.Document.Descendants<SdtBlock>().ToList();

      foreach (SdtBlock sdt in sdtList)
      if (sdt.SdtProperties.GetFirstChild<Tag>().Val.Value == "ServerName")

      It returns the SdtBlock from an OpenXML Word File with the Tag ServerName…

      My Problem is now that i don’t now how is the correct Syntax for
      List<SdtBlock> sdtList = mainDocumentPart.Document.Descendants<SdtBlock>().ToList();
      in Powershell?

      With the following Powershell function i’ll get the Document and also the MainDocumentPart
      But i cant get the last Line to work… -.-
      function Get-OpenXMLsdtBlock


      BEGIN {

      PROCESS {

      $doc = [DocumentFormat.OpenXml.Packaging.WordprocessingDocument]::Open($Document.FullName,$true)
      $mainPart = $doc.MainDocumentPart
      $contentParts = $mainPart.Document.Body.Descendants<SdtBlock>()


      END {

      Any Ideas???

      Many thanks for your help!

      by nohandle at 2013-01-17 12:57:46

      Compiling it within the script using the add-type cmdlet is not a solution? It is bit late here and I C# force is not strong with me anymore, so I am just pitching the idea. I’ll hopefully look into tomorrow in more detail.

Viewing 0 reply threads
  • The topic ‘convert C# function to Powershell function’ is closed to new replies.