#* FileName: PowerShellTemplate.ps1
#*=============================================================================
#* Created: [11 December 2007]
#* Author: Donabel Santos
#* Reqrmnts:
#* Keywords:
#*=============================================================================
#* Purpose:
#*
#* This Powershell script queries ActiveDirectory for OU-specific
#* users and inserts those users as new records in a Sharepoint
#* out-of-the-box Contact List
#*=============================================================================
#*=============================================================================
#* SCRIPT BODY
#*=============================================================================
#Powershell Script that queries a specific OU in AD,
#and populates the Contact List
#based on the members that are queried
#IMPORTANT NOTE:
#This needs to be run on the MOSS Server because the
#SP Object Model cannot be invoked remotely
#Step 1: Install Powershell RC2
#http://support.microsoft.com/kb/925228
#Step 2: Set Execution Policy to RemoteSigned (uncomment below)
#Set-ExecutionPolicy RemoteSigned
#Step 3: Set the following variables
$siteUrl = "http://moss/sites/test"
$ou = "LDAP://OU=My Group,OU=Guest Users,DC=domain,DC=ca"
#Step 4: Run this script on Powershell on the Sharepoint Server
#Rest of code follows
#Load Sharepoint DLL
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
#we want to query just the top web of the "test" site collection
$webName = ""
#create a site object
$spSite = new-object Microsoft.SharePoint.SPSite($siteurl)
#the following just displays URL, ID, Name and Users in a table format
$spSite.AllWebs | format-table Url, ID, Name, AllUsers
#open web
$spWeb = $spSite.OpenWeb($webName)
#we want to get a handle on contacts
$listName = "Contacts"
#get a handle to the Contacts list
$spList = $spWeb.lists[$listName]
#Create an AD DirectorySearcher object
$searcher = new-object DirectoryServices.DirectorySearcher([ADSI]"$ou")
#sample filter below, if you need to filter further
#$searcher.filter = "(&(objectClass=user)(givenName=Belle))"
#find all that matches
$groups = $searcher.findall()
#the following for loop just displays
@(foreach($group in $groups)
{
[string]$firstname = $group.properties.givenname
[string]$lastname = $group.properties.sn
if (($firstname.length -gt 0) -and ($lastname.length -gt 0))
{
"Given Name:{0} {1}" -f $firstname, $firstname.length
}
}
)
#this code block inserts the AD user as a new user
#into the Sharepoint Contact list
@(foreach($group in $groups)
{
#extract all properties we need first
#add fields here if necessary
[string]$name = $group.properties.name
[string]$firstname = $group.properties.givenname
[string]$lastname = $group.properties.sn
[string]$company = $group.properties.company
[string]$email = $group.properties.mail
[string]$objectclass = $group.properties.objectclass
[string]$distinguishedname = $group.properties.distinguishedname
#now add this to the sharepoint list only if first name
#and last name are not empty
#add fields here if necessary
if (($firstname.length -gt 0) -and ($lastname.length -gt 0))
{
$spitem = $spList.Items.Add()
$spitem["Last Name"] = $lastname
$spitem["First Name"] = $firstname
$spitem["Company"] = $company
$spitem["E-mail Address"] = $email
$spitem.Update()
}
}
)
#voila! we're done!
#*=============================================================================
#* END OF SCRIPT:
#*=============================================================================