如何将 Windows 组中的所有用户打印到文本文件?

如何将 Windows 组中的所有用户打印到文本文件?

我正在尝试将“学生”组的所有用户打印到文本文件“Students.txt”中。

我不在域中,因此这不起作用:

net group "Students" >>  students.txt

因为我得到了以下信息:

This command can be used only on a Windows Domain Controller.

先感谢您

如果有人对 VB.Net 解决方案感兴趣,我已经编写了一个 Winform 解决方案,其中有一个多行文本框来复制/粘贴成员(无论如何,感谢您的帮助):

  Imports System.DirectoryServices 'first add a refernce to it from .Net Tab'

....

  Public Function MembersOfGroup(ByVal GroupName As String) As List(Of DirectoryServices.DirectoryEntry)
        Dim members As New List(Of DirectoryServices.DirectoryEntry)
        Try
            Using search As New DirectoryServices.DirectoryEntry("WinNT://./" & GroupName & ",group")
                For Each member As Object In DirectCast(search.Invoke("Members"), IEnumerable)
                    Dim memberEntry As New DirectoryEntry(member)
                    members.Add(memberEntry)
                Next
            End Using
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return members
    End Function

    Private Sub TxtGroup_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtGroup.KeyDown
        If e.KeyCode = Keys.Enter Then
            Me.TxtGroupMembers.Text = String.Empty
            If Me.TxtGroup.Text.Length <> 0 Then
                Dim members As List(Of DirectoryServices.DirectoryEntry) = MembersOfGroup(Me.TxtGroup.Text)
                For Each member As DirectoryServices.DirectoryEntry In members
                    Me.TxtGroupMembers.Text &= member.Name & vbCrLf
                Next
            End If
        End If
    End Sub

答案1

如果您可以使用 PowerShell,那么这应该可以解决问题(在本地计算机上运行,$GroupName​​根据您的需要更改变量和输出文件路径):

$GroupName = "Administrators"
$GroupMembers = @()
$Server = $env:computername
$Group= [ADSI]"WinNT://$Server/$GroupName,group"
$Members = @($Group.psbase.Invoke("Members"))
$Members | ForEach-Object { $GroupMembers += $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) }
Write-Output $GroupMembers | out-File "C:\somefolder\somefile.txt"

答案2

您可以尝试使用net localgroup以下/domain参数:

net localgroup "Students" /domain >> students.txt

相关内容