【问题标题】:Proxy setting for powershell connecting to outlook.office365Powershell 连接到 Outlook.office365 的代理设置
【发布时间】:2018-02-20 09:08:45
【问题描述】:

我正在编写连接到 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 上设置代理设置,只想为每个会话显式设置代理。

【问题讨论】:

    标签: powershell proxy office365 exchange-server


    【解决方案1】:

    您是否尝试过以下方法:

    1.) 通过 NETSH 设置代理

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

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

    【讨论】:

    • 感谢您的建议,但 New-PSSession cmdlet 中似乎没有使用 $webclient。还有什么办法吗?
    【解决方案2】:

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

    $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
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-18
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多