Skip to content

Instantly share code, notes, and snippets.

@gaelicWizard
Last active February 12, 2024 21:51
Show Gist options
  • Save gaelicWizard/19929c2242916528f0f7 to your computer and use it in GitHub Desktop.
Save gaelicWizard/19929c2242916528f0f7 to your computer and use it in GitHub Desktop.
Automatically sync the Description field between the local registry, Active Directory, and LogMeIn.
'VERSION: 1.1
'NOTE : This script requires that "SELF" have "WRITE DESCRIPTION" permission on AD computer object. Do this through "delegate control".
On Error Resume Next
Set WSHShell = CreateObject("WScript.Shell")
Set objSysInfo = CreateObject("ADSystemInfo")
Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName)
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
localDescriptionKey = "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\LANManServer\Parameters\SRVComment"
logMeInDescriptionKey = "HKEY_LOCAL_MACHINE\Software\LogMeIn\V5\webSvc\HostDescription"
cacheDescriptionKey = "HKEY_LOCAL_MACHINE\Software\gaelicWizard\update_computer_description\lastDescriptionValue"
Function GetDomainName()
Dim Info
Set Info = CreateObject("AdSystemInfo")
GetDomainName = Info.DomainDNSName
End Function
Function GetComputerName()
Dim InfoNT
Set InfoNT = CreateObject("WinNTSystemInfo")
GetComputerName = lcase(InfoNT.ComputerName)
End Function
Function GetForestDNSName()
Dim Info
Set Info = CreateObject("AdSystemInfo")
GetForestDNSName = Info.ForestDNSName
End Function
function startService( strServiceName )
' Start service:
Set colServiceList = objWMI.ExecQuery _
("Select * from Win32_Service where Name='" & strServiceName & "'")
For each objService in colServiceList
'wscript.echo "Starting " & strServiceName & "..."
errReturn = objService.StartService()
Next
Wscript.Sleep 2000
' Start dependencies:
Set colServiceList = objWMI.ExecQuery("Associators of " _
& "{Win32_Service.Name='" & strServiceName & "'} Where " _
& "AssocClass=Win32_DependentService " & "Role=Antecedent" )
For each objService in colServiceList
startService( objService.Name )
Next
end function
function stopService( strServiceName )
' Stop dependencies:
Set colServiceList = objWMI.ExecQuery("Associators of " _
& "{Win32_Service.Name='" & strServiceName & "'} Where " _
& "AssocClass=Win32_DependentService " & "Role=Antecedent" )
For each objService in colServiceList
stopService( strServiceName )
Next
Wscript.Sleep 2000
' Stop service:
Set colServiceList = objWMI.ExecQuery _
("Select * from Win32_Service where Name='" & strServiceName & "'")
For each objService in colServiceList
'wscript.echo "Stopping " & strServiceName & "..."
errReturn = objService.StopService()
Wscript.Sleep 2000
Next
end function
function restartService( strServiceName )
stopService( strServiceName )
Wscript.Sleep 2000
startService( strServiceName )
end function
function readFromRegistry( strRegistryKey )
Dim value
On Error Resume Next
value = WSHShell.RegRead( strRegistryKey )
if err.number <> 0 then
readFromRegistry= ""
else
readFromRegistry=value
end if
end function
function writeComputerDescription( strNewComputerDescription )
' Update local registry with new description.
'wscript.echo "Writing description: " & strNewComputerDescription
WSHShell.RegWrite localDescriptionKey, strNewComputerDescription, "REG_SZ"
' Update AD with new description; requires SELF permission "write description".
objComputer.Description = strNewComputerDescription
objComputer.SetInfo
' Update LogMeIn with new description; takes effect next time LogMeIn service starts.
WSHShell.RegWrite logMeInDescriptionKey, GetComputerName() & ": " & strNewComputerDescription, "REG_SZ"
'Restart LogMeIn so that it uploads its new description.
restartService( "LogMeIn" )
' Cache this update.
WSHShell.RegWrite cacheDescriptionKey, strNewComputerDescription, "REG_SZ"
end function
function stripComputerNamePrefix( strDescriptionWithComputerNamePrefix )
'wscript.echo "Stripping " & strDescriptionWithComputerNamePrefix & "."
stripComputerNamePrefix = Replace(strDescriptionWithComputerNamePrefix, GetComputerName() & ": ","",,, vbTextCompare)
stripComputerNamePrefix = Replace(strDescriptionWithComputerNamePrefix, GetComputerName() & "." & GetDomainName(),"",,, vbTextCompare)
'wscript.echo "Stripped LogMeIn description: " & stripComputerNamePrefix & "."
end function
function updateComputerDescriptionMain()
' Write the description everywhere if it's changed anywhere.
computerName = GetComputerName()
localDescription = readFromRegistry(localDescriptionKey)
'wscript.echo "Local description: " & localDescription & "."
logMeInDescription = readFromRegistry(logMeInDescriptionKey)
'wscript.echo "LogMeIn description: " & logMeInDescription & "."
directoryDescription = objComputer.Description
'wscript.echo "Directory description: " & directoryDescription & "."
cachedDescription = readFromRegistry(cacheDescriptionKey)
'wscript.echo "Cached description: " & cachedDescription & "."
'wscript.echo "FQDN: " & GetComputerName() & "." & GetDomainName()
if NOT cachedDescription = directoryDescription then
'Directory description updated since last launch; propagate.""
writeComputerDescription( directoryDescription )
else if NOT computerName & ": " & cachedDescription = logMeInDescription then
'LogMeIn description updated since last launch; propagate.'
strippedLogMeInDescription = stripComputerNamePrefix( logMeInDescription )
writeComputerDescription( strippedLogMeInDescription )
else if NOT cachedDescription = localDescription then
'Local description updated since last launch; propagate.
writeComputerDescription( localDescription )
end if
end if
end if
end function
updateComputerDescriptionMain()
@tornister76
Copy link

hi, Have you tried to change LMI host description this way and it really changed host description in LMI Central list? We try but after restart it is overwriteen by old one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment