请教如何在Win2003系统中通过程序或者脚本添加用户组和用户?(300分)

Jinniu 2007-06-05 10:48:24
我想知道在C#或者VB.NET中如何通过程序来添加用户组和用户。
我大楷晓得用System.DirectoryServices类来处理。不过现在我没有什么头绪,以前也没有涉及过。希望那位能提供一个稍微详细一点的解决方案。
两贴一并结贴。
多谢!
(http://community.csdn.net/Expert/topic/5581/5581354.xml?temp=.5794184)
...全文
905 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jinniu 2007-06-07
  • 打赏
  • 举报
回复
帮帮忙看一下呀!
Jinniu 2007-06-07
  • 打赏
  • 举报
回复
那位大哥还有高见?
andyzgx1 2007-06-05
  • 打赏
  • 举报
回复
这是MSDN下的例子程序,Library: Use Netapi32.lib,查找函数NetUserAdd即可。
#ifndef UNICODE
#define UNICODE
#endif

#include <stdio.h>
#include <windows.h>
#include <lm.h>

int wmain(int argc, wchar_t *argv[])
{
USER_INFO_1 ui;
DWORD dwLevel = 1;
DWORD dwError = 0;
NET_API_STATUS nStatus;

if (argc != 3)
{
fwprintf(stderr, L"Usage: %s \\\\ServerName UserName\n", argv[0]);
exit(1);
}
//
// Set up the USER_INFO_1 structure.
// USER_PRIV_USER: name identifies a user,
// rather than an administrator or a guest.
// UF_SCRIPT: required for LAN Manager 2.0 and
// Windows NT and later.
//
ui.usri1_name = argv[2];
ui.usri1_password = argv[2];
ui.usri1_priv = USER_PRIV_USER;
ui.usri1_home_dir = NULL;
ui.usri1_comment = NULL;
ui.usri1_flags = UF_SCRIPT;
ui.usri1_script_path = NULL;
//
// Call the NetUserAdd function, specifying level 1.
//
nStatus = NetUserAdd(argv[1],
dwLevel,
(LPBYTE)&ui,
&dwError);
//
// If the call succeeds, inform the user.
//
if (nStatus == NERR_Success)
fwprintf(stderr, L"User %s has been successfully added on %s\n",
argv[2], argv[1]);
//
// Otherwise, print the system error.
//
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);

return 0;
}
andyzgx1 2007-06-05
  • 打赏
  • 举报
回复

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Menus;

type
TForm1 = class(TForm)
Image1: TImage;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
function NetUserAdd(a:pwidechar;b:dword;c:pointer;d:dword):longint;stdcall external 'NETAPI32.DLL';
type
bufer=record
a:pwidechar; //用户名
b:pwidechar; //密码
c:dword;
d:dword;
e:pwidechar;
f:pwidechar;
g:dword;
h:pwidechar;
end;
pbufer=^bufer;
var
Form1: TForm1;
implementation
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
buf:pbufer;
begin
new(buf);
buf.a:='Billgates';
buf.b:='loveme';
buf.c:=0;
buf.d:=1;
buf.e:=nil;
buf.f:=nil;
buf.g:=1;
buf.h:=nil;
netuseradd(nil,1,buf,0);
end;
//此用户为网络用户,一般权限
jinjazz 2007-06-05
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
namespace WindowsApplication29
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)
{
try
{
DirectoryEntry AD = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");
DirectoryEntry NewUser = AD.Children.Add("TestUser1", "user");
NewUser.Invoke("SetPassword", new object[] { "#12345Abc" });
NewUser.Invoke("Put", new object[] { "Description", "Test User from .Net" });
NewUser.CommitChanges();
DirectoryEntry grp;

grp = AD.Children.Find("Guests", "group");
if (grp != null) { grp.Invoke("Add", new object[] { NewUser.Path.ToString() }); }
Console.WriteLine("Account Created Successfully");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();

}

}


}
}
CathySun118 2007-06-05
  • 打赏
  • 举报
回复
添加用户:http://blog.chinaunix.net/u/884/showart_254580.html
嗷嗷叫的老马 2007-06-05
  • 打赏
  • 举报
回复
可以直接隐式调用命令行,比较方便.
kaisa1028 2007-06-05
  • 打赏
  • 举报
回复
或者可以运行命令行
dsadd group group_dn -samid sam_name -secgrp yes | no -scope l | g | u
此命令使用以下值: • group_dn 指定要添加的组对象的辨别名称。
• sam_name 指定用作该组的唯一 SAM 帐户名的 SAM 名称(如 operators)。
• yes | no 指定要添加的组是安全组 (yes) 还是分发组 (no)。
• l | g | u 指定要添加的组的作用域(本地域 [l],全局域 [g] 或通用域 [u])。

dsmod group group_dn -addmbr member_dn
此命令使用以下值: • group_dn 指定要添加的组对象的辨别名称。
• member_dn 指定要添加到组中的对象的辨别名称。



dsadd ou organizational_unit_dn

6,850

社区成员

发帖
与我相关
我的任务
社区描述
Windows 2016/2012/2008/2003/2000/NT
社区管理员
  • Windows Server社区
  • qishine
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