convert C# function to Powershell function

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 11 months ago.

  • Author
    Posts
  • #5449

    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 sdtList = mainDocumentPart.Document.Descendants().ToList();

    foreach (SdtBlock sdt in sdtList)
    {
    if (sdt.SdtProperties.GetFirstChild().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 sdtList = mainDocumentPart.Document.Descendants().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()

    }

    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.

You must be logged in to reply to this topic.