0

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);
    
John Saunders
  • 160,644
  • 26
  • 247
  • 397
techspeque
  • 26
  • 6

2 Answers2

0

-Confirm:$false -Force are meant to be function parameters, they don't exist on their own.

Do-Something -param1 'foo' -Confirm:$false -Force

But even if you modify every function call with these parameters, it probably won't solve all of your issues. See here for more options.

Community
  • 1
  • 1
latkin
  • 16,402
  • 1
  • 47
  • 62
-1

OK, there is an Object coming with PowerShell called CommonParameters (MSDN: System.Management.Automation.Internal.CommonParameters)

Parameters which are meant can be seen here: MSDN: Common Parameter Names

And as far as I know, is the variable $ConfirmPreference set to None you don't need to set Confirm to false for every Command you are calling. That's implicitly...

And you also must set this variable one time after creating Runspace or before calling your first Command, and not with each Command or Script.

And you can also set this variable like this:

runspace.SessionStateProxy.SetVariable("ConfirmPreference", "none");
algorhythm
  • 8,530
  • 3
  • 35
  • 47