且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Powershell 脚本访问 Sharepoint 用户配置文件属性

更新时间:2022-12-07 08:07:01

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

I've further developed the code so that it now accepts a comma delimited list of properties and writes them to a delimited file.

# 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 文件中.现在就可以了.

This gets me a bit closer. I'd still really like a script that iterates through all the profile properties and puts them into a CSV or XML file more gracefully. This will do for now.