Add SSO apps with Powershell MOSS 2007 aka strange powershell syntax
The setting
At the moment I am working for a client that has quite a lot of applications that require SSO in SharePoint 2007 to function in their Intranet. If you need to know what SSO in SharePoint 2007 is, please follow this link. In a big enterprise it is important to create reusable stuff for development, test, acceptance and production environments. This is where my friend Powershell comes in. Powershell scripting provides the opportunity to reuse script and settings in those environments in a repeatable way. So I decided to go and script to create the desired SSO applications.
I assume some familiarity with Powershell and the SharePoint Object Model here. The namespace Microsoft.SharePoint.Portal.SingleSignon contains the desired properties and methods to create the SSO applications in SharePoint 2007. So first thing we do is create a reference:
[System.Reflection.Assembly]::LoadWithPartialName
("Microsoft.SharePoint.Portal.SingleSignon")
So now we have a reference and I thought everything would be straightforward. I looked up the SDK and figured I needed to create an array of ApplicationField and create an ApplicationInfo object. Feed that to the AddApplication method and hey presto all is done. Oh no….no.no.no. So I went to work in PowerGui, which has Intellisense. So I get to the ApplicationField by typing:
$field1 = new-object Microsoft.SharePoint.Portal.SingleSignon.Application.ApplicationField("foo", $true)
All I got was this error saying that ApplicationField cannot be found. Hmmm it is there according Visual Studio and the SDK. So i tried just by typing in the PowerShell console:
$field1 = new-object Microsoft.SharePoint.Portal.SingleSignon.Application
$fied1 | Get-Member
The problem
Hmm ApplicationField appears to be missing. It is a type in the type Application type. Googled a bit around, talked a bit with my coworkers. Fired up Reflector. Using Reflector I noticed the plus sign in the namespace.
Turns out it is a nested type and as PowerShell uses reflection in the reference you must also create the object on a nested type with a plus sign. So the correct code is:
$field1 = new-object Microsoft.SharePoint.Portal.SingleSignon.Application+ApplicationField("foo", $true)
The solution
Hey presto it worked! In the namespace there are quite some members that are nested types. The finished script looks like this:
(I’ve added the code here as the long code lines will not properly wrap. If you know a fix please let me know.)
code_sso_powershell
# Load a bunch of assemblies that are needed for creating the sso apps
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Portal.SingleSignon")
Write-Host "Creating SSO application" -ForegroundColor Green
[xml]$config = Get-Content settings.xml
# create an array of appfields
# note the plus syntax in the constructor
$field1 = new-object Microsoft.SharePoint.Portal.SingleSignon.Application+ApplicationField($config.xml.appinfo.appfield1.value, [System.Convert]::ToBoolean($config.xml.appinfo.appfield1.mask))
$field2 = new-object Microsoft.SharePoint.Portal.SingleSignon.Application+ApplicationField($config.xml.appinfo.appfield2.value, [System.Convert]::ToBoolean($config.xml.appinfo.appfield2.mask))
$field3 = new-object Microsoft.SharePoint.Portal.SingleSignon.Application+ApplicationField($config.xml.appinfo.appfield3.value, [System.Convert]::ToBoolean($config.xml.appinfo.appfield3.mask))
$field4 = new-object Microsoft.SharePoint.Portal.SingleSignon.Application+ApplicationField($config.xml.appinfo.appfield4.value, [System.Convert]::ToBoolean($config.xml.appinfo.appfield4.mask))
$field5 = new-object Microsoft.SharePoint.Portal.SingleSignon.Application+ApplicationField($config.xml.appinfo.appfield5.value, [System.Convert]::ToBoolean($config.xml.appinfo.appfield5.mask))
[Microsoft.SharePoint.Portal.SingleSignon.Application+ApplicationField[]]$appfields = ($field1, $field2, $field3, $field4, $field5)
# create the application info data
# set the type of the application
switch ($config.xml.appinfo.grouptype) {
"Individual" {$appType = [Microsoft.SharePoint.Portal.SingleSignon.Application+ApplicationType]::Individual}
"Group" {$appType = [Microsoft.SharePoint.Portal.SingleSignon.Application+ApplicationType]::Group}
default {Write-Error "Could not determine type of the application"}
}
# to overwrite or not
switch ($config.xml.appinfo.disposition) {
"overwrite" {$appCreationDisposition = [Microsoft.SharePoint.Portal.SingleSignon.Application+ApplicationCreationDisposition]::Overwrite}
"new" {$appCreationDisposition = [Microsoft.SharePoint.Portal.SingleSignon.Application+ApplicationCreationDisposition]::CreateNew}
default {Write-Error "Could not determine overwrite or create new sso application"}
}
$appinfo = New-Object Microsoft.SharePoint.Portal.SingleSignon.Application+ApplicationInfo($config.xml.appinfo.name, $config.xml.appinfo.name,$appType, $config.xml.appinfo.email)
[Microsoft.SharePoint.Portal.SingleSignon.Application]::AddApplication($appinfo, $appfields, $appCreationDisposition)
Note that I use an XML file to supply the settings. The xml file looks as follows:
<xml>
<appinfo grouptype="individual" disposition="overwrite" name="myid2" displayname="myid2" email="roelhans@tsunami.nl">
<appfield1 value="test1" mask="false"></appfield1>
<appfield2 value="test2" mask="false"></appfield2>
<appfield3 value="test3" mask="false"></appfield3>
<appfield4 value="test4" mask="false"></appfield4>
<appfield5 value="test5" mask="false"></appfield5>
</appinfo>
</xml>
Of course it is pretty easy to add multiple settings in the xml file and have powershell read each appinfo node with a foreach-object loop. I’ll leave that one to you the reader however.
Conclusion
I hope you find this post insightful on how to set up SSO applications in SharePoint 2007. I suppose that this will also work in SharePoint 2010, I have not tested it however and there might be cmdlet out there that does this hard work for you. I also hope it has saved you some time on the strange syntax in PowerShell for nested types.
For now: happy scripting!

Recent Comments