Tuesday, October 18, 2011

Developing a PowerShell Script for SharePoint 2007

Since I started working with SharePoint infrastructure and administration, I've been thinking on a solution to automatize some daily tasks that ever since I was able only to perform either via Central Administration or STSADM command.

So today, I started studying how to work with PowerShell scripting for SharePoint, and it seems to be pretty easy and cool :)

The first thing you must do so PowerShell can access SharePoint assemblies is to load them using below code line:

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

Once you have the assemblies loaded, you can invoke all methods of SP object model, just like if you were developing with any .NET programming language. Below example shows how to get both the primary and secondary Site Collection Administrators from a given URL:

$site = New-Object Microsoft.SharePoint.SPSite("http://mysite")
$priSCALogin = $site.Owner.LoginName
$secSCALogin = $site.SecondaryContact.LoginName

write "-Primary SCA: $priSCALogin"
write "-Secondary SCA: $secSCALogin"

I'll keep working and studying about this and obviously, posting.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...