Running PowerShell through C# pipeline

I am writing PS script that will be integrated with the C# based Windows Form Application. This is what I have tried/done already:
  1. I am able to run PS scripts through the runspace and pipeline, however i encounter the error while I am trying to run the script that uses functions, saying: " Cannot invoke this function because the current host does not implement it."
  2. I have researched the problem and I have found very few people who had encoutered similar problem, however one of the solutions underlined that I need to use (add) the following $ConfirmPreference = "None", ,-Confirm:$false, -Force
  3. However when I tried that the following error message appeared: " Missing expression after unary operator '-'. " You can see that I have tried to concatenate the strings by assigning them to the variables, but that did not help. I do not have any idea how to solve it, has anyone got any ideas? Any help will be much appreciated.
       // create Powershell runspace
       Runspace runspace = RunspaceFactory.CreateRunspace();
       // open it
       runspace.Open();
    
       // create a pipeline and feed it the script text
       Pipeline pipeline = runspace.CreatePipeline();
    
       //allow the functions to be exec
       string sa = "$ConfirmPreference = \"None\" ";
       string se = " -Confirm:$false -Force";
       string s = sa +scriptText + se;
       pipeline.Commands.AddScript(s);
I get the same error when I try to execute your command (but with double quotes around & to 1) on PowerShell 1.0. 1.0 doesn't support the WindowStyle parameter. Is it possible you are developing this on 2.0 but then testing it out on a PowerShell 1.0 system? Also, if you are running this from PowerShell 2.0 this will make the current window hidden. Is that your intent? You could fire the script off in another, hidden PowerShell window using Start-Process -WindowStyle Hidden.

Comments