ADSI (Active Directory Services Interface)是Microsoft新推出的一项技术,它统一了许多底层服务的编程接口,程序员可以使用一致的对象技术来访问这些底层服务。 ADSI把这些服务的公共部分提取出来,同时隔离出相异的部分,程序员可以用统一的接口访问底层服务的公共部分,并延伸到底层服务的专有部分。

管理用户组

获取用户组的用户列表

Dim oGrp
Dim oUser
Dim sDomain
dim sMsg
sDomain = "localhost"
On Error Resume Next Set oGrp = GetObject("WinNT://"&sDomain&"/Administrators,group")
For Each oUser In oGrp.Members
sMsg = sMsg & oUser.Name & "(" & oUser.Class & ") " & oUser.ADsPath & vbnewline
Next
msgbox sMsg If (Err.Number<>) Then
MsgBox("An error has occurred. " &vbnewline& Err.Description)
End If
Set oGrp = Nothing
Set oUser = Nothing

另一种方法:

Dim oDomain
Dim oGrp
Dim oUser
Dim sDomain
dim sMsg
sDomain = "localhost"
On Error Resume Next Set oDomain = GetObject("WinNT://"&sDomain)
Set oGrp = oDomain.GetObject("group", "Administrators") For Each oUser In oGrp.Members
sMsg = sMsg & oUser.Name & "(" & oUser.Class & ") " & oUser.ADsPath & vbnewline
Next
msgbox sMsg If (Err.Number<>) Then
MsgBox("An error has occurred. " &vbnewline& Err.Description)
End If
Set oGrp = Nothing
Set oUser = Nothing

查询用户是否属于该用户组

Dim oGrp
On Error Resume Next Set oGrp = GetObject("WinNT://localhost/Administrators")
MsgBox oGrp.IsMember("WinNT://DESKTOP-K3O4FGP/Administrator") If (Err.Number<>) Then
MsgBox("An error has occurred. " &vbnewline& Err.Description)
End If
Set oGrp = Nothing

添加用户到用户组

该操作要求当前登录用户为Administrator。

Dim oGrp
dim sDomain
sDomain = "DESKTOP-K3O4FGP"
Set oGrp = GetObject("WinNT://"&sDomain&"/Administrators")
oGrp.Add ("WinNT://"&sDomain&"/Admin") if (Err.Number<>) then
MsgBox("An error has occurred. " &vbnewline& Err.Description)
else
msgbox "Complete"
end if
Set oGrp = Nothing

从用户组中移除用户

该操作要求当前登录用户为Administrator。

Dim oGrp
dim sDomain
sDomain = "DESKTOP-K3O4FGP"
On Error Resume Next Set oGrp = GetObject("WinNT://"&sDomain&"/Administrators")
oGrp.Remove ("WinNT://"&sDomain&"/jeffsmith") If (Err.Number<>) Then
MsgBox("An error has occurred. " &vbnewline& Err.Description)
else
msgbox "Complete"
End If
Set oGrp = Nothing

创建用户组

该操作要求当前登录用户为Administrator。

Dim oDomain
Dim oGroup
Dim sDomain
sDomain = "localhost"
On Error Resume Next
Set oDomain = GetObject("WinNT://"&sDomain)
Set oGroup = oDomain.Create("group","MyGroup")
oGroup.SetInfo if (Err.Number<>) then
MsgBox("An error has occurred. " &vbnewline& Err.Description)
else
msgbox "Complete"
end if
Set oGroup = Nothing
Set oDomain = Nothing

删除用户组

该操作要求当前登录用户为Administrator。

Dim oDomain
Dim sDomain
sDomain = "localhost"
On Error Resume Next
Set oDomain = GetObject("WinNT://"&sDomain)
oDomain.Delete "group","MyGroup" if (Err.Number<>) then
MsgBox("An error has occurred. " &vbnewline& Err.Description)
else
msgbox "Complete"
end if
Set oDomain = Nothing

管理用户

添加用户

该操作要求当前登录用户为Administrator。

