PowerShell用戶認(rèn)證Function實(shí)例代碼
在最近工作中遇到對(duì)用戶驗(yàn)證,需要根據(jù)用戶名和密碼驗(yàn)證用戶是否合法。在外文網(wǎng)站找到的這段代碼,在這里分享給大家,如果你也需要用戶驗(yàn)證的話,那么可以直接copy使用,現(xiàn)在沒地方用,也可以收藏備用。
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 $_
}
}
}
使用很簡(jiǎn)單方便:Test-UserCredential “用戶名” “密碼” “用戶域”,第三個(gè)參數(shù)“用戶域”為可選參數(shù),返回為布爾類型。
以上就是對(duì)PowerShell 用戶認(rèn)證 Function的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!
相關(guān)文章
Spring Boot中整合PageHelper實(shí)現(xiàn)分頁功能詳細(xì)步驟
在Spring Boot項(xiàng)目中整合PageHelper并實(shí)現(xiàn)分頁查詢功能的全部步驟,通過以上配置和代碼,我們可以輕松地實(shí)現(xiàn)數(shù)據(jù)庫分頁查詢,提高了開發(fā)效率并改善了用戶體驗(yàn),感興趣的朋友跟隨小編一起看看吧2024-05-05
JAVAEE Filter 過濾器設(shè)置是否緩存實(shí)例詳解
網(wǎng)頁中,每次的客戶端訪問服務(wù)器,有部分不用重復(fù)請(qǐng)求的,這樣可以減輕服務(wù)器的工作量。那么如何設(shè)置客戶端是否都緩存呢?接下來通過本文給大家介紹JAVAEE Filter 過濾器設(shè)置是否緩存的實(shí)例,感興趣的朋友一起學(xué)習(xí)吧2016-05-05
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(53)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-08-08

