1

Really struggling with this. I have tried various different way, but nothing seems to work.

-using addScript: I get an error telling me that I can't call parameters this way an should use a UI like ISE ?!

-using FilePath parameter, I can't find the right way to pass the arguments (trouble binding)

This is the latest version I tried, and is lifting no errors, but the script is not executed, nothing happens...

Help would be much appreciated.

runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
pipeline = runspace.CreatePipeline();

string script =
@"{param($merchantName, $appType, $gruntDirectory, $merchantInstanceDirectory, $editorConnectionString) "+
_config.MerchantInstance.Directory + @"\Generate_And_Compile_LESS.ps1"
+ " –merchantName $merchantName"
+ " –appType $appType"
+ " –gruntDirectory $gruntDirectory"
+ " -merchantInstanceDirectory $merchantInstanceDirectory"
+ " -editorConnectionString $editorConnectionString }";

Command compileCommand = new Command("Invoke-Command");
compileCommand.Parameters.Add("Scriptblock", ScriptBlock.Create(script));

var args = new List<string>();
args.Add(merchantName);
args.Add(appType.GetHashCode().ToString());
args.Add("'" + _config.Grunt.Directory + "'");
args.Add("'" + _config.MerchantInstance.Directory + "'");
args.Add("'" + _connectionStrings.AppConnectionString + "'");

compileCommand.Parameters.Add("ArgumentList", String.Join(",", args));

pipeline.Commands.Add(compileCommand);
Collection<PSObject> results = pipeline.Invoke();

1 Answer 1

2

You can use this code, which I personally just tested.

static void Main(string[] args)
{
    PowerShell ps = PowerShell.Create();
    ps.AddScript(@"c:\test\test.ps1").AddParameter("param1", "paramvalue1");
    ps.Invoke();
}

Here is my test script, located in c:\test\test.ps1.

[CmdletBinding()]
param (
    [string] $param1
)
Set-Content -Path $PSScriptRoot\test.txt -Value $param1;

FYI, make sure that you launch 32-bit (x86) PowerShell, and set the execution policy to Unrestricted. Visual Studio is a 32-bit process, and invokes the 32-bit PowerShell engine by default.

Sign up to request clarification or add additional context in comments.

2 Comments

Forgot to mention, I want to use pipeline object so that I can catch the specific errors that are thrown inside the script.
ps = ps.AddScript(...).AddParameter(....) worked better for me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.