Dim oDomain
Dim oUser
Dim sDomain
sDomain = "localhost"
On Error Resume Next Set oDomain = GetObject("WinNT://"&sDomain)
Set oUser = oDomain.Create("user","jeffsmith")
'oUser.FullName = "FullName" '用户全名
'oUser.Description = "Description" '描述
'oUser.SetPassword "password" '设置密码
'oUser.PasswordExpired = 1 '下次登录需要更改密码
'oUser.UserFlags = oUser.UserFlags Or &H10000
'&H20000(下次登录须更改密码)
'&H0040(用户不能更改密码)
'&H10000(密码永不过期)
'&H0002(账户已禁用)
oUser.SetInfo if (Err.Number<>) then
MsgBox("An error has occurred. " &vbnewline& Err.Description)
else
msgbox "Complete"
end if

如果未设置用户属性,则 新建的用户的默认属性如下:

Property Value
Full Name SAM Account Name (such as jeffsmith)
Password Empty
User Must Change Password TRUE
User Cannot Change Password FALSE
Password Never Expires FALSE
Account Disabled FALSE
Group Domain User
Profile Empty
Account Never Expires TRUE

修改用户属性

该操作要求当前登录用户为Administrator。

Dim oUser
Dim sDomain
sDomain = "localhost"
On Error Resume Next
Set oUser = GetObject("WinNT://"&sDomain&"/jeffsmith") oUser.FullName = "jeffsmith"
oUser.Description = "Description"
oUser.AccountDisabled = False
oUser.IsAccountLocked = False
oUser.SetInfo if (Err.Number<>) then
MsgBox("An error has occurred. " &vbnewline& Err.Description)
else
msgbox "Complete"
end if

用户属性详见:https://docs.microsoft.com/zh-cn/windows/win32/adsi/iadsuser-property-methods

设置用户密码

该操作要求当前登录用户为Administrator。

Dim oUser
Dim sDomain
sDomain = "localhost"
On Error Resume Next
Set oUser = GetObject("WinNT://"&sDomain&"/jeffsmith") oUser.SetPassword "pa55w0rd!" if (Err.Number<>) then
MsgBox("An error has occurred. " &vbnewline& Err.Description)
else
msgbox "Complete"
end if

更改用户密码

该操作要求当前登录用户为Administrator。

Dim oUser
Dim sOldPass
Dim sNewPass
Dim sDomain
sDomain = "localhost"
On Error Resume Next Set oUser = GetObject("WinNT://"&sDomain&"/JeffSmith,user")
' Add code to securely retrieve the old and new password.
oUser.ChangePassword sOldPass, sNewPass if (Err.Number<>) then
MsgBox("An error has occurred. " &vbnewline& Err.Description)
else
msgbox "Complete"
end if
Set oUser = Nothing

删除用户

该操作要求当前登录用户为Administrator。

Dim oDomain
Dim sDomain
sDomain = "localhost"
On Error Resume Next Set oDomain = GetObject("WinNT://"&sDomain)
oDomain.Delete "user", "jeffsmith" if (Err.Number<>) then
MsgBox("An error has occurred. " &vbnewline& Err.Description)
else
msgbox "Complete"
end if

查询用户隶属的组

Dim oUser
Dim oGroup
Dim sDomain
Dim sMsg
sDomain = "localhost"
On Error Resume Next
Set oUser = GetObject("WinNT://"&sDomain&"/Administrator") For Each oGroup In oUser.Groups
sMsg = sMsg & oGroup.Name & vbnewline
Next if (Err.Number<>) then
MsgBox("An error has occurred. " &vbnewline& Err.Description)
else
msgbox sMsg
end if

引用:https://docs.microsoft.com/zh-cn/windows/win32/adsi/adsi-objects-of-winnt

