开发者

PowerShell: Can you grab parts of a upstream, piped command for output?

开发者 https://www.devze.com 2023-01-31 19:57 出处:网络
If I\'m using PowerShell Cmdlets like: Get-ADGroup -Server \"my-dc\" -Filter {name -like \"*Blue*1开发者_运维百科\"} | Get-ADGroupMember | Out-File $output

If I'm using PowerShell Cmdlets like:

Get-ADGroup -Server "my-dc" -Filter {name -like "*Blue*1开发者_运维百科"} | Get-ADGroupMember | Out-File $output

Is there a way that I can output the found group name inside of the text file as well? Currently, this only outputs the group members into the file.


Another variant:

Get-ADGroup -Filter {Name -like "*demo*"} | % { "GroupName: $($_.Name)"; Get-ADGroupMember $_ } | Out-File C:\Scripts\Group.txt

This will have something similar in the text file:

GroupName: DemoUsers

distinguishedName : CN=Ravikanth,CN=Users,DC=BarCamp,DC=in name : Ravikanth objectClass : user objectGUID : c4257f39-c84e-43e3-adb2-dfb6d13a8f2a SamAccountName : Ravikanth SID : S-1-5-21-4177501474-3918321425-3674396201-1000


Try this:

Get-ADGroup | %{
    # Here $_ is the group, do with it what you will :)
    $_
    $_ | get-ADGroupmember
}


Get-ADGroup -Server "my-dc" -Filter {name -like "*Blue*1"} | Get-ADGroupMember | foreach { $_.name | out-file -FilePath $output -Append}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号