簡體   English   中英

(C#) 創建遠程 powershell 會話時出現“拒絕訪問”錯誤

[英](C#) Getting "Access Denied" error when creating remote powershell session

我有 C# 和 powershell 的問題。 我有一個在服務器上運行的 ASP.net WebApi,我需要它打開與 Microsoft Exchange 的遠程 powershell 會話以從房間郵箱中獲取一些數據。 我使用此代碼連接:

PowerShell instance = PowershellHelper.Instance;

instance.AddScript("$passUnsafe = \"" + Password + "\";" +
"$pass = $passUnsafe | convertto-securestring -AsPlainText -Force;" +
"$UserCredential = new-object -typename System.Management.Automation.PSCredential -argumentlist \"" + Username + "\",$pass;" +
"if (!(Get-PSSession | Where { $_.ConfigurationName -eq \"Microsoft.Exchange\" })) { $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection; }" +
"else { $Session = (Get-PSSession | Where { $_.ConfigurationName -eq \"Microsoft.Exchange\" })[0]; }" +
"Import-PSSession $Session -DisableNameChecking;");

PowershellHelper.Instance.Streams.Error.Clear();
instance.Invoke();

這在我的本地機器上工作正常,但在遠程服務器上無效。 腳本在嘗試執行“New-PSSession”時崩潰,並出現以下錯誤:

[outlook.office365.com] Connecting to remote server outlook.office365.com failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.

經過一番研究,我發現這是因為不允許非管理員用戶執行遠程會話命令。 如果我將 IIS 應用程序池用戶添加到管理員組,它工作正常,但我想盡可能避免這種情況。 有人說您可以將用戶添加到內置的“遠程管理用戶”組,但對我不起作用。 我什至試圖直接更改用戶權限,授予它完全訪問權限,但它也不起作用。 僅當我將其添加到管理員組時才有效。 似乎有其他東西阻止了執行,或者 IIS 忽略了用戶組。 憑據沒問題。 有誰知道是否還有其他方法可以在沒有管理員權限的情況下執行遠程 powershell 命令?

我上周在將基於 Web 的用戶配置應用程序從其 Exchange 2010 目標遷移到 Exchange 2016 時遇到了這個問題。最終,問題是應用程序服務器上的權限問題。 IIS APPPOOL\\DefaultAppPool標識試圖將值寫入HKU:\\.DEFAULT\\Software\\Microsoft\\CurrentVersion\\WSMAN\\Client\\ConnectionCookies

我授予了IIS APPPOOL\\DefaultAppPool標識對 ConnectionCookies 鍵的完全控制權限,並且應用程序能夠成功調用New-PSSession

我在一個名為 Mike's Ramblings 的博客上發現了這個決議(Blogspot): http : //coderike.blogspot.com/2016/06/new-pssession-access-denied.html

Mike 發現了 Process Monitor 的問題。 通過運行 Process Monitor、重現問題並搜索具有 ACCESS DENIED 結果的任何事件,我能夠做到這一點。 雙擊事件並單擊進程選項卡將顯示調用用戶。

當我的應用程序使用具有服務帳戶標識的專用應用程序池時,我仍然不明白為什么IIS APPPOOL\\DefaultAppPool標識會嘗試創建此值。

用 Mark Russinovich 的話來說:“有疑問時,進程監視器。”

根據 Simon Li 的評論,您可以使用以下 PowerShell 腳本授予您的帳戶遠程 PowerShell 執行權限:

Set-User "XXXXX" -RemotePowerShellEnabled $true

https://docs.microsoft.com/en-us/powershell/exchange/exchange-server/control-remote-powershell-access-to-exchange-servers?view=exchange-ps

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM