PowerShell用戶認證Function實例代碼。本站提示廣大學習愛好者:(PowerShell用戶認證Function實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是PowerShell用戶認證Function實例代碼正文
在比來任務中碰到對用戶驗證,須要依據用戶名和暗碼驗證用戶能否正當。在外文網站找到的這段代碼,在這裡分享給年夜家,假如你也須要用戶驗證的話,那末可以直接copy應用,如今沒處所用,也能夠珍藏備用。
Function Test-UserCredential {
[CmdletBinding()] [OutputType([System.Boolean])]
param(
[Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()]
[System.String] $Username,
[Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()]
[System.String] $Password,
[Parameter()]
[Switch] $Domain
)
Begin {
$assembly = [system.reflection.assembly]::LoadWithPartialName('System.DirectoryServices.AccountManagement')
}
Process {
try {
$system = Get-WmiObject -Class Win32_ComputerSystem
if ($Domain) {
if (0, 2 -contains $system.DomainRole) {
throw 'This computer is not a member of a domain.'
} else {
$principalContext = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext 'Domain', $system.Domain
}
} else {
$principalContext = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext 'Machine', $env:COMPUTERNAME
}
return $principalContext.ValidateCredentials($Username, $Password)
}
catch {
throw 'Failed to test user credentials. The error was: "{0}".' -f $_
}
}
}
應用很簡略便利:Test-UserCredential “用戶名” “暗碼” “用戶域”,第三個參數“用戶域”為可選參數,前往為布爾類型。
以上就是對PowerShell 用戶認證 Function的材料整頓,後續持續彌補相干材料,感謝年夜家對本站的支撐!