简体   繁体   English

获取OU中每个广告组的所有成员

[英]Get all members of every AD Groups in OU

I'm new to Powershell and AD. 我是Powershell和AD的新手。 I've got the challenge of being able to query an OU to retrieve all Groups in that OU and then all members of each group. 我面临的挑战是能够查询OU以检索该OU中的所有组,然后检索每个组的所有成员。 I've currently got the following script which allows me to do it: 我目前有以下脚本可以执行此操作:

$Groups = Get-ADGroup -Filter * -SearchBase "OU=xx,OU=xx xx,DC=xx,DC=xx" -SERVER "xxxxxx" #creates a variable with the name Groups and stores all the groups into it

$Results = foreach( $Group in $Groups ){ #looks for members in each group and stores them in Results
    Get-ADGroupMember -Identity $Group -SERVER "xxxxxx" | Select distinguishedName | foreach {
        [pscustomobject]@{
            GroupName = $Group.Name
            Name = $_
        }
    }
}

$Results| sort -Property GroupName | Export-Csv -Path c:\Temp\groups.csv -NoTypeInformation #stores results in a csv

The issue that I've got is I need to be able to do the same but in one line so that I can run it and pull it back into Excel (for reporting on). 我遇到的问题是,我需要能够执行同一操作,但只需要一行就可以运行它,然后将其拉回Excel(用于报告)。 I can't execute the script from Excel due to signing but I could execute one line. 由于签名,我无法从Excel执行脚本,但可以执行一行。 Any advice would be greatly appreciated 任何建议将不胜感激

You could use the EncodedCommand option: 您可以使用EncodedCommand选项:

$ScriptText  = Get-Content C:\Path\To\Script.ps1 -Raw
$ScriptBytes = [System.Text.Encoding]::Unicode.GetBytes($ScriptText)
$EncCommand  = [System.Convert]::ToBase64String($ScriptBytes)

Now copy the contents of $EncCommand (with $EncCommand |Set-ClipBoard for example) and supply the resulting base64 string as a command line argument to the EncodedCommand command line switch: 现在复制$EncCommand的内容(例如,使用$EncCommand |Set-ClipBoard ),并将生成的base64字符串作为命令行参数提供给EncodedCommand命令行开关:

powershell.exe -EncodedCommand JABHAHIAbwB1AHAAcwAgAD0AIABHAGUAdAA...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM