0

I am using this line of code in powershell and it works but it displays too much. I am trying to find and display where the group that contains "Avecto" in the name.

dsquery user -samid MyUSerName| dsget user -memberof | dsget group -samid

Is there anyway to add that on this script statement?

Del
  • 29
  • 7
  • 2
    Any reason why you are using dsquery - dsget for this vs the built-in ADDS group membership cmdlets (Get-ADGroupMember, Get-ADPrincipalGroupMembership, GetAuser -memberof) for this effort? Just curious. You get these by installing the MS RSAT tool, or using implicit remoting to an DC to proxy the cmdlets to your session. – postanote Sep 11 '18 at 20:12

1 Answers1

0

Use the where keyword in PS (shorthanded here as ?)

dsquery user -samid MyUSerName| dsget user -memberof | dsget group -samid | ? {$_ -match 'avecto'}

So it would read where each item in the pipeline matches the string 'Avecto'

You could store the data into a variable and manipulate further, as just a simple Object array will be returned if more than one result, otherwise in a single result it will just be a string

Dharman
  • 30,962
  • 25
  • 85
  • 135
trebleCode
  • 2,134
  • 19
  • 34
  • Actually `where` and `?` are both aliases for the `Where-Object` cmdlet. – Bill_Stewart Sep 11 '18 at 21:08
  • This worked. thank you :). I had the syntax with the bracket messed up. – Del Sep 12 '18 at 11:10
  • @Del no problem. Would you mind marking it at the answer please? – trebleCode Sep 12 '18 at 23:56
  • I do have an additional question. How would I error check this MyUserName is null? – Del Sep 13 '18 at 11:50
  • You could do something like: `$myUserName = 'yourusernamehere'; $userTest = dsquery user -samid $myUserName; $matchString = 'avecto'; if($userTest -ne $null) { $userGroups = @(dsquery user -samid MyUSerName| dsget user -memberof | dsget group -samid | ? {$_ -match $matchString}); if($userGroups.Count -gt 0){ $userGroups} else { Write-Output "User $myUserName is valid, but does not belong to any groups matching string $matchString" } }` – trebleCode Sep 14 '18 at 12:39