常數[PSCredential]::Empty
(又名[System.Management.Automation.PSCredential]::Empty
)爲您提供PSCredential
類型的有效對象,但username
和password
均設置爲空。
但是,這不是當前用戶的憑據;相反,它意味着「沒有憑據」。您所稱的函數中可能存在邏輯錯誤(例如,當函數的邏輯說當使用當前的安全上下文時,在$credentials -eq [PSCredential]::Empty
中),但在某些情況下,該相同的值可能用於其他目的(例如說你想使用匿名認證)。
您無法獲取當前用戶的憑證而不提示或以其他方式明確分配它們;否則這會帶來安全風險
#create a credential object for demo purposes.
#Imagine that instead of this line we were saying `$credential = Get-CurrentUserCredential`
#(you have to imagine this, since no such function exists).
$credential = [PSCredential]::new('myUsername', ('superSecretPassword' | ConvertTo-SecureString -AsPlainText -Force))
#we can now see this user's password;
#someone malicious could have this script run under the current user's
#profile & have this information reported back to them.
$credential.GetNetworkCredential().Password
如果你想運行的東西作爲當前用戶,你已經是(即因此它們是當前用戶);所以你不需要獲得他們的憑證。
也就是說,在有些情況下,如果有他們的憑據會很好;例如如果訪問不使用當前用戶上下文的資源/需要顯式憑證。在這種情況下,您必須提示輸入憑證(Get-Credential $env:username
),或者從某個資源中讀取憑證(請參閱https://stackoverflow.com/a/6240319/361842)。
這裏有一個非常全面的證書解釋:http://duffney.io/AddCredentialsToPowerShellFunctions;絕對值得一讀。
嗨,我嘗試了各種方式讓Windows身份驗證工作,也許這個問題是我們公司獨一無二的。我們有多個具有域名信任的域名,所以我可以使用「母帶」域名信譽來導航其他系統。但是,它似乎並沒有轉化爲PowerShell入門。所以,我試圖用空pscreds來欺騙它。 如果我不能,我將不得不爲每個連接到的服務器使用域特定的信譽。這使事情變得複雜。 –
需要與Windows登錄憑據對應的「空」憑證有很多原因。假設有人給我一個腳本來做一些事情,腳本中的一個參數就是這樣做時使用的憑證。我可能希望在某些非完整的管理腳本中使用此腳本,並使用管理腳本運行的憑據。 –
不確定要真正理解,但我編輯我的答案。 – JPBlanc