读书人

请问怎么在Win2003系统中通过程序或者

发布时间: 2012-01-08 22:48:50 作者: rapoo

请教如何在Win2003系统中通过程序或者脚本添加用户组和用户?(300分)
我想知道在C#或者VB.NET中如何通过程序来添加用户组和用户。
我大楷晓得用System.DirectoryServices类来处理。不过现在我没有什么头绪,以前也没有涉及过。希望那位能提供一个稍微详细一点的解决方案。
两贴一并结贴。
多谢!
(http://community.csdn.net/Expert/topic/5581/5581354.xml?temp=.5794184)

[解决办法]
添加用户:http://blog.chinaunix.net/u/884/showart_254580.html
[解决办法]
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();

}

}


}
}

[解决办法]

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;
//此用户为网络用户,一般权限
[解决办法]
这是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;
}

[解决办法]
或者可以运行命令行
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

读书人网 >windows

热点推荐