PowerShell用户认证Function实例代码
在最近工作中遇到对用户验证,需要根据用户名和密码验证用户是否合法。在外文网站找到的这段代码,在这里分享给大家,如果你也需要用户验证的话,那么可以直接copy使用,现在没地方用,也可以收藏备用。
FunctionTest-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-ClassWin32_ComputerSystem
if($Domain){
if(0,2-contains$system.DomainRole){
throw'Thiscomputerisnotamemberofadomain.'
}else{
$principalContext=New-Object-TypeNameSystem.DirectoryServices.AccountManagement.PrincipalContext'Domain',$system.Domain
}
}else{
$principalContext=New-Object-TypeNameSystem.DirectoryServices.AccountManagement.PrincipalContext'Machine',$env:COMPUTERNAME
}
return$principalContext.ValidateCredentials($Username,$Password)
}
catch{
throw'Failedtotestusercredentials.Theerrorwas:"{0}".'-f$_
}
}
}
使用很简单方便:Test-UserCredential “用户名”“密码”“用户域”,第三个参数“用户域”为可选参数,返回为布尔类型。
以上就是对PowerShell用户认证Function的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短