2

I started learning PowerShell and C# recently. I would like to use it in a web app with visual studio.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;


namespace Extraction_test.Controllers
{
    public class HomeController : Controller
    {
               public string Index() //PowershellExtract()
        {
            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();
            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript(@"$filepath=""C:\Users\myusernamefolder\Downloads\ReadExcel\Test.xlsx"";");
            pipeline.Commands.Add("Param([string]$filepath;)");
            string scriptpath = "[System.Threading.Thread]::CurrentThread.CurrentCulture = [System.Globalization.CultureInfo] \"en-US\";" +
                    "$Excel = New-Object -ComObject Excel.application;" +
                    "$Excel.visible=$True;" +
                    "$Workbook=$Excel.Workbooks.open($filepath);" +
                    "$Worksheet=$WorkbookSheets.item(\"Feuil1\");" +
                    "$Worksheet.activate();" +
                    "$col = 4;" +
                    "$lines = @(5,6,7,9);" +
                    "foreach ($line in $lines) { $global:output = $Worksheet.Cells.Item($line,$col).Value(); }" +
                    "$global:test='test string value'; " +
                    "$workbook.Close();" +
                    "$Excel.Quit();";
            pipeline.Commands.AddScript(scriptpath);
            Collection<PSObject> results = pipeline.Invoke();
            var result = runspace.SessionStateProxy.PSVariable.GetValue("test");
            //var result = runspace.SessionStateProxy.PSVariable.GetValue("test");
            return result.ToString();

        }
    }
}

However, I couldn't find anywhere a way to add a parameter script in a pipeline

pipeline.Commands.Add("Param([string]$filepath;)");

Thanks in advance!

5
  • Also, my main goal is to extract the $output in the return. But at first, I am testing with $test Commented Mar 2, 2018 at 7:40
  • Do not create Pipeline yourself, use PowerShell. Commented Mar 2, 2018 at 7:58
  • However, with PowerShell, I didn't find any solutions to retrieve my result without runspace and pipeline. I found that I had to use var result = runspace.SessionStateProxy.PSVariable.GetValue("test"); Unless there is a way to retrieve the result with Powershell and runspace Commented Mar 2, 2018 at 8:20
  • PowerShell.Invoke return output of invoked command. Commented Mar 2, 2018 at 10:10
  • I tried it but I have this result System.Collections.ObjectModel.Collection1[System.Management.Automation.PSObject] Is there a way to specify which output I would like to return? For example, $test Commented Mar 2, 2018 at 10:30

1 Answer 1

2

if you create the Command object separately, you can add a CommandParameter to it:

Pipeline pipeline = runspace.CreatePipeline();    
string scriptFile = Path.Combine(ScriptDir, scriptpath);
Command scriptCommand = new Command(scriptFile);

CommandParameter commandParm = new CommandParameter("name", "value");
scriptCommand.Parameters.Add(commandParm);
pipeline.Commands.Add(scriptCommand);
pipeline.Invoke()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer. I implemented it in my code but how do you define ScriptDir? Visual Studio doesn't seem to recognize it.
That would be a string that holds the path to the script: string ScriptDir="C:\\scripts". scriptpath would then be the name, like string scriptpath= "pizza.ps1". Sorry for the confusing variable names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.