PowerShell用户认证Function实例代码

网友投稿 176 2023-07-06


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.PrincipalChttp://ontext '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的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:springmvc与mybatis集成配置实例详解
下一篇:完整B树算法Java实现代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~