JKennedy
JKennedy

Reputation: 18799

How do I set $ConfirmPreference = "None" for a PowerShell instance?

I am trying to run PowerShell scripts using C# using this link as a reference.

So far I have got:

  try
  {
       using (PowerShell PowerShellInstance = PowerShell.Create())
       {
             PowerShellInstance.AddCommand(scriptPath);                      
             var PSOutput = PowerShellInstance.Invoke();
             if (PowerShellInstance.Streams.Error.Count > 0)
             {
                 foreach (var line in PowerShellInstance.Streams.Error)
                 {
                      Console.WriteLine(line);
                 }
                 return false;
             }
             else
             {
                 return true;
             }
        }
   }
   catch (Exception ex)
   {
       return false;
   }

Which keeps throwing an exception:

"AuthorizationManager check failed."

Inner Exception: A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: Run only scripts that you trust. While scripts from the internet can be useful, this script can potentially harm your computer. If you trust this script, use the Unblock-File cmdlet to allow the script to run without this warning message. Do you want to run C:\PowerShellScripts\MyScript.ps1?

So looking at the Exception I can see it's asking me to confirm the script but there is no window for the user to interact, hence the exception.

So I started looking at how to stop the confirmation text and found Powershell New-Item: How to Accept Confirmation Automatically

But even adding:

PowerShellInstance.AddScript("$ConfirmPreference = \"None\"");
PowerShellInstance.Invoke();

Before executing my script didn't work. So is there a way of setting $ConfirmPreference = "None" for my PowerShell instance using C#?

Upvotes: 0

Views: 2956

Answers (2)

George Chakhidze
George Chakhidze

Reputation: 1399

While the accepted answer solved this specific problem, the correct way of setting $ConfirmImpact preference variable is via session state:

var sessionState = InitialSessionState.CreateDefault();
sessionState.Variables.Add(new SessionStateVariableEntry("ConfirmPreference", ConfirmImpact.None, ""));

using (PowerShell shell = PowerShell.Create(sessionState))
{
    // execute commands, etc
}

(This is for visitors who came here from Google search results)

Upvotes: 2

Johan de Haan
Johan de Haan

Reputation: 1018

I think it has something to do with the Execution Policy. You can query the execution policy with the Cmdlet Get-ExecutionPolicy. You can:

  1. change the Execution Policy to (for example): "Unrestricted" by using Set-ExecutionPolicy Unrestricted or
  2. run your script by running powershell.exe -ExecutionPolicy Bypass C:\PowerShellScripts\MyScript.ps1 or
  3. unblock the script by using the Cmdlet Unblock-File C:\PowerShellScripts\MyScript.ps1

Upvotes: 1

Related Questions