[英]Get All local members and groups displayed together
到目前為止,我擁有下面的腳本,它像一個超級按鈕一樣起作用,但是只列出了“管理員”組的成員。 由於我的服務器可能是德語,法語……我無法保證這樣的組將與英語單詞一起存在。 因此,我想對其進行調整以收集所有組和關聯的成員,而不是僅收集管理員。。。
下面的腳本列出了非空本地組中的所有用戶。 但是,我想在CSV中也輸入用戶所屬的組的名稱,以獲取更清晰的解釋。
有人可以幫我嗎? 我有點卡住,一無所有。
$Servers=Get-Content ListOfComputers.txt
$output = 'ListOfLocalAdministratorsGroup.csv'
$results = @()
foreach($server in $Servers)
{
$admins = @()
$computer =[ADSI]"WinNT://$server"
$computer.psbase.children | where { $_.psbase.schemaClassName -eq 'group' } | foreach {
$group =[ADSI]$_.psbase.Path
$members = @($group.psbase.Invoke("Members"))
$members | foreach {
$obj = new-object psobject -Property @{
Server = $Server
Admin = $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)
}
$admins += $obj
}}
$results += $admins
}
$results| Export-csv $Output -NoTypeInformation
本地管理員組將始終具有以下sid:S-1-5-32-544( 在Windows操作系統中的知名安全標識符中記錄。 )
因此,您可以將以下內容添加到腳本中以獲得正確的組名:
$objSID = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-544")
$objgroup = $objSID.Translate( [System.Security.Principal.NTAccount])
$objgroupname = ($objgroup.Value).Split("\")[1]
在Trondh的最后編輯中,它像一種魅力。
這是代碼的最新版本。 因此,它將收集本地Administrators組的所有成員(獨立於其使用的語言)
非常感謝 :) !!
#The Third section will query each computer in the ListOfComputers.txt to get the members of the local group Administrators
$Servers=Get-Content ListOfComputers.txt
$output = 'ListOfLocalAdministratorsGroup.csv'
$results = @()
$objSID = New-Object System.Security.Principal.SecurityIdentifier("S-1-5-32-544")
$objgroup = $objSID.Translate( [System.Security.Principal.NTAccount])
$objgroupname = ($objgroup.Value).Split("\")[1]
foreach($server in $Servers)
{
$admins = @()
$group =[ADSI]"WinNT://$server/$objgroupname"
$members = @($group.psbase.Invoke("Members"))
$members | foreach {
$obj = new-object psobject -Property @{
Server = $Server
Admin = $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)
}
$admins += $obj
}
$results += $admins
}
$results| Export-csv $Output -NoTypeInformation
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.