Add and Sync AD Users with Sharepoint Foundation, getting null errors

I’m trying to import and sync AD users with SharePoint Foundation but keep running into null errors. The results of my script run with psbreakpoint in ISE are immediately below. The script is based on the ones posted at http://community.spiceworks.com/topic/210127-populate-active-directory-user-information-in-sharepoint-foundation, which I ran with similar errors.

What am I doing wrong?

PS C:\Windows\system32>
$session = New-PSSession -ComputerName XXXDC

#Load The Active Directory Module
Invoke-Command -Session $session -ScriptBlock { Import-Module ActiveDirectory }

Retrieve the Get-ADUser cmdlet to your Session

Import-PSSession -Session $session -CommandName Get-ADUser -allowclobber

Add SharePoint.PowerShell Snapin

Add-PSSnapin Microsoft.SharePoint.PowerShell

Retrieve Specific Web –

$spWeb = Get-SPWeb http://XXXsharepoint

$spList = $spWeb.Lists[“Test”]

This command deletes all of the items on your list so that it starts fresh each time# and removed anything from AD that has been removed. You could pull everything into# an array and then compare but I felt it would be faster to clear out the list# and then start from scratch each time.

while($splist.Items.Count -gt 0){$splist.Items.Delete(0)}#

Get All Users and Create a new ListItem foreach user.

Get-ADUser -filter * -Properties SAMAccountName -SearchBase ‘OU=PHS,DC=peninsulahumane,DC=com’ |
Where-Object {$_.CN -ne $null} |

set-psbreakpoint -Variable item -mode read
ForEach-Object {
$item = $spList.AddItem()
$item[‘Name’]=$_.CN
$item.Update()
}
exit-pssession

ModuleType Name ExportedCommands


Script tmp_up534i3o.sur Get-ADUser
You cannot call a method on a null-valued expression.
At line:28 char:1

  • $item = $spList.AddItem()
  •   + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull
    
    

Cannot index into a null array.
At line:29 char:1

  • $item[‘Name’]=$_.CN
  •   + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : NullArray
    
    

You cannot call a method on a null-valued expression.
At line:30 char:1

  • $item.Update()
  •   + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull
    
    
    
    
    
    
    
    
    

=

According to the errors, it looks like $splist is null. I’d try interactively running the get-spweb part and seeing what you get back from $spweb.Lists.