[英]Powershell check if user is local-user and have admin rights from username and password on local windows machine (Not active directory)
我想編寫一個 PowerShell 腳本,該腳本將用戶名和密碼作為輸入,然后檢查具有這些憑據的用戶是否存在並具有管理員權限。
我看到的大多數問題或文章都是關於活動目錄的。 我不是在談論活動目錄。 我只想檢查一台正常的本地機器。
我已經嘗試過了,但我認為這也與活動目錄有關。
$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)
您甚至不需要密碼,只需要使用 microsoft.powershell.localaccounts 模塊的用戶 ID。
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
樣品運行:
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
注意:上面的用戶名已替換。
高溫高壓
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.