2

I have a test .Net core 3.1 c# console app which creates a new Runspace and runs a powershell script.

var Session = InitialSessionState.CreateDefault();
Session.ImportPSModule("MicrosoftTeams");
var pool = RunspaceFactory.CreateRunspacePool(Session);
poo.Open();

using (PowerShell ps = PowerShell.Create())
{
   ps.Runspacepool = pool;
   ps.AddScript(ScriptContents);
   var x = await ps.InvokeAsync();  
}

The contents of a test powershell script looks like this

$proxyserver = "http://myproxy:8080";

[system.net.webrequest]::DefaultWebProxy = New-Object system.net.webproxy($proxyserver)
[system.net.webrequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

$pass = ConvertTo-SecureString "xxxx" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList "user",$pass
$a = get-Credential -Credential $cred
Connect-MicrosoftTeams -Credential $a

If this powershell script is launched via standard powershell session, it works as intended, being redirected to the HTTP proxy and successfully connecting. However if it's launched from within the C# application, everything works fine, apart from the fact it's not being redirected to the proxy.

I'm not sure what I'm doing wrong here. Is there a better way to achieve the proxy override?

Rich79
  • 23
  • 5
  • 1) When launched from your app, does `[system.net.webrequest]::DefaultWebProxy` get set correctly, even if it's not being used by teams? Try just writing it to your output to check. 2) Is there any output stored in `x` otherwise? and 3) try adding your proxy settings to the powershell session initialization, or otherwise setting it *before* importing the teams module. The O365 modules tend to do a little too much work on the initial import – Cpt.Whale Aug 24 '21 at 15:17
  • Thanks @Cpt.Whale ! Yes getting `[system.net.webrequest]::DefaultWebProxy` it shows the correct Proxy being set. I don't use the output of x, but I use callbacks `ps.Streams.Information.DataAdded` etc. I know it's not using the proxy because Connect-MicrosoftTeams reports connectivity error and TCPView.exe filtered on the process is see it taking the system default. I repeat that by just running the powershell script and TCPView shows my explicitly set proxy. *try adding your proxy settings to the powershell session initialization* ? Do you mean during runspace initialization? – Rich79 Aug 24 '21 at 23:30
  • I have one more update. If I use `$request= [system.net.webrequest]::CreateHttp('http://google.com')` within the same script and launched from the C# app, it correctly takes the proxy override. So just `Connect-MicrosoftTeams` when launched from within `PowerShell.Create()` isn't obeying the proxy setting. Hmm. – Rich79 Aug 25 '21 at 01:10
  • Yep, that's pretty weird. I did mean runspace initialization, but it might be simpler to try adding `import-module MicrosoftTeams` to your script file (instead of via `Session.ImportPSModule()`), but place it after the proxy settings are configured. My only other suggestion is checking if the module has updates or prerelease versions you could try? – Cpt.Whale Aug 25 '21 at 13:24

0 Answers0