用vbs和ADSI管理Windows账户的更多相关文章

  1. Windows账户管理

    windows账户管理 最近部署人员给我们提了一个需求,就是希望简化部署过程. 为了能够远程桌面控制终端电脑,他们需要为每台终端设置进行一些设置,例如创建用户名和密码,开启允许 远程桌面设置,以及开机 ...

  2. 玩转Windows服务系列——命令行管理Windows服务

    说到Windows服务的管理就不得不说通过命令行的方式管理Windows服务,因为无论是系统管理员,还是通过编程的方式调用cmd命令,命令行都是非常方便以及强大的工具. 接下来就看一下如何通过cmd命 ...

  3. 使用Chef管理windows集群

    但凡服务器上了一定规模(百台以上),普通的ssh登录管理的模式就越来越举步维艰.试想Linux发布了一个高危漏洞的补丁,你要把手下成百上千台机器都更新该补丁,如果没有一种自动化方式,那么至少要耗上大半 ...

  4. 使用Chef管理windows集群 | 运维自动化工具

    但凡服务器上了一定规模(百台以上),普通的ssh登录管理的模式就越来越举步维艰.试想Linux发布了一个高危漏洞的补丁,你要把手下成百上千台机器都更新该补丁,如果没有一种自动化方式,那么至少要耗上大半 ...

  5. 玩转Windows服务系列——命令行管理Windows服务

    原文:玩转Windows服务系列——命令行管理Windows服务 说到Windows服务的管理就不得不说通过命令行的方式管理Windows服务,因为无论是系统管理员,还是通过编程的方式调用cmd命令, ...

  6. [转帖]Ansible管理windows集群

    Ansible管理windows集群 http://www.cnblogs.com/Dev0ps/p/10026908.html 写的挺好的 我关注点还是不够好呢 最近公司新项目需要安装400+win ...

  7. 厉害—Ansible管理windows集群

    最近公司新项目需要安装400+windows server 2012系统的工作站,想着怎么能像linux下运用ansible批量管理,linux就很简单了有ssh服务 但是下却没这么简单,但还是有办法 ...

  8. [转帖]Ansible批量远程管理Windows主机(部署与配置)

    2018-09-12 12:04:42 https://blog.51cto.com/7424593/2174156 一.测试环境介绍 Ansible管理主机: 系统:   CentOS6.8 IP ...

  9. Ansible 批量管理Windows Server服务器

    Ansible批量管理Windows Server         Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具,  它用Python写成,类似于saltstack和Puppe ...

随机推荐

  1. 天下代码一大抄,整个案例的搬是什么鬼!疑似冒充蚂蚁金服高级Java开发工程师?你大爷

    写在开始 上班前的第一件事,就是码云看看有什么消息,回复下网友的问题.如果看到喜欢的项目会点进去瞅瞅,然后就开始一天的工作. 然而,这一天的工作并不开心,一个今日热门项目让自己很恼火,一开始感觉并没有 ...

  2. Server基本语句的用法

    1.创建数据库 create database databaseName use databaseName go   /*  转到指定数据库 */ 2.创建表 create table tableNa ...

  3. reports buileder 触发器的写法

    触发器写法: function CF_SHOULD_BACK_TIMEFormula return Number is--其他:取MES工时按工段分别统计产量.投入工时合计:应回报工时=移动数量*[∑ ...

  4. 从0系统学Android--3.6 RecyclerView

    从0系统学Android--更强大的滚动控件---RecyclerView 本系列文章目录:更多精品文章分类 本系列持续更新中.... 参考<第一行代码> 首先说明一点昨天发了一篇关于 L ...

  5. sqlserver默认隔离级别下并发批量update同一张表引起的死锁

    提到死锁,最最常规的场景之一是Session1 以排它锁的方式锁定A表,请求B表,session2以排它锁的方式锁定B表,请求A表之类的,访问顺序不一致导致死锁的情况本文通过简化,测试这样一种稍显特殊 ...

  6. MySQL多实例安装教程

    目录 MySQL的多实例 实验准备: 准备阶段: 实验阶段 MySQL的多实例 实验准备: 1. 一个干净的centos7系统 2. 关闭防火墙和selinux 3. 之前已经二进制安装过的MySQL ...

  7. 《Web Development with Go》Middleware之共享数据

    这个库值得学, 好像写起来越来越溜 package main import ( "fmt" "log" "net/http" //" ...

  8. alter对话框处理:

    from selenium import webdriverd = webdriver.Firefox()d.get('file://C:\\我的代码\\selenium自动化测试\\alter.ht ...

  9. vue.js 的 vue-element-admin 实践开发

    官方网址: https://panjiachen.github.io/vue-element-admin-site/zh/ 一:面包屑导航,根目录文字修改: 定位到文件 vue-element-sup ...

  10. .net core 在 Docker 上的部署

    Docker可以说是现在微服务,DevOps的基础,咱们.Net Core自然也得上Docker..Net Core发布到Docker容器的教程网上也有不少,但是今天还是想来写一写.你搜.Net co ...