none
I want to pass parameters topower shell from C# RRS feed

  • Question

  • I want to pass parameters topower shell from C#,

    here is my coding but it doesn't work.

     public
     void
     RunScript(String scriptText)
    {
    // string script = System.IO.File.ReadAllText(scriptText);
    // create Powershell runspace
    Runspace runspace = RunspaceFactory.CreateRunspace();
    /// / open it
    runspace.Open();
    // create a pipeline and feed it the script text
    Pipeline pipeline = runspace.CreatePipeline();
    Command command = new Command(scriptText);
    CommandParameter cp = new CommandParameter("aa" ,"C:\\fso\\*" );
    string asd = command.CommandText;
    command.Parameters.Add(cp);
    pipeline.Commands.Add(command);
    Console.WriteLine("Converting..........." );
    pipeline.Invoke();
    Console.WriteLine("Converted!" );
    runspace.Close();
    }
    here is my powershell code


    Param(
    $Param1
    )
    [ref]$SaveFormat = "microsoft.office.interop.word.WdSaveFormat" -as [type]
    $word = New-Object -ComObject word.application
    $word.visible = $false

    $folderpath =$Param1
    $fileType = "*doc"
    Get-ChildItem -path $folderpath -include $fileType |
    foreach-object `
    {

    $path = ($_.fullname).substring(0,($_.FullName).lastindexOf("."))

    "Converting $path to $fileType ..."
    "$path"


    $doc = $word.documents.open($_.fullname)
    $doc.saveas([ref] $path, [ref]$SaveFormat::wdFormatDocumentDefault)
    $doc.close()
    }
    $word.Quit()
    $word = $null
    [gc]::collect()
    [gc]::WaitForPendingFinalizers()
    • Moved by edhickey Wednesday, July 21, 2010 4:30 PM (From:.NET 3.0/3.5 Windows Workflow Foundation)
    • Moved by Chao Kuo Monday, July 26, 2010 6:35 AM (From:Visual C# General)
    Wednesday, July 21, 2010 12:03 PM

Answers