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 Populate Active Directory user information in Sharepoint Foundation - Software & Applications - Spiceworks Community, 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
=