2

我正在用 C# 编写一个程序,将图片添加到 Active Directory 用户对象(在 ThumbnailPhoto 属性中)。该程序在 Exchange Server 2010 之外的另一台 PC 上运行。例如,我可以使用 PowerShell 远程处理为 AD 用户启用邮箱。

当我在 PowerShell 中执行以下命令时(在 Exchange Server 以外的另一台 PC 上),它运行良好:

enter-pssession exchange_server_fqdn
$cred = get-credential          ( domain\administrator , **** )
$sess = new-pssession -configurationName Microsoft.Exchange -ConnectionUri http://exchange_server_fqdn/PowerShell/ -Authentication Kerberos -Credential $cred
import-pssession $sess
Import-RecipientDataProperty -Identity ADusername -Picture -FileData ([Byte[]]$(Get-Content -Path 'C:\\person.jpg' -Encoding Byte -ReadCount 0))
remove-pssession $sess

但我需要从 C# 执行命令。以下代码运行(不引发异常),但图像未添加到 AD 用户对象:

string username = "xxx";
string password = "yyy";
string exchangeServer = "zzz"; // FQDN of Exchange Server 2010
string liveIdconnectionUri = "http://" + exchangeServer + "/Powershell?serializationLevel=Full";

// credentials
SecureString passwordSecureString = new SecureString();
foreach (char c in password) passwordSecureString.AppendChar(c);
PSCredential credential = new PSCredential(username, passwordSecureString);

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
    new Uri(liveIdconnectionUri),
    "http://schemas.microsoft.com/powershell/Microsoft.Exchange",
    credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;

PowerShell powershell = PowerShell.Create();
Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
runspace.Open();
powershell.Runspace = runspace;
PSCommand command = new PSCommand();

// Add ThumbnailPhoto attribute to Active Directory user
// This command works when executed on the Exchange Server 2010 in the Exchange Management Shell,
// But it doens't work when executed in C#.
command.AddScript("Import-RecipientDataProperty -Identity ADusername -Picture -FileData ([Byte[]]$(Get-Content -Path 'C:\\person.jpg' -Encoding Byte -ReadCount 0))");

powershell.Commands = command;
try {
    Collection<PSObject> commandResults = powershell.Invoke<PSObject>();
    foreach (PSObject result in commandResults) Console.WriteLine(result.ToString());
}
catch (Exception ex) {
    Console.WriteLine(ex.Message);
}

代码中唯一的问题是带有command.AddScript. 例如,如果我将该行替换为:

command.AddCommand("Get-Mailbox");
command.AddParameter("Identity", "ADusername");

...然后它确实有效。启用邮箱也可以。

如何从 C# 执行命令以将图像添加到 Active Directory 用户对象(使用 PowerShell 远程处理)?

基于公认答案的工作代码:

DirectoryEntry container = new DirectoryEntry(LDAP_URI + USERS_DIR);
DirectoryEntry user = container.Children.Add("cn=" + username, "user");

// Set other property's of the user object:
//// user.Properties ["xxx"].Value = "yyy";
//// ...

byte [] buffer;
FileStream fileStream = new FileStream(@"c:\photo.jpg", FileMode.Open, FileAccess.Read);

try {
    int length = (int) fileStream.Length;  // get file length
    buffer = new byte [length];            // create buffer
    int count;                             // actual number of bytes read
    int sum = 0;                           // total number of bytes read

    // read until Read method returns 0 (end of the stream has been reached)
    while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
        sum += count;  // sum is a buffer offset for next reading
}

finally {
    fileStream.Close();
}

user.Properties ["thumbnailPhoto"].Value = buffer;

user.CommitChanges();
4

2 回答 2

0

这听起来像是一个身份验证问题。即使您在 Exchange 服务器上运行该命令,它实际上是针对 Active Directory 执行的,并且无法保证它将针对哪个域控制器执行。这是双跳场景,可以通过在 WS-Man 远程会话中使用 CredSSP 来解决。

于 2012-05-24T13:21:29.427 回答
0

我会采取不同的方法:

byte[] pictureBytes = //Use a FileStream to read the data

command.AddCommand("Import-RecipientPropertyData");
command.AddParameter("Identity", "fooUser");
command.AddParameter("Picture", "$true");
command.AddParameter("Encoding", "Byte");
command.AddParameter("ReadCount", 0);
command.AddParameter(FileData, pictureBytes);

或者,您可以将图片读入您的图片字节并使用 ADSI ( DirectoryEntry) 直接写入thumbnailPhoto属性。只要确保它pictureBytes<= 10KB,因为这是 AD 中属性的最大值。

于 2012-05-26T15:56:31.800 回答