栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

PowerShell用户认证Function实例代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/149155.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号