简体   繁体   English

通过登录的VBS脚本用户帐户描述

[英]VBS script user account description by logon

I have a fairly common VBScript to update user object descriptions in ADUC with the computer name, IP, and date/time of their last logon. 我有一个相当普通的VBScript,它使用上次登录的计算机名称,IP和日期/时间来更新ADUC中的用户对象描述。 I've got that functioning properly in my environment. 我已经在我的环境中正常运行了。 I've been trying to figure out how to add a "if contains then" statement to look at the distinguished name of the computer object, if it contains my server organizational unit name in the string, then quit the script. 我一直在尝试找出如何添加“如果包含则”语句来查看计算机对象的可分辨名称,如果它在字符串中包含我的服务器组织单位名称,则退出脚本。 I've tried it a few ways and can't seem to figure it out. 我已经尝试了几种方法,但似乎无法弄清楚。 We use a lot of terminal servers for different applications and so my intent is to only update descriptions when logon occurs on a PC. 我们将许多终端服务器用于不同的应用程序,因此我的目的是仅在PC上登录时更新描述。 I've found so many examples of scripts that serve similar functions, at this point I'm completely lost. 我发现了许多具有类似功能的脚本示例,但现在我已经完全迷失了。 I'm sure this has been done before but I'm not being able to find the right path. 我敢肯定这已经完成了,但是我找不到正确的路径。

'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

Maybe the better way to attack this is to filter by operating system? 也许对此进行攻击的更好方法是按操作系统进行筛选? I thought when you performed the GetObject with objSysInfo.ComputerName it would come back with the distinguished name to do the if contain then. 我以为,当您使用objSysInfo.ComputerName执行GetObject时,它将以可分辨的名称返回以进行if包含then。 Maybe my understanding is wrong. 也许我的理解是错误的。

Update1: Thanks to JosefZ I figured it out. Update1 感谢JosefZ,我弄清楚了。 Full script below: 完整脚本如下:

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

On line 4 change "SERVERS" to something that exists in the name of the OU you want to exclude from this script. 在第4行上,将"SERVERS"更改为要从此脚本中排除的OU名称中存在的名称。 I wanted to stop this script from running when users logged into servers and "SERVERS" is part of the title of the Parent OU. 当用户登录服务器并且“ SERVERS”是父OU标题的一部分时,我想停止运行此脚本。 When it finds that string it will quit the script. 当找到该字符串时,它将退出脚本。

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

Full script in "Update 1" of question. 问题的“更新1”中的完整脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM