question

$$ANON_USER$$ avatar image
0 Votes"
$$ANON_USER$$ asked IanXue-MSFT commented

PowerShell: "No positional parameter found"

File.csv contains a list of groups that are in different domains. I'm trying to recursively pull the list of users in the list but it keeps giving me the following at at $adgroup:"No positional parameter found"




$groups = Get-Content -path 'C:\Temp\test.csv'
$domains = (Get-ADForest).Domains
$result = foreach ($group in $groups)

{
foreach ($domain in $domains)
{

$adGroup = Get-ADGroup $group -SearchBase "" -Server thegc3268
$x = ($.DistinguishedName -split ‘,DC=‘)[1]
$members = (Get-ADGroupMember $adGroup -Server $x -recursive).where({

             $_.ObjectClass -eq 'User' 
            
         })
            
         foreach($user in $members)
         {

             [pscustomobject]@{
                 GroupName = $adGroup.Name
                 samAccountName = $user.samAccountName
                 distinguishedName = $user.distinguishedName
                 name = $user.name
             }
         }
     }
 }

$result | Export-Csv c:\temp\testing.csv -NoTypeInformation

windows-server-powershellwindows-active-directory
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,

Is there any update? Have you got a chance to verify the below suggestions?
Please feel free to let us know if more assistance is needed. If the reply is helpful, please “Accept Answer” to help other community members find it more easily,

0 Votes 0 ·

1 Answer

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 commented

There are two undeclared variables in your code. Fix those and try this:

 Get-Content -path 'C:\Temp\file.csv' |
     ForEach-Object {
         #
         # The variable $domains has not been declared or initialized on the following line!!
         foreach ($domain in $domains) {
             $adGroup = Get-ADGroup $_ -Searchbase "" -server thegc3268      # "-Searchbase" is unnecessary in this context
             #
             # The variable $info has no been declared or initialized on the following line
             $x = ($info.DistinguishedName -split ',DC=')[1]
             Get-ADGroupMember $adGroup -Server $x -recursive |
                 Where-Object {
                     $_.ObjectClass -eq 'User' 
                 } |​​​​​​​
                     ForEach-Object {
                         [pscustomobject]@{​​​​​​
                             GroupName = $adGroup.Name
                             samAccountName = $_.samAccountName
                             distinguishedName = $_.distinguishedName
                             name = $_.name
                         }​​​​​​​
                     }​​​​​​​
         }​​​​​​​ | Export-Csv c:\temp\output.csv -NoTypeInformation


· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for catching those-it should not be "info", it should be "x". Made the change in the original question.

and $domain = thegc3268 pulls "The operation is not supported on the global catalog port"

0 Votes 0 ·

The variable "$x" is undefined in your corrected code.

Remove the -SearchBase "" from the Get-ADGroup cmdlet (or provide a usable value).

0 Votes 0 ·

$domains =(get-adforest).domains
Get-Content -path 'C:\Temp\file.csv' |
ForEach-Object {
foreach ($domain in $domains) {
$adGroup = Get-ADGroup $_ -server thegc3268

          $x = ($x.DistinguishedName -split ',DC=')[1]
          Get-ADGroupMember $adGroup -Server $x -recursive |
              Where-Object {
                  $_.ObjectClass -eq 'User' 
              } |​​​​​​​
                  ForEach-Object {
                      [pscustomobject]@{​​​​​​
                          GroupName = $adGroup.Name
                          samAccountName = $_.samAccountName
                          distinguishedName = $_.distinguishedName
                          name = $_.name
                      }​​​​​​​
                  }​​​​​​​
      }​​​​​​​ | Export-Csv c:\temp\output.csv -NoTypeInformation
0 Votes 0 ·

I think I'm querying GC incorrectly
The above code outputs the error "the operation isn't supported in global catalog port"

0 Votes 0 ·
Show more comments