title: PowerShell: Cannot find an overload for IndexOf and the argument count: 2
body: <p>I am attempting to use a portion of a script I wrote to return a list of Local Groups a specified user may be part of on a remote server so they can be quickly removed from said Local Group. Everything seems to work fine until the groups a person may be part of goes below two. When there is only one group I get the following error:</p>
  
  <pre><code>Cannot find an overload for "IndexOf" and the argument count: "2".
  At line:177 char:30
  +                 [string]([array]::IndexOf &lt;&lt;&lt;&lt; ($localGroups, $_)+1) + ". " + $_
      + CategoryInfo          : NotSpecified: (:) [], MethodException
      + FullyQualifiedErrorId : MethodCountCouldNotFindBest
  </code></pre>
  
  <p>Here are the Functions I wrote for this particular script...</p>
  
  <p>This function will return a list of groups the given user is part of:</p>
  
  <pre><code>function Get-LocalGroupAccess
  {
      [CmdletBinding()]
      Param(
          [Parameter(Mandatory=$true)]
          [string]$fqdn,
  
          [Parameter(Mandatory=$true)]
          [string]$userName
  
      )
      Process
      {
          $serverPath = [ADSI]"WinNT://$fqdn,computer"
          $serverPath.PSBase.Children | where {$_.PSBase.SchemaClassName -eq 'group'} | foreach {
              $lGroup = [ADSI]$_.PSBase.Path
              $lGroup.PSBase.Invoke("Members") | foreach {
                  $lMember = $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null).Replace("WinNT://","")
                  if ($lMember -like "$userName")
                  {
                      $localAccess += @($lGroup.Name)
                  }
              }
          }  
          return($localAccess)
      }
  }
  </code></pre>
  
  <p>This function sets the User Object (I am not sure this is the technical term):</p>
  
  <pre><code>function Set-UserObj($userDomain, $userName)
  {
      $userObj = [ADSI]"WinNT://$userDomain/$userName"
  
      return ($userObj)
  }
  </code></pre>
  
  <p>This function set the FQDN (checks to see if it is pingable):</p>
  
  <pre><code>function Set-FQDN($fqdn)
  {
      do{
          $fqdn = Read-Host "Enter the FQDN"
      } while (-not(Test-Connection $fqdn -quiet))
  
      return($fqdn)
  }
  </code></pre>
  
  <p>This function will take the selection for the group you want to remove the given user from, change it to the proper place in the array, and return the group:</p>
  
  <pre><code>function Set-LocalGroup($localGroups, $selectGroup)
  {
      $selectGroup = Read-Host "What group would you like to add $userDomain/$userName to?"
  
      $selectGroup = [int]$selectGroup -= 1
      $setGroup = $localGroups[$selectGroup]
  
      return($setGroup)
  }
  </code></pre>
  
  <p>This function sets the Group object (not sure if this is the technical term):</p>
  
  <pre><code>function Set-GroupObj($fqdn, $group)
  {
      $groupObj = [ADSI]"WinNT://$fqdn/$group"
  
      return($groupObj)
  }
  </code></pre>
  
  <p>This function removes the given user from the group selected:</p>
  
  <pre><code>function Remove-UserAccess ($gObj, $uObj)
  {
      $gObj.PSBase.Invoke("Remove",$uObj.PSBase.Path)
  }
  </code></pre>
  
  <p>In the script the user name, domain and FQDN are requested. After these are provided the script will return a list of groups the given user is part of. Everything works fine until the user is part of one group. Once that takes place, it throws the error I pasted above.</p>
  
  <p>Please note, this is my first time posting and I am not sure what information is needed here. I hope I provided the proper and correct information. if not, please let me know if there is something else that you require.</p>
  
  <p>Thanks!</p>