0

I keep running in circles with trying to use PowerShell within a C# Application. I know everything I need to do is capable within PowerShell, I just want to wrap a GUI around it to make things easier.

I have PowerShell.SDK and Management.Automation installed with Nuget.

I initially created a PowerShell and tried to do the commands.

PowerShell powerShell = PowerShell.Create();

But that's when I found that this creates a basic shell and didn't have my modules or policies. I did the follwing and it errors out saying it tried to do a user interaction. How do I get it to allow user interaction? I would be done if this worked.

InitialSessionState initialSessionState = InitialSessionState.CreateDefault();  
PropertyInfo[] propertyInfo = initialSessionState.GetType().GetProperties();  
propertyInfo[2].SetValue(initialSessionState, ExecutionPolicy.Unrestricted, null);  
initialSessionState.ImportPSModule(new string[] { "Microsoft.Online.SharePoint.PowerShell" });  
Runspace runspace = RunspaceFactory.CreateRunspace(initialSessionState);  
runspace.Open();  
PowerShell powerShell = PowerShell.Create(runspace);  
powerShell.AddCommand("Connect-SPOService").AddParameter("-Url", "<https://site admin.sharepoint.com>").AddParameter("-Credential", "name@site.com").Invoke();

I then tried to manually create the Credential needed by the connect. But it now throws exceptions in the Output Windows and does nothing. I have no way of knowing why since they happened within Invoke. How do I see the actual errors from PowerShell?

System.Security.SecureString passwordSecure = new System.Security.SecureString();  
foreach (char a in "FakePassword") { passwordSecure.AppendChar(a); }  
System.Management.Automation.PSCredential passwordCredential = new System.Management.Automation.PSCredential("name@site.com", passwordSecure);  
runspace.SessionStateProxy.SetVariable("PasswordCredential", passwordCredential);  
powerShell.AddCommand("Connect-SPOService").AddParameter("-Url", "https://site-admin.sharepoint.com").AddParameter("-Credential", "name@site.com").Invoke();

How do I create a PowerShell in C# that inherits my personal PowerShell Environment? I think dependencies are hitting me too.

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Add an exception handler (try/catch) around the PS code and then output the trace stack. You do NOT want a new runspace created. Read following to stop a new runspace : https://learn.microsoft.com/en-us/dotnet/api/System.Management.Automation.PowerShell?view=powershellsdk-7.2.0 Make sure you are running PS as Admin by right click PS shortcut and select Run As Admin. You can also check the Event Viewer on Remote machine to see any errors at time you run your PS. – jdweng Feb 27 '23 at 05:22
  • The following may (or may not) be helpful: https://stackoverflow.com/a/73999216/10024425 – Tu deschizi eu inchid Feb 27 '23 at 16:22

1 Answers1

1

How do I get it to allow user interaction?

By implementing a custom host application.

How do I see the actual errors from PowerShell?

Inspect the PowerShell instance after Invoke() returns:

if (powerShell.HadErrors) {
    foreach (var errorRecord in powerShell.Streams.Error) {
        // inspect each `errorRecord` here
    }
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Thank you. This has helped a lot as I was able to resolve some mistakes, but also confirm that this appears to be an environment issue. Is there any way to have the C# PowerShell use the same environment as if I just opened an actual PowerShell? – John Klein Feb 27 '23 at 17:38