PowerShell user authentication function Instance code _java

Source: Internet
Author: User

   has encountered a user authentication in its recent work and needs to verify that the user is legitimate based on the user name and password. In the foreign language website found this piece of code, here to share to everyone, if you also need user verification, then you can directly copy use, now there is no place to use, you can also collect spare.

Function test-usercredential {[cmdletbinding ()] [OutputType ([System.Boolean])] param ([Parameter (mandatory = $true)] [Validatenotnullorempty ()] [System.String] $Username, [Parameter (mandatory= $true)] [Validatenotnul

     Lorempty ()] [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 isn't a member of a domain. ' else {$principalContext = New-object-typename System.DirectoryServices.AccountManagement.PrincipalContext ' Domain ', $system. Domain}} else {$principalContext = New-object-typename System.DirectoryServices.AccountMan Agement. PrincipalcontExt ' Machine ', $env: COMPUTERNAME} return $principalContext. Validatecredentials ($Username, $Passwo RD)} catch {throw ' Failed to test user credentials. The error was: ' {0} '. '-F $_}}

Easy to use: test-usercredential "username" "Password" "User domain", the third parameter "user domain" is an optional parameter, and returns to the Boolean type.

The above is the PowerShell user authentication function data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.