powershell 连接到 outlook.office365 的代理设置

Proxy setting for powershell connecting to outlook.office365

我正在编写连接到 outlook office365(exchange online)的 PowerShell 脚本,如下所示:

$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $credential -Authentication Basic -AllowRedirection

现在的问题是我想通过具有身份验证的代理服务器连接,所以做了以下

$proxy = New-Object System.Net.WebProxy "http://myproxy:80"
$proxy.Credentials = $cred
[System.Net.WebRequest]::DefaultWebProxy = $proxy
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential 
$credential -Authentication Basic -AllowRedirection

但是它没有通过我设置的代理连接。所以我做了以下事情:

$proxy = New-Object System.Net.WebProxy "http://myproxy:80"
$proxy.Credentials = $cred
[System.Net.WebRequest]::DefaultWebProxy = $proxy
$sessionOption = New-PSSessionOption -ProxyAccessType IEConfig
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential 
$credential -Authentication Basic -AllowRedirection -SessionOption $sessionOption

但是还是没有通过代理。

我也试过 netsh winhttp set proxy "myproxy:80" 并且它通过了代理服务器,但它似乎没有身份验证。

有什么方法可以显式设置 New-PSSession cmdlet 的代理吗? 注意:我不想在 IE 上设置代理设置,只想为每个会话显式设置代理。

您是否尝试过以下方法:

1.) 通过 NETSH 设置代理

2.) 在你的 powershell 方法中使用:

$webclient=New-Object System.Net.WebClient
$creds=Get-Credential
$webclient.Proxy.Credentials=$creds

这就是我所做的,它似乎有效。

$proxyAddress = $proxyHost + ":" + $proxyPort
netsh winhttp set proxy $proxyAddress
$proxysecpasswd = ConvertTo-SecureString $proxyPassword -AsPlainText -Force
$proxycred = New-Object System.Management.Automation.PSCredential($proxyUser, $proxysecpasswd)
$sessionOpts = New-PSSessionOption -ProxyAccessType WinHttpConfig -ProxyCredential $proxycred -ProxyAuthentication Basic

$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($userId, $secpasswd)
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $credential -Authentication "Basic" -AllowRedirection -SessionOption $sessionOpts

如果有其他更好的方法或者这会带来我应该注意的任何其他问题,请提出一些意见。