我正在尝试获取以下脚本,以将一组服务器上的所有本地管理员成员和域组导出到csv中。导出本地管理员可以正常工作,但是我注意到它不导出域组(即:我有一个位于本地管理员组中的Domain Admins组,并且不在csv中显示)。
这是我拥有的代码,将不胜感激:
param(
[parameter(Position=0,ValueFromPipeline=$true)]
$ComputerName=[Net.Dns]::GetHostName(),
[System.Management.Automation.PSCredential] $Credential,
[UInt32] $BlockSize=50
)
begin {
$WMIEnumOpts = new-object System.Management.EnumerationOptions
$WMIEnumOpts.BlockSize = $BlockSize
function Get-LocalAdminGroupMember {
param(
[String] $computerName,
[System.Management.Automation.PSCredential] $credential
)
$params = @{
"Class" = "Win32_Group"
"ComputerName" = $computerName
"Filter" = "LocalAccount=TRUE and SID='S-1-5-32-544'"
}
if ( $credential ) {
if ( $computerName -eq [Net.Dns]::GetHostName() ) {
Write-Warning "The -Credential parameter is ignored for the current computer."
}
else {
$params.Add("Credential", $credential)
}
}
Get-WmiObject @params | ForEach-Object {
$groupName = $_.Name
$_.GetRelated("Win32_Account","Win32_GroupUser","","",
"PartComponent","GroupComponent",$false,$WMIEnumOpts) | Select-Object `
@{Name="ComputerName"; Expression={$_.__SERVER}},
@{Name="Name"; Expression={$groupName}},
@{Name="Member"; Expression={$_.Caption -replace "^$($_.__SERVER)\\", ""}},
@{Name="Type"; Expression={$_.__CLASS}}
}
}
}
process {
$Filename = PATH HERE
$OutFileName = "C:\temp\admins.csv"
Get-Content $Filename | Foreach-Object {Get-LocalAdminGroupMember -computerName $_ | Select-Object * | Export-csv -NoType $OutFileName -Append}
答案 0 :(得分:0)
啊,尝试从远程计算机访问网络资源的乐趣。您将失去任何由域帐户执行的操作。这只是它的工作原理。好消息是,还有一种获取所需信息的方法,如果需要,您甚至可以使用Get-WmiObject
来获取。如果您尚未重命名Administrators组(因为确实是谁做的?),您可以轻松地做到这一点,但是如果您这样做了,并且必须像上面一样通过SID查找该组,则必须查询像您一样的远程服务器,并使用您获得的修改后的名称通过以下查询进行调整。我建议使用Win32_GroupUser类代替此操作:
Get-WmiObject -ComputerName $Server -Query "SELECT * FROM win32_GroupUser WHERE GroupComponent = ""Win32_Group.Domain='$computerName',Name='Administrators'"""
要使其代替您的功能,它可能看起来像这样:
function Get-LocalAdminGroupMember {
param(
[String] $computerName,
[System.Management.Automation.PSCredential] $credential
)
$params = @{
"ComputerName" = $computerName
"Query" = "SELECT * FROM win32_GroupUser WHERE GroupComponent = ""Win32_Group.Domain='$computerName',Name='Administrators'"""
}
if ( $credential ) {
if ( $computerName -eq [Net.Dns]::GetHostName() ) {
Write-Warning "The -Credential parameter is ignored for the current computer."
}
else {
$params.Add("Credential", $credential)
}
}
Get-WmiObject @params |
Where{$_.PartComponent -match ':(.+?)\.Domain="(.+?)",Name="(.+?)"'}|
ForEach{
[PSCustomObject]@{
"ComputerName"=$computerName
"Name"='Administrators'
"Member"=$Matches[2..3] -join '\' -replace "^$computerName\\"
"Type"=$Matches[1]
}
}
}