简体   繁体   中英

Powershell check if user is local-user and have admin rights from username and password on local windows machine (Not active directory)

I want to write a PowerShell script that takes username and password as input and then it checks if a user with those credentials exists and has admin rights.

Most of the questions or articles I have seen are about the active directory. I'm not talking about the active directory. I just want to check for a normal local machine.

I've tried this but I think this is about the active directory too.

$username = read-host 'Enter username'
$password = read-host 'Enter pass'

$computer = $env:COMPUTERNAME

Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$obj = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('machine', $computer)
$obj
$obj.ValidateCredentials($username, $password) 

You don't even need the password only the Userid using the microsoft.powershell.localaccounts module.

Function Test-Admin {

#Note: this requires that the module micorosft.powershell.localacounts
#      be loaded BEFORE the function is run as including a required directive
#      would cause the program to fail if the program loads the module!

  Param (
   [Parameter(Mandatory=$True)]
     [String] $UserToFind 
  )

$Admins = Get-LocalGroupMember -Group Administrators

If ( $Null -eq $($Admins.Name -like "*$UserToFind*") ) { 
        $Status = ' Not ' }
Else  { $Status = ' '     }

  "$UserToFind is" + $Status + "an Administrator of this Machine"

} #End Test-Admin

Sample runs:

PS> Test-Admin -UserToFind ImAnAdmin
ImAnAdmin is an Administrator of this Machine

PS> Test-Admin -UserToFind ImNotAnAdmin
ImNotAnAdmin is Not an Administrator of this Machine

Note: user names above replaced.

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM