VBS脚本用户帐户描述登录

时间:2015-08-21 00:54:19

标签: authentication vbscript active-directory

我有一个相当常见的VBScript来更新ADUC中的用户对象描述,其中包含计算机名称,IP和上次登录的日期/时间。我在我的环境中正常运行。我一直试图找出如何添加"如果包含然后"用于查看计算机对象的可分辨名称的语句,如果它在字符串中包含我的服务器组织单位名称,则退出该脚本。我已经尝试了几种方法,似乎无法弄明白。我们为不同的应用程序使用了很多终端服务器,因此我的目的是仅在PC上登录时更新描述。我找到了很多提供类似功能的脚本示例,此时我完全迷失了。我确信之前已经做过,但我找不到合适的路径。

'Open a connection to LDAP
Set objSysInfo = CreateObject("ADSystemInfo")
'Find the User in LDAP that is opening the connection
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
'Find the Computer in LDAP that the connection originates from
Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName)
'Set string to local computer
strComputer = "." 
'If object contains value of being in the server OU, quit the script.

'!-This is the section I'm trying to figure out, removed my failed attempts-!

'impersonate the computer's wmiservice
Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
'Use the wmiservice to query for the IPAddresses from the Network Adapter Configuration where the NIC is IPEnabled
Set IPs = objWMIService.ExecQuery _ 
 ("Select IpAddress from Win32_networkadapterconfiguration where IPEnabled = True") 
'For Each IPAddress returned, set string to the IPAddress (Hopefully the IPv4 one)
For Each IP in IPs
 strIPaddr = IP.IPAddress(i)
'Loop through
Next
'Build the string that will bound to the User's Description
' In this case: Workstation001 - 192.168.100.21 - 3/6/2015 12:51PM
strMessage = objComputer.CN & " - " & strIPaddr & " - " & Now 
'Apply the string to the User's Description, then write it to LDAP
objUser.Description = strMessage
objUser.SetInfo

攻击这个的更好方法是按操作系统过滤?我想当你使用objSysInfo.ComputerName执行GetObject时,它会返回可分辨名称来执行if then then。也许我的理解是错误的。

UPDATE1: 感谢JosefZ我明白了。完整的脚本如下:

Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName)
If InStr(1, objComputer.distinguishedName, "SERVERS") > 0 then
 Wscript.Quit
End if
strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set IPs = objWMIService.ExecQuery _ 
 ("Select IpAddress from Win32_networkadapterconfiguration where IPEnabled = True") 
For Each IP in IPs
 strIPaddr = IP.IPAddress(i)
Next
strMessage = objComputer.CN & " - " & strIPaddr & " - " & Now 
objUser.Description = strMessage
objUser.SetInfo

在第4行,将"SERVERS"更改为您要从此脚本中排除的OU名称中存在的内容。我希望在用户登录服务器和"服务器"是母OU的标题的一部分。当它找到该字符串时,它将退出该脚本。

1 个答案:

答案 0 :(得分:0)

Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName)
If InStr(1, objComputer.distinguishedName, "SERVERS") > 0 then
 Wscript.Quit
End if

问题“更新1”中的完整脚本。