使用Powershell脚本访问Sharepoint UserProfile属性

时间:2010-07-01 10:45:16

标签: sharepoint powershell

以下脚本为Sharepoint 2007上的用户分配所有UserProfile属性:

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")
# Function:          Get-UserProfiles
# Description:       return a UserProfileManager object containing all user profiles
# Parameters:        SSPName          SSPName    
#
Function global:Get-UserProfiles($SSPName)
{
    $ServerContext = [Microsoft.Office.Server.ServerContext]::GetContext($SSPName);
    $UPManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServerContext);
    return $UPManager.GetEnumerator();
}

$profiles = Get-UserProfiles("SharedServices");
$profiles | ForEach-Object { $_.GetEnumerator();}

但是,我想要做的是能够在配置文件中返回特定值的表或csv文件,例如用户名,WorkEmail,WorkPhone。我已尝试将输出汇总到|ft Username, WorkEmail, Workphone| select Username, WorkEmail, WorkPhone,但这只会返回空白。

我觉得自己如此接近。我不想用$_.GetEnumerator()次呼叫替换$_.Item("property")来电,而且我觉得我不应该这样做。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

我进一步开发了代码,以便它现在接受以逗号分隔的属性列表,并将它们写入分隔文件。

# Outputs a delimited file with specified user profile properties for each user in Sharepoint

# Create array of desired properties
$arProperties = 'UserName','FirstName','LastName','Title','WorkEmail','WorkPhone','Manager','AlternateContact','RoleDescription','PictureURL';
# Specify output file
$outfile = 'UserProfiles.csv';
#Specify delimiter character (i.e. not one that might appear in your user profile data)
$delim = '^';
# Specify Shared Service Provider that contains the user profiles.
$SSP = "SharedServices";

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")

# Function:          Get-UserProfiles
# Description:       return a UserProfileManager object containing all user profiles
# Parameters:        SSPName          SSPName    
#
Function global:Get-UserProfiles($SSPName)
{
 $ServerContext = [Microsoft.Office.Server.ServerContext]::GetContext($SSPName);
 $UPManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServerContext);
 return $UPManager.GetEnumerator();
}
$profiles = Get-UserProfiles($SSP);

#Initialise Output file with headings
$header = [string]::join($delim,$arProperties);
Write-Output $header | Out-File $outfile

#Output the specified properties for each
$profiles | ForEach-Object {
 foreach($p in $arProperties){
  # Get the property name and add it to a new array, which will be used to construct the result string
  $arProfileProps += $_.Item($p);
 }  
 $results = [string]::join($delim,$arProfileProps);
 # Get rid of any newlines that may be in there.
 $CleanResults = $results.Replace("`n",'');
 Write-Output $CleanResults
 Remove-Variable -Name arProfileProps
} | Out-File -Append $outfile

这让我更接近了。我仍然非常喜欢一个遍历所有配置文件属性的脚本,并将它们更优雅地放入CSV或XML文件中。这将暂时做到。