分配登录脚本以本地用户脚本、分配、用户

2023-09-06 14:17:09 作者:孤傲何妨

我写了一个C#code,创建新的本地用户

I wrote a c# code that creates new local user

DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer");
DirectoryEntry group = localMachine.Children.Find("administrators", "group");
DirectoryEntry user = localMachine.Children.Find(accountName, "user");
Console.WriteLine(user.Properties.ToString());

我试图做来为用户登录脚本:

I tried to set the logon script for that user by doing:

localMachine.Properties["scriptPath"].Insert(0, "logonScript.vbs"); localMachine.CommitChanges();

同组或用户instances.but的财产不以任何论文情况下(LOCALMACHINE,组或用户)的存在。我知道,因为我做的:

same with group or user instances.but the property doesn't exist in any of theses instances (localMachine, group or user). I know that because I did:

System.Collections.ICollection col = localMachine.Properties.PropertyNames;
foreach (Object ob in col) { Console.WriteLine(ob.ToString()); }

怎么做,在其他方式任何想法?干杯,

Any idea of how to do that in other way?Cheers,

推荐答案

以下code是很好的工作在我的Windows 7的:

The following code is well working on my Windows Seven :

DirectoryEntry deComputer = new DirectoryEntry("WinNT://MyComputer,computer");
DirectoryEntry deUser = deComputer.Children.Add("JPB", "user");
deUser.Invoke("SetPassword", new object[] { "Password" }); 
deUser.Properties["Description"].Add("user $userName");
deUser.Properties["userflags"].Add(512);
deUser.Properties["passwordExpired"].Add(1);
deUser.Properties["LoginScript"].Add("start.cmd");
deUser.CommitChanges();

该脚本文件Start.cmd是present在目录:

The script file Start.cmd is present in the directory :

%systemroot%\system32\repl\import\Scripts

下面是PowerShell中的同样的事情:

Here is the same thing in PowerShell :

$computer = "MyComputer"
$userName = "jpb"
$objComputer = [ADSI]"WinNT://$computer"
$objUser = $objComputer.Create('user', $userName)
$objUser.SetPassword("Password")
$objUser.PSBase.InvokeSet('Description', "user $userName")
$objUser.PSBase.InvokeSet('LoginScript', "start.cmd")
$objUser.PSBase.InvokeSet('userflags', 512)
$objUser.PSBase.InvokeSet('passwordExpired', 1)
$objUser.SetInfo();

我希望它能帮助

I hope it helps

JP