使用 Powershell 脚本访问 Sharepoint 用户配置文件属性 [英] Accessing Sharepoint UserProfile Properties with Powershell script

查看:75
本文介绍了使用 Powershell 脚本访问 Sharepoint 用户配置文件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The following script spits out all UserProfile properties for users on Sharepoint 2007:

[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 Username, WorkEmail, Workphone|选择 Username、WorkEmail、WorkPhone 但这只会返回空白.

However, what I want to do is be able to return a table, or csv file of specific values in the profile, e.g. Username, WorkEmail, WorkPhone. I have tried piping the output to |ft Username, WorkEmail, Workphone and | select Username, WorkEmail, WorkPhone but this just returns blanks.

我觉得我很亲近.我不想用大量的 $_.Item("property") 调用替换 $_.GetEnumerator() 调用,而且感觉不像我应该有.有什么想法吗?

I feel like I am so close. I don't want to replace the $_.GetEnumerator() call with lots of $_.Item("property") calls and it doesn't feel like I should have to. Any ideas?

推荐答案

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

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.

这篇关于使用 Powershell 脚本访问 Sharepoint 用户配置文件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