我有一個簡單的腳本:
$ErrorPreference = 'Stop'
$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
if ($null -eq $user) {
throw 'User and password are required'
} else {
$password = (Get-Credential -credential $user).Password
if ($null -eq $password) {
throw 'Password is required'
} else {
$plainpassword = [System.Net.NetworkCredential]::new("", $password).Password
$service = Get-WMIObject -class Win32_Service -filter "name='myservice'"
if ($null -ne $service) {
[void]$service.change($null, $null, $null, $null, $null, $false, $user, $plainpassword)
} else {
throw 'Service was not installed properly'
}
}
}
當我在憑據輸入對話框中按“取消”時,用戶和密碼必須是$null. 所以,我在$null. 但我收到一個錯誤:
Get-Credential : Cannot bind argument to parameter 'Credential' because it is null.
At C:....ps1:6 char:45
$password = (Get-Credential -credential $user).Password
~~~~~
CategoryInfo : InvalidData: (:) [Get-Credential], ParameterBindingValidationException
FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetCredenti
alCommand
Password is required
At C:....ps1:8 char:9
throw 'Password is required'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : OperationStopped: (Password is required:String) [], RuntimeException
FullyQualifiedErrorId : Password is required
我看到throw 'Password is required'但同時我看到:
Get-Credential : Cannot bind argument to parameter 'Credential' because it is null.
我希望如果$user是,$null那么它不應該發生,我什至會在這條線之前退出。為什么會發生這種情況以及如何處理?
PS。這似乎$ErrorPreference = 'Stop'是多余的,只是為了確定......
uj5u.com熱心網友回復:
tl;博士
# Get the current user's domain-qualified name, using whoami.exe for simplicity
# (same as [System.Security.Principal.WindowsIdentity]::GetCurrent().Name).
# (Alternatively, use "$env:USERDOMAIN\$env:USERNAME", or
# if the domain prefix isn't needed just $env:USERNAME)
$user = whoami
# Prompt for the current user's password.
# Note the use of -UserName rather than -Credential.
# -Message is then unfortunately *required* in *Windows PowerShell*,
# but no longer in *PowerShell (Core) 7 *
$cred = get-credential -UserName $user -Message 'Enter your credentials:'
# Abort, if the prompt was canceled (return value is $null)
# or an empty password was given (a [pscredential] instance was returned,
# but the length of its .Password property is 0).
if ($null -eq $cred -or $cred.Password.Length -eq 0) { throw "Password is required." }
# Get the plain-text password for the later WMI method call.
# !! THIS SHOULD GENERALLY BE AVOIDED.
$password = $cred.GetNetworkCredential().Password
# ... Make your WMI call
至于你嘗試了什么:
Get-Credential -credential $user
Get-Credential的-Credential引數是一個奇怪的野獸,最好避免,因為它期望一個已經是實體[pscredential]的引數作為它的引數。
正如您所做的那樣,僅將用戶名傳遞給它,以便預先填寫用戶名欄位,原則上是可行的,但是當您取消對話框時會導致(陳述句終止)錯誤,并伴隨著令人困惑的錯誤訊息:
Cannot bind argument to parameter 'Credential' because it is null- 盡管您顯然確實將引數傳遞給-Credential.錯誤的原因是,由于引數被屬性修飾,因此將僅使用名稱傳遞給
-Credential自身會觸發憑據提示,并且這發生在從該提示接收回傳值之前:-CredentialSystem.Management.Automation.CredentialAttributeGet-Credential如果該提示被取消,則回傳,然后
$null嘗試系結到引數會觸發錯誤。$null-Credential相反,如果您傳遞一個預先存在的
[pscredential]實體,則不會顯示任何提示,并且該實體只是通過 傳遞。
改用-UserName引數(假設你需要在提示中預填一個用戶名),這樣行為更明智;不幸的是,出于語法原因,在Windows PowerShell中,您還必須使用引數,-Message如上所示;幸運的是,這不再是PowerShell (Core) 7 的要求:
如果取消對話框,
$null則回傳[1],不會發生錯誤。- 注意:在PowerShell (Core) 7 中,
Get-Credential不再使用GUI對話框提示,而是直接在控制臺視窗中提示。您不能再取消這樣的提示,但是您可以按Ctrl C,這會自動終止整個腳本。
- 注意:在PowerShell (Core) 7 中,
否則,將
[pscredential]回傳一個實體,其中包含用戶名和密碼作為[securestring].
[1] 從技術上講,沒有輸出,PowerShell 使用有時稱為“AutomationNull”的特殊單例發出信號 - 請參閱此答案。但是,在比較等運算式-eq中,它的行為類似于$null.
uj5u.com熱心網友回復:
因為該 cmdlet 需要引數,所以您必須將呼叫包裝在 try catch 塊中以捕獲該 ParameterBindingException
$ErrorPreference = 'Stop'
$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
if ($null -eq $user) {
throw 'User and password are required'
} else {
try {
$cred = Get-Credential -Credential $user
$password = ($cred.getNetworkCredential()).Password
$service = Get-WMIObject -class Win32_Service -filter "name='myservice'"
if ($null -ne $service) {
[void]$service.change($null, $null, $null, $null, $null, $false, $user, $password)
} else {
throw 'Service was not installed properly'
}
}
catch [System.Management.Automation.ParameterBindingException]{
throw 'Password is required'
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/522518.html
標籤:电源外壳
上一篇:查找陣列/表的更簡單方法?
