使用 PowerShell 列出所有用户配置文件属性 - PowerShell 查询

使用 PowerShell 列出所有用户配置文件属性 - 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 文件,

到目前为止,我只能获得特定属性(请参阅下面的答案)。我尝试将输出通过管道传输到|ft| select但这只会返回空白。

我感觉我已经很接近了。我不想$_.GetEnumerator()用很多$_.Item("property")电话来代替通话,而且我觉得我没有必要这么做。有什么想法吗?

答案1

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

# 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 文件中。现在这样就行了。

相关内容