2011-09-09 21 views
0

如何創建$ Null用戶名和$ Null密碼PScredentials對象?

根據本文,空PSCredential會導致Powershell使用Windows身份驗證,這似乎是在域設置中運行腳本的更簡單的方法。可惜的是我似乎無法找出其中/如何他將其設置爲NULL: http://blogs.msdn.com/b/dvespa/archive/2010/02/22/how-to-use-windows-authentication-with-the-pscredential-class.aspx

其他資源: 指定$空密碼這樣的回答,但不會允許$空的用戶名。 Create PSCredential without a password

謝謝。

回答

0

爲什麼你需要一個$ null的PSCredential對象?

根據文檔

-Credential <PSCredential> 

Specifies a user account that has permission to perform this action. The default is the current user. 

這意味着塔,如果你不使用這個參數,你會使用Windows身份驗證。


編輯:

因此在PowerShell中,如果你想有一個空的憑證,你只需要指定它:

測試:

Get-WmiObject Win32_Process -Credential 

Get-WmiObject Win32_Process -Credential $null 

Get-WmiObject Win32_Process -Credential (Get-Credential) 
+0

嗨,我嘗試了各種方式讓Windows身份驗證工作,也許這個問題是我們公司獨一無二的。我們有多個具有域名信任的域名,所以我可以使用「母帶」域名信譽來導航其他系統。但是,它似乎並沒有轉化爲PowerShell入門。所以,我試圖用空pscreds來欺騙它。 如果我不能,我將不得不爲每個連接到的服務器使用域特定的信譽。這使事情變得複雜。 –

+0

需要與Windows登錄憑據對應的「空」憑證有很多原因。假設有人給我一個腳本來做一些事情,腳本中的一個參數就是這樣做時使用的憑證。我可能希望在某些非完整的管理腳本中使用此腳本,並使用管理腳本運行的憑據。 –

+0

不確定要真正理解,但我編輯我的答案。 – JPBlanc

0

常數[PSCredential]::Empty(又名[System.Management.Automation.PSCredential]::Empty)爲您提供PSCredential類型的有效對象,但usernamepassword均設置爲空。

但是,這不是當前用戶的憑據;相反,它意味着「沒有憑據」。您所稱的函數中可能存在邏輯錯誤(例如,當函數的邏輯說當使用當前的安全上下文時,在$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;絕對值得一讀。