简体   繁体   English

如何使用C#在Active Directory用户路径中使用%username%变量?

[英]How to use %username% variable in Active Directory user paths using C#?

I developed an Active Directory user management tool in C# that does some things automatically. 我用C#开发了一个Active Directory用户管理工具,该工具会自动执行一些操作。 This is better for my particular case than the "Active Directory Users and Computers" tool from Microsoft. 对于我的特殊情况,这比Microsoft的“ Active Directory用户和计算机”工具更好。 I have to use roaming profiles and home directories which contain the username of a user. 我必须使用包含用户名的漫游配置文件和主目录。

Now I want to change the username of a user with my program in C#. 现在,我想使用C#程序更改用户的用户名。 Everything works fine, but I want to use the "%username%" variable instead of putting the new username directly into these paths (home directory and roaming profile), because, by using the variable, I ensure that the new username will be placed into these paths if I copy the user object using Microsoft's AD management tool (right click --> "copy"). 一切正常,但是我想使用“%username%”变量,而不是将新的用户名直接放入这些路径(主目录和漫游配置文件)中,因为通过使用变量,我确保将新的用户名放入如果我使用Microsoft的AD管理工具将用户对象复制到这些路径中(右键单击->“复制”)。

If I enter "%username%" when creating or editing a user with Microsoft's AD tool, this variable gets replaced by the username, so it works. 如果在使用Microsoft的AD工具创建或编辑用户时输入“%username%”,则此变量将替换为用户名,因此可以使用。 But if I put this variable into a path using C#, it just puts the string "%username%" at the end of the path (eg "\\fileserver1\\UserHomes\\%username%"). 但是,如果我使用C#将此变量放入路径中,则会将字符串“%username%”放在路径的末尾(例如“ \\ fileserver1 \\ UserHomes \\%username%”)。 It doesn't replace it with the actual username and "stores" that placeholder. 它不会替换为实际的用户名并“存储”该占位符。

How can I use this variable within my C# code properly, so that it get's replaced with the actual username? 如何在C#代码中正确使用此变量,以便将其替换为实际的用户名?

I'm using this code (reduced, it's just an example) to change the users username (SamAccountName) and, for example, the home directory. 我正在使用此代码(减少了,仅是一个示例)来更改用户用户名(SamAccountName)以及例如主目录。 "user" is my UserPrincipal object. “用户”是我的UserPrincipal对象。 Of course, I'm renaming the actual folder after this: 当然,在此之后,我将重命名实际文件夹:

[...]
string newUsername = "NewUsername"; // New username

user.SamAccountName = newUsername;
user.UserPrincipalName = $"{ newUsername }@{ domain }";
user.HomeDirectory = "\\fileserver1\UserHomes\%username%";

user.Save(); // Save changes

The %username% environment variable and the one used AD Users and Computers is not the same. %username%环境变量和一个使用的AD用户和计算机是不同的。 AD Users and Computers replaces it with the actual username value immediately when you click OK. 单击“确定”后,“ AD用户和计算机”立即将其替换为实际的用户名值。 It is not something that AD understands, just a token used by the AD Users and Computers application. AD不能理解它,只是AD用户和计算机应用程序使用的令牌。

Considering you already constructed a string using the username, you shouldn't find it hard to fix this. 考虑到您已经使用用户名构造了一个字符串,因此您不应该很难解决这个问题。

user.HomeDirectory = $"\\fileserver1\UserHomes\{newUsername}";

Also powershell would probably be more appropriate for something like this. 同样,powershell可能更适合于这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM