convert C# function to Powershell function

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

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

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

      Hi!

      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
      {
      [System.Management.Automation.Cmdlet(System.Management.Automation.VerbsCommon.Get,"OpenXMLsdtBlock")]
      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")
      {
      this.WriteObject(sdt);
      }
      }
      }
      }
      }
      }

      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
      {
      [CMDLETBINDING(SupportsShouldProcess=$false,ConfirmImpact='None')]
      Param(
      [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$True)]
      [System.IO.FileInfo]$Document,

      [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$True)]
      [String]$ControlName
      )

      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!
      Bernhard

      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.