用vbs和ADSI管理Windows账户
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账户的更多相关文章
- Windows账户管理
windows账户管理 最近部署人员给我们提了一个需求,就是希望简化部署过程. 为了能够远程桌面控制终端电脑,他们需要为每台终端设置进行一些设置,例如创建用户名和密码,开启允许 远程桌面设置,以及开机 ...
- 玩转Windows服务系列——命令行管理Windows服务
说到Windows服务的管理就不得不说通过命令行的方式管理Windows服务,因为无论是系统管理员,还是通过编程的方式调用cmd命令,命令行都是非常方便以及强大的工具. 接下来就看一下如何通过cmd命 ...
- 使用Chef管理windows集群
但凡服务器上了一定规模(百台以上),普通的ssh登录管理的模式就越来越举步维艰.试想Linux发布了一个高危漏洞的补丁,你要把手下成百上千台机器都更新该补丁,如果没有一种自动化方式,那么至少要耗上大半 ...
- 使用Chef管理windows集群 | 运维自动化工具
但凡服务器上了一定规模(百台以上),普通的ssh登录管理的模式就越来越举步维艰.试想Linux发布了一个高危漏洞的补丁,你要把手下成百上千台机器都更新该补丁,如果没有一种自动化方式,那么至少要耗上大半 ...
- 玩转Windows服务系列——命令行管理Windows服务
原文:玩转Windows服务系列——命令行管理Windows服务 说到Windows服务的管理就不得不说通过命令行的方式管理Windows服务,因为无论是系统管理员,还是通过编程的方式调用cmd命令, ...
- [转帖]Ansible管理windows集群
Ansible管理windows集群 http://www.cnblogs.com/Dev0ps/p/10026908.html 写的挺好的 我关注点还是不够好呢 最近公司新项目需要安装400+win ...
- 厉害—Ansible管理windows集群
最近公司新项目需要安装400+windows server 2012系统的工作站,想着怎么能像linux下运用ansible批量管理,linux就很简单了有ssh服务 但是下却没这么简单,但还是有办法 ...
- [转帖]Ansible批量远程管理Windows主机(部署与配置)
2018-09-12 12:04:42 https://blog.51cto.com/7424593/2174156 一.测试环境介绍 Ansible管理主机: 系统: CentOS6.8 IP ...
- Ansible 批量管理Windows Server服务器
Ansible批量管理Windows Server Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具, 它用Python写成,类似于saltstack和Puppe ...
随机推荐
- C lang:character input and output (I/O)
Xx_Introduction Character input and output is by more line character conpose of the text flow Defin ...
- 卡拉OK歌词原理和实现高仿Android网易云音乐
大家好,我们是爱学啊,继上一篇讲解了[LRC歌词原理和实现高仿Android网易云音乐],今天给大家带来一篇关于卡拉OK歌词原理和在Android上如何实现歌词逐字滚动的效果,本文来自[Android ...
- Python内置装饰器@property
在<Python装饰器(Decorators )>一文中介绍了python装饰器的概念,日常写代码时有一个装饰器很常见,他就是内置的@property. 我们一步步的来接近这个概念. 一个 ...
- C# Distinct去重泛型List
List<int>去重 List<string>去重 List<T>去重 1. List<int>去重 List<int> ilist = ...
- Linux 目录管理的相关命令
mkdir,rmdir 创建目录mkdir -p:当上级目录不存在时,自动创建上级目录 -v:显示创建过程 $ mkdir -pv /tmp/x/y/z/ mkdir: created directo ...
- luoguP1871 对撞机【赛后第一题
题面 题目描述 在2312年,宇宙中发现了n台巨型对撞机,这些对撞机分别用1-n的自然数标识.科学家们不知道启动这些对撞机会发生什么危险事故,所以这些机器,刚开始都是出于关闭的状态. 随着科学家们的研 ...
- centos7 链路聚合+KVM桥接连网
一.两个物理网卡做链路聚合(em3,em4) 1)创建team类型的网卡,连接别名为team0,使用的模式为activebackup-主备/loadbalance-负载均衡nmcli con add ...
- 10 个提升效率的Linux小技巧
您是否曾经惊讶于看到某人在 UNIX 中非常快速地工作,触发命令并快速地执行操作?是的,我碰到过几次,并且我一直都在向那些超级巨星开发者学习.在本文中,我想分享一些 UNIX 命令实践,这些实践是我在 ...
- SpringBoot日志原理解析
1.日志框架 小张:开发一个大型系统:1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件?2.框架来记录系统的一些运行时信息:日志框架 : ...
- itextsharp操作pdf——插入图片、二维码等
简单介绍 业务需求,需要往pdf图纸上添加二维码功能,将实现过程记录下来 下载类库 直接下载 添加引用 添加命名空间 using System.IO; using iTextSharp.text.pd ...