Wednesday, November 01, 2006
« SharePoint/PowerShell 2: Add some Users ... | Main | SharePoint/PowerShell 4: Adding the Port... »

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.

 

 

 

 

 

Wednesday, November 01, 2006 8:05:34 PM (GMT Standard Time, UTC+00:00)  #    Disclaimer  |  Comments [0]  |  Related posts:
PowerShell quickie: Extract the feature IDs used in large SharePoint projects
UK Community Day at Microsoft Reading
PowerShell: Generating a proxy for all the SharePoint WebServices
Silverlight 2: Unable to call SharePoint's Web Services
Presenting SharePoint + Silverlight at the SharePoint UK UserGroup Basingstoke March 27th
Sharepoint and Powershell Virtual lab
Tracked by:
http://www.google.com/search?q=hkrsixri [Pingback]
http://urdustan.com/catalog/images/sys/4/health.htm [Pingback]
http://pickeringpublishing.com/Bringitonhome/archives/2/buysoma.htm [Pingback]
http://urdustan.com/catalog/images/sys/4/order-cialis.htm [Pingback]
http://urdustan.com/catalog/images/sys/3/valium.htm [Pingback]
http://programmazioneneurolinguistica.com/feeds/1/vitamin.htm [Pingback]
http://pickeringpublishing.com/Bringitonhome/archives/1/safeviagrawoman.htm [Pingback]
http://buildguide.net/forum/templates/subRed/3/weightwatchers.htm [Pingback]
http://buildguide.net/forum/templates/subRed/3/buycialisonline.htm [Pingback]
http://e-rat.org/aaarg/images/misc/1/levitra.htm [Pingback]
http://e-rat.org/aaarg/images/misc/3/buy-soma.htm [Pingback]
http://pickeringpublishing.com/Bringitonhome/archives/2/weight-loss.htm [Pingback]
http://pickeringpublishing.com/Bringitonhome/archives/2/diet.htm [Pingback]
http://urdustan.com/catalog/images/sys/4/buy-hydrocodone.htm [Pingback]
http://bluehoney.org/bluehoney/images/base/3/online-order-phentermine.htm [Pingback]
http://powerlance.com/template/misc/4/buyvicodin.htm [Pingback]
http://chatcake.com/tmp/2/atkins-diet.htm [Pingback]
http://e-rat.org/aaarg/images/misc/1/buy-viagra-online.htm [Pingback]
http://pickeringpublishing.com/Bringitonhome/archives/3/viagra-online.htm [Pingback]
http://chatcake.com/tmp/1/zyrtec.htm [Pingback]
http://arkarpa.org/joomla/images/misc/2/cialisonline.htm [Pingback]
http://chatcake.com/tmp/4/meridiaweightloss.htm [Pingback]
http://programmazioneneurolinguistica.com/feeds/3/phentermine-adipex.htm [Pingback]
http://urdustan.com/catalog/images/sys/3/buyphentermine.htm [Pingback]
http://kitaabghar.com/dir/javascript/3/best-diet-pills.htm [Pingback]
http://shop.trovata.com/images/misc/1/zyrtec.htm [Pingback]
http://localboard.on.ca/GuestBook/public/b/buy-online-soma.htm [Pingback]
http://getindyknow.com/components/com_mymenu/2/zyrtec.htm [Pingback]
http://inlay.com/phpBB/cache/misc/b/buy-online-soma.htm [Pingback]
http://www.eea-esem2006.org/fileadmin/d/buycialisonline.htm [Pingback]
http://shining.com/store/ph/b/xanax.htm [Pingback]
http://www.alpenhof.it/fileadmin/inc/c/cheap-meridia.htm [Pingback]
http://helpthemknow.com/phplist/attachments/c/propecia-online.htm [Pingback]
http://shining.com/store/ph/a/tramadolonline.htm [Pingback]
http://www.eufos-vienna2007.org/fileadmin/template/css/a/healthy-diet.htm [Pingback]
http://www.eea-esem2006.org/fileadmin/a/tramadol.htm [Pingback]
http://inlay.com/phpBB/cache/misc/a/buy-propecia.htm [Pingback]
http://www.eea-esem2006.org/fileadmin/b/tramadolonline.htm [Pingback]
http://localboard.on.ca/GuestBook/public/a/buy-propecia.htm [Pingback]
http://shining.com/store/ph/c/cheapviagra.htm [Pingback]
http://www.eufos-vienna2007.org/fileadmin/template/css/d/diet-hoodia.htm [Pingback]
http://helpthemknow.com/phplist/attachments/b/tramadol-ultram.htm [Pingback]
http://localboard.on.ca/GuestBook/public/d/buy-xanax-on-line.htm [Pingback]
http://www.eea-esem2006.org/fileadmin/b/ultram.htm [Pingback]
http://getindyknow.com/components/com_mymenu/3/effexor.htm [Pingback]
http://www.google.com/search?q=qnxzkllu [Pingback]
http://www.google.com/search?q=jtebpsnu [Pingback]
http://www.google.com/search?q=pxiamndx [Pingback]
http://www.google.com/search?q=fpkkdxug [Pingback]
http://www.google.com/search?q=rhshaeah [Pingback]
http://www.bourgestourisme.com/club-hotelier-bourges/images/hotel/c/buy-ultram.h... [Pingback]
http://www.idweb.fr/altajuris/images/para/c/tramadol-discount.htm [Pingback]
http://www.ucboxingclub.org/cgi-bin/board.pl?read=6082 [Pingback]
http://tzs.net/photos/g2data/a/levitra.htm [Pingback]
http://bravemenrun.com/phpu/code/a/soma.htm [Pingback]
http://bravemenrun.com/phpu/code/d/buy-phentermine.htm [Pingback]
http://trading-report.com/stocks/b/alprazolam.htm [Pingback]
http://thepaintedbody.com/mambo/images/a/buy-viagra.htm [Pingback]
http://bravemenrun.com/phpu/code/c/valium.htm [Pingback]
http://kinobg.com/temp/d/somacheap.htm [Pingback]
http://bravemenrun.com/phpu/code/a/carisoprodol.htm [Pingback]
http://tinybum.com/stcbb/images/avatars/c/saleviagra.htm [Pingback]
http://revolution3d.net/scs/base/c/nexium2.htm [Pingback]
http://hterry.com/photogallery/include/a/alprazolam.htm [Pingback]
http://hterry.com/photogallery/include/a/vitamin.htm [Pingback]
http://pspro-magazine.de/wotlab/acp/lib/c/viagrapill.htm [Pingback]
http://revolution3d.net/scs/base/c/weightwatchers.htm [Pingback]
http://revolution3d.net/scs/base/d/cialis-online.htm [Pingback]
http://payperclickguru.com/blog/wp-content/d/cialisonline.htm [Pingback]
http://hterry.com/photogallery/include/a/fitness.htm [Pingback]
http://mactalla.com/inc/d/effexor.htm [Pingback]
http://payperclickguru.com/blog/wp-content/b/carisoprodolonline.htm [Pingback]
http://evolans.com/woot/base/d/xanax.htm [Pingback]
http://revolution3d.net/scs/base/b/tramadolonline.htm [Pingback]
http://extensionsmirror.nl/cache/lang_cache/c/fitness-equipment.htm [Pingback]
http://extensionsmirror.nl/cache/lang_cache/d/healthcare.htm [Pingback]
http://extensionsmirror.nl/cache/lang_cache/a/soma.htm [Pingback]
http://extensionsmirror.nl/cache/lang_cache/d/zyrtec.htm [Pingback]
http://dragon.go2england.org/inc/d/celexapaxilvs.htm [Pingback]
http://www.gaslamp.org/backoffice/a/ultram.htm [Pingback]
http://www.tiergeschichten.de/images/b/cheaptramadol.htm [Pingback]
http://www.gaslamp.org/backoffice/c/fioricet.htm [Pingback]
http://mobikefed.org/2002/b/diet-pills.htm [Pingback]
http://www.tiergeschichten.de/images/d/phentermine-online.htm [Pingback]
http://mobikefed.org/2002/d/viagraonline.htm [Pingback]
http://www.gaslamp.org/backoffice/c/propecia.htm [Pingback]
http://mobikefed.org/2002/c/buy-viagra-online.htm [Pingback]
http://www.tiergeschichten.de/images/e/bt.htm [Pingback]
http://www.gaslamp.org/backoffice/e/bvo.htm [Pingback]
http://www.focuspocus.org/photopages/e/bv.htm [Pingback]
http://www.gaslamp.org/backoffice/e/bs.htm [Pingback]
http://aleembawany.com/wp-content/base/3/generic-cialis.php [Pingback]
http://chatcake.com/rq/4/buy-xenical.php [Pingback]
http://marchalesbica.org.mx/pruebag/backend/3/order-viagra.php [Pingback]
http://www.verbum.nl/lib/cache/1/ultram.php [Pingback]
http://www.clairedanes.com/nb/gallery/g2data/inc/3/phentermine-diet-pills.php [Pingback]
http://www.verbum.nl/lib/cache/4/buy-soma.php [Pingback]
http://www.verbum.nl/lib/cache/3/meridia.php [Pingback]
http://www.ifhhro.org/UserFiles/Flash/2/nexium.php [Pingback]
http://www.ifhhro.org/UserFiles/Flash/4/buy-cialis-online.php [Pingback]
http://www.globalyouthonline.nl/art/1/pharmacy.php [Pingback]
http://www.roleplayingtips.com/j4/chat/3/viagraonline.php [Pingback]
http://www.ifhhro.org/UserFiles/Flash/1/diet.php [Pingback]
http://www.roleplayingtips.com/j4/chat/3/xenical.php [Pingback]
http://www.globalyouthonline.nl/art/4/buy-cialis.php [Pingback]
http://www.roleplayingtips.com/j4/chat/1/ultram.php [Pingback]
http://www.roleplayingtips.com/j4/chat/2/meridia.php [Pingback]
http://www.drzwi.pl/files/pages_gallery/mini/inc/2/phentermine-online.php [Pingback]
http://www.drzwi.pl/files/pages_gallery/mini/inc/1/xenical.php [Pingback]
http://www.drzwi.pl/files/pages_gallery/mini/inc/3/soma-online.php [Pingback]
http://www.mercantilelibrary.com/email-manager/base/4/diabetes.php [Pingback]
http://www.bixbylibrary.org/cms/images/misc/4/health.php [Pingback]
http://www.mercantilelibrary.com/email-manager/base/3/order-tramadol.php [Pingback]
http://www.mercantilelibrary.com/email-manager/base/3/ativan.php [Pingback]
http://www.distefanowinery.com/images/cat_icons/1/health.php [Pingback]
http://www.mercantilelibrary.com/email-manager/base/4/viagra-pill.php [Pingback]
http://www.distefanowinery.com/images/cat_icons/4/online-pharmacy.php [Pingback]