0

hope you are doing well

i want to use an output of users from an OU and process them into a cmdlet. i used an Array and a loop to do the job but i faced issues.

$OUpath= 'OU=xx,OU=xx,OU=xx,DC=xx,DC=xx,DC=xx'
$str= get-aduser -Filter * -SearchBase $OUpath | select-object SamAccountName


$i=0
while($i -lt $str.Length) { Get-ADPrincipalGroupMembership $str[$i] | Select-Object name,distinguishedName | select-string "Disable Screen Saver" ; $i++ }

it shows this error

Get-ADPrincipalGroupMembership : Cannot validate argument on parameter 'Identity'. The Identity property on the argument is null or empty.
At line:6 char:60
+ ... $i -lt $str.Length) { Get-ADPrincipalGroupMembership $str[$i] | Selec ...
+                                                          ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-ADPrincipalGroupMembership], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADPrincipalGroupMembership
 

i tried to convert the array to a string by pipeing it to out-string to a new array before the loop and didnt work out.

  • In short: [`Select-Object`](https://docs.microsoft.com/powershell/module/microsoft.powershell.utility/select-object) (`select`) by default returns _a `[pscustomobject]` instance_ that has the _requested properties_ - even when you're only asking for a _single_ property. To get only that property's _value_, use the `-ExpandProperty` parameter instead of the (possibly positionally implied) `-Property` parameter. See the linked duplicate for details and alternatives, notably the ability to simply use `(...).SomeProperty` – mklement0 Feb 02 '23 at 08:33

0 Answers0