PowerShell用户认证Function实例代码
更新时间:2016年09月15日 09:47:58 作者:破狼
这篇文章主要介绍了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的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!
相关文章
详解@ConditionalOnMissingBean注解的作用
这篇文章主要介绍了详解@ConditionalOnMissingBean注解的作用,@ConditionalOnMissingBean,它是修饰bean的一个注解,主要实现的是,当你的bean被注册之后,如果而注册相同类型的bean,就不会成功,它会保证你的bean只有一个,需要的朋友可以参考下2023-10-10Spring Cloud Nacos 和 Eureka区别解析
Spring Cloud Nacos 和 Spring Cloud Eureka 都是 Spring Cloud 微服务框架中的服务注册和发现组件,用于帮助开发者轻松地构建和管理微服务应用,这篇文章主要介绍了Spring Cloud Nacos 和 Eureka区别,需要的朋友可以参考下2023-08-08SSM框架通过mybatis-generator自动生成代码(推荐)
这篇文章主要介绍了SSM框架通过mybatis-generator自动生成代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2017-11-11
最新评论