views:

342

answers:

1

I am trying to remove a specific user from sharepoint and have hit a wall.

I keep getting the following error

You cannot call a method on a null-valued expression.

Code:

function verifyUsers
{
    $verify_sitepath="https://extranet.mydomain.com"
    $verify_site=new-object Microsoft.SharePoint.SPSite($verify_sitepath)
    $verify_web=$verify_site.Rootweb
    $verify_web.site.url
    $verify_groups = $verify_web.groups | ? {$_.Name -match "^.*$CurrentGroup" }
    foreach($verify_group in $verify_groups)
    {
     #Write-Output "+ $($verify_group.Name)"
     foreach($verify_user in $verify_group.users)
     {
      $verify_user = $verify_user -replace "MYDOM\\",""
      Write-Output "$verify_user" | Out-File -filepath "$splist$currentGroup.txt" -append
     }
    }
    $strReference = get-Content "C:\Powershell\Users from SharePoint\$currentgroup.txt"
    $strDifference = get-Content "C:\Powershell\Users in groups\$currentgroup.txt"
    Compare-Object $strReference $strDifference

    Compare-Object $strReference $strDifference | `
    Where-Object { $_.SideIndicator -eq "<=" } | `
    ForEach-Object {
     if ($_.InputObject -eq "testuser ")
     {
      Write-Host "testuser HAHAHAHAHA"
      $TheNewGroup = $OpenWeb.SiteGroups | Where-Object {($_.Name -match $currentGroup)}
      "group Name: $TheNewGroup.Name"
      #$TheNewGroup.deleteuser("MYDOM\" + $_.InputObject)
      #$web.Update()

      $TheNewGroup.SiteUsers.Remove("MYDOM\testuser ")
     }
     Write-Host $_.InputObject
     "Deleting user: {0} from $currentgroup" -f $_.InputObject
    }
}
+1  A: 

Found the anwser (Sorry about my messy code above)

$theuser = $verify_web.AllUsers.Item("MYDOM\testuser")
$TheNewGroup.RemoveUser($theuser)
Mitchell Skurnik