我有一個腳本可以檢查本地管理員并排除預期有權成為本地管理員組的一部分并顯示結果的用戶。有沒有辦法簡化下面的腳本?
$date = Get-Date -Format "MM-dd-yyy-HHmmss"
$report = "C://Temp/ProvisioningForWSReport-$date.txt"
function LocalAdmins{
[cmdletbinding(SupportsShouldProcess=$True,ConfirmImpact="Low")] Param([parameter(Mandatory=$false)][switch]$Enable)
Begin {
$writeTxt = "$(Get-Date) - Currently checking if there are other users who have admin rights on the machine....";
Write-Output $writeTxt
Add-Content -path $report $writeTxt
}
Process {
$hostname = hostname
net localgroup Administrators >> C:\temp\AdminUsers.txt
$User1 = "Administrator"
$User2 = "user1"
$User3 = "user2"
$Text1 = "Members"
$Text2 = "-------------------------------------------------------------------------------"
$Text3 = "The command completed successfully."
(gc C:\temp\AdminUsers.txt ) | ? {$_.trim() -ne "" } | set-content C:\temp\AdminUsers1.txt
$LocalAdmin = Get-Content C:\temp\AdminUsers1.txt | Select-String -Pattern "$User1", "$User2", "$User3", "$Text1", "$Text2", "$Text3" -NotMatch
$writeTxt = "$(Get-Date) - [INFO] These are the users that have admin rights on the machine: $LocalAdmin ";
Write-Output $writeTxt
Add-Content -path $report $writeTxt
del C:\temp\AdminUsers.txt
del C:\temp\AdminUsers1.txt
}
End {}
}
#####Execute Functions#####
LocalAdmins
這是此腳本的示例輸出:
Checking if there are other users have the admin rights on the machine...
01/05/2022 13:27:58 - [INFO] These are the users that have admin rights on the machine: User3 User4
另外,我怎樣才能顯示這樣的結果?
Checking if there are other users have the admin rights on the machine...
01/05/2022 13:27:58 - [INFO] These are the users that have admin rights on the machine: User3, User4
uj5u.com熱心網友回復:
如果您有使用類似 cmdlet 的探測Get-LocalGroupMember,您可以net localgroup Administrators像這樣過濾命令的輸出:
# get the list of user names that are member of the Administrators group
# remove empty and non usable lines of the output
$adminlist = (net localgroup Administrators) | Where-Object { $_ -match '\S' } | Select-Object -Skip 4 | Select-Object -SkipLast 1
# now filter away the members you do not want to be listed
$notThese = 'Administrator', 'user1', 'user2'
$localAdmins = $adminlist | Where-Object { $notThese -notcontains $_ }
現在您在變數中有一組具有管理員權限的用戶名 $localAdmins
要創建您想要的輸出,只需加入陣列并合并到您的字串中:
"$(Get-Date) - [INFO] These are the users that have admin rights on the machine: {0}" -f ($localAdmins -join ', ')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/405512.html
標籤:
