Ok now we have the users in AD we can add them to the portal. Again we can use import-csv to add them from the users CSV file
heres the routine
# Function: Add-UsersToSP
# Description: Add each user in the import CSV file to the given role
# Parameters: SiteCollectionURL URL for the root of the Site Collection
# UserFile Location of the CSV file containing the users
# Domain Users domain
# Role Name of the SharePoint Role e.g Reader, Contribute
#
function Add-UsersToSP([string]$SiteCollectionURL,[string]$UserFile, [string]$Domain, [string]$Role)
{
Import-Csv $UserFile | foreach-object {$spsite=new-object Microsoft.SharePoint.SPSite($SiteCollectionURL) } {$spsite.RootWeb.Roles[$Role].AddUser($Domain + "\" + $_.LoginName, $_.Email, $_.DisplayName, "") }
}
In this script we get a SPSite object, get its rootweb and index into the roles collection to return the role we want. We then use the AddUser method to add the user.
And we call it like this Add-UsersToSP "http://sps:2828" "users.csv" "contoso" "contribute"
Now there is something a little different going on in this script. In the foreach-object loop there are 2 scriptblocks (the curly braces). This is because you can have up to 3 scriptblocks in the foreach clause: Begin, Process and End
The Begin scriptblock will be called once at the start of the iteration, Process is called for each object in the collection and End will be called at yes the end of the iteration allowing you to clean up your objects. I don't have an end in this case but you could call dispose on the SPSite object.
In v3 the Roles collection is deprecated and you should start using the new SPRoleDefinition and SPRoleAssignment classes. These new classes have full support for the new security features but for now I'm keeping things as simple as I can.
Now is a good time to point out how easy it is to explore the SharePoint OM interactively, in this case the Roles collection.
First of all from the command line do this
PS C:\demo> $spsite=new-object microsoft.sharepoint.spsite("http://sps:2828")
PS C:\demo> $spsite.rootweb.roles
this produces a listing like this
Name : Full Control
Users : {}
Groups : {SUGUK Intranet Owners}
Type : Administrator
Description : Has full control.
Xml : <Role ID="1073741829" Name="Full Control" Description="Has ful
l control." Type="5" />
ID : 1073741829
PermissionMask : FullMask
ParentWeb : SUGUK Intranet
Name : Design
Users : {}
Groups : {Designers}
Type : WebDesigner
Description : Can view, add, update, delete, approve, and customize.
Xml : <Role ID="1073741828" Name="Design" Description="Can view, add
, update, delete, approve, and customize." Type="4" />
ID : 1073741828
PermissionMask : 1012866047
ParentWeb : SUGUK Intranet
.....
You can use the select CmdLet to produce a simple table listing
PS C:\demo> $spsite.rootweb.roles | select name, users
Name Users
---- -----
Full Control {}
Design {}
Manage Hierarchy {}
Approve {}
Contribute {Brian Ballack, Walter French}
Read {}
Restricted Read {}
Limited Access {NT AUTHORITY\authenticated users, S...
View Only {}
If we want to see the full object of say the RootWeb you can do this
PS C:\demo> $spsite.rootweb | get-member
or just its properties
PS C:\demo> $spsite.rootweb | get-member -membertype property
or just its methods
PS C:\demo> $spsite.rootweb | get-member -membertype methods
I find myself using this lot before coding C# against an object as I can try out the API and mess around with it before running up VS2005
The next step will be to create a host of portal areas as defined in a CSV file.