# encoding: ascii
# api: powershell
# title: Get MOSS User Profile
# description: Quick functions to get either 
# version: 0.1
# type: function
# author: Peter
# license: CC0
# function: Get-UserProfile
# x-poshcode-id: 1305
# x-derived-from-id: 1306
# x-archived: 2012-02-07T08:16:38
# x-published: 2012-09-03T15:04:00
#
# a) the raw UserProfile object, which you may use to quickly inspect or modify;
# b) a more user-friendly display of the object data. Because there are no bulk methods to grab user profile data, this may be used to effectively extract your user profile data.
# Side note: beware, enumerating over the UserProfileManager object is SLOW.
# Assumption: haven’t tested against multi-value properties, so I’m not sure how they look.
#
function Get-UserProfile($accountName)
{
	[reflection.assembly]::LoadWithPartialName("Microsoft.SharePoint") | out-null
	[reflection.assembly]::LoadWithPartialName("Microsoft.Office.Server") | out-null
	
	$upm =[Microsoft.Office.Server.UserProfiles.UserProfileManager](
		[Microsoft.Office.Server.ServerContext]::Default)
		
	$upm.GetUserProfile($accountName)
}

function Get-UserProfileData($profile)
{
	$propsToDisplay = $upm.Properties | ? { $_.IsSystem -eq $false -and $_.IsSection -eq $false }
	
	$o = new-object PSObject
	$propsToDisplay | foreach {
		add-member -inp $o -membertype NoteProperty -name $_.Name -value $profile[$_.Name].Value
	}
	
	$o
}




#USAGE 1: (update one profile)
# $up = Get-UserProfile "spadmin"
# $up["YourCustomField"].Value = "new value"
# $up.Commit()
#
#USAGE 2: (export profile data to CSV)
#
# $upm = [Microsoft.Office.Server.UserProfiles.UserProfileManager](
#      [Microsoft.Office.Server.ServerContext]::Default)



#

# $unrolledProfilesCollection = $upm | foreach { $_ }
# $unrolledProfilesCollection | foreach { Get-UserProfileData $_ } | export-csv -NoTypeInformation "C:\temp\profiles.txt"
