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

时间:2017-09-12 03:12:39

标签: powershell proxy office365 exchange-server

我正在开发连接到outlook office365(在线交流)的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上设置代理设置,只想在每个会话中明确设置代理。

提前致谢。

2 个答案:

答案 0 :(得分:0)

您是否尝试过以下操作:

1。)通过NETSH设置代理

2。)在您的powershell方法中使用:

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

答案 1 :(得分:0)

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

$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

如果还有其他更好的方法,或者如果这会引起我应该注意的任何其他问题,请发表一些意见。