Powershell

無法讓 CredSSP 身份驗證在 PowerShell 中工作

  • October 17, 2014

在嘗試使用遠端處理創建 PowerShell 腳本時,我遇到了我認為是雙跳問題。在那篇文章中,Perriman 給出了問題的簡潔描述以及解決問題的具體步驟(如果您知道命令,這幾乎是微不足道的,但對於像我這樣不太熟悉的人來說,這些資訊非常寶貴!)。

Enable-WSManCredSSP Server在我的 Win7 伺服器上執行時沒有發生任何事故,但嘗試Enable-WSManCredSSP Client –DelegateComputer <FQDN of the server>在我的 Win7 客戶端上執行時產生了這個錯誤:

Enable-WSManCredSSP : The client cannot connect to the destination specified
in the request. Verify that the service on the destination is running and
is accepting requests.
Consult the logs and documentation for the WS-Management service running
on the destination, most commonly IIS or WinRM. If the destination
is the WinRM service, run the following com mand on the destination
to analyze and configure the WinRM service: "winrm quickconfig".

執行winrm quickconfig確認我的伺服器正在執行 WinRM:

WinRM already is set up to receive requests on this machine.
WinRM already is set up for remote management on this machine.

Get-WSManCredSSP確認我的伺服器已準備好接受來自客戶端的憑據:

The machine is not configured to allow delegating fresh credentials.
This computer is configured to receive credentials from a remote client computer.

我還找到了 Boessen關於 WinRM 的文章,其中他描述了一般的 WinRM 設置,並發現了一個可以在診斷中獲得有用數據點的花絮;在客戶端執行的這個命令使用winrs工具遠端訪問伺服器:

winrs -r:http://<FQDN of my server>:5985 -u:<myDomain>\msorens "dir c:\"

該命令返回了預期的結果,即伺服器上根目錄的內容,沒有發生任何意外,確認我的 FQDN 是正確的並且 WinRM 已啟用。

Boessen 指出埠 5985 是 Win7 的預設埠;在伺服器上執行的此命令確認值為 5985:

get-item wsman:\localhost\listener\listener*\port

問題:為什麼我無法在客戶端執行 Enable-WSManCredSSP 命令?


2011.06.07 更新

我找到了上述問題的解決方案:呼叫Enable-PSRemoting,宣傳配置電腦以接收遠端命令,允許客戶端上的Enable-WSManCredSSP成功工作!很好奇,但它的手冊頁表明它執行了許多不同的操作,所以我假設其中一個無意中做了我需要的操作。

但是當我嘗試使用 CredSSP 身份驗證時,我遇到了另一個障礙。這是命令:

Invoke-Command { Write-Host "hello, world" } -computername $serverName `
-credential $testCred  -Authentication Credssp

這是回應:

連接到遠端伺服器失敗並顯示以下錯誤消息:
WinRM 客戶端無法處理該請求。電腦策略不允許
將使用者憑據委託給目標電腦。使用 gpedit.msc
並查看以下策略:電腦配置
-> 管理模板 -> 系統 -> 憑證委派
-> 允許委派新的憑證。驗證它是否已啟用並且
配置了適合目標電腦的 SPN。例如,
對於目標電腦名稱“myserver.domain.com”,SPN 可以是以下之一
以下內容:WSMAN /myserver.domain.com 或 WSMAN/*.domain.com。
有關詳細資訊,請參閱 about_Remote_Troubleshooting 幫助主題。

我驗證了這些設置,正如這條非常有用的錯誤消息所建議的那樣,在我看來它配置正確。

新問題:這個與 CredSSP 的遠端連接嘗試失敗了什麼?


在回答時請記住以下幾點: 讓我提前消除任何我知道我在這裡做什麼的想法,儘管有任何相反的表現。:-) Windows 管理員不是我的專業領域!

在短暫的中斷後,我又回到了這個問題上,用新的眼光(我的和同事)再次審視,並決定再次回到基礎:

在我執行的客戶端上(在管理員 shell 中):

enable-wsmancredssp -role client -delegatecomputer devremvm03 -force

在我執行的伺服器上(在管理員 shell 中):

enable-wsmancredssp -role server -force

兩者都返回正常輸出,表明 CredSSP 現在是“真”。

然後,我使用以下練習器程式碼來逐步提高複雜性:

$block = {
 Write-Host ("hello, world: {0}, {1}" -f $env:USERNAME, (hostname))
}
$username = "test_user"
$password = "..."   
$adjPwd = $password | ConvertTo-SecureString -asPlainText -Force
$testCred = (New-Object System.Management.Automation.PSCredential($username,$adjPwd))    

switch ($choice)
{
 "basic"       { Invoke-Command $block }
 "remote"      { Invoke-Command $block -computername $serverName }
 "credentialA" { Invoke-Command $block -computername $serverName -credential $testCred  }
 "credentialB" { Invoke-Command $block -computername $serverName -credential $testCred  -Authentication Credssp}
 "session"     { 
     $testSession = New-PSSession -computername $serverName -credential $testCred -Authentication Credssp
     if ($testSession) { Invoke-Command $block -Session $testSession; Remove-PSSession $testSession }
     }
}

所有這些都在我的 run.ps1 腳本中,因此成績單如下(並且在管理員 shell 中執行):

PS C:\> .\run.ps1 basic
hello, world: msorens, MyLocalMachine
PS C:\> .\run.ps1 remote MyRemoteServer
hello, world: msorens, MyRemoteServer
PS C:\> .\run.ps1 credentialA MyRemoteServer
hello, world: test_user, MyRemoteServer
PS C:\> .\run.ps1 credentialB MyRemoteServer
hello, world: test_user, MyRemoteServer
PS C:\> .\run.ps1 session MyRemoteServer
hello, world: test_user, MyRemoteServer

以前,只有 basic、remote 和 credentialA 有效。現在所有5個工作。哇!

引用自:https://serverfault.com/questions/277573