Thursday, 21 August 2014

VBS - write computer details to AD computer account description

original post, how to delegate permissions in AD
http://www.compit.se/?p=282


output:
martin.junge / SN: F4***R1 / Dell Latitude E6420 / Windows 8.1 Pro  x64 / Last Boot Time: 08.08.2014 / Last Update Time: 21.7.2014 / Bios vers.: A21 / bitlocker disabled


---my modified vbs file---

On Error Resume Next

strComputer = "."
Set objSysInfo = CreateObject( "WinNTSystemInfo" )
strUserName = objSysInfo.UserName


Set objSysInfo = CreateObject("ADSystemInfo")
Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName)
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colcomputersystem = objWMIService.ExecQuery _
    ("Select * from Win32_computersystem")
Set colBIOS = objWMIService.ExecQuery _
    ("Select * from Win32_BIOS")

For each objcomputersystem in colcomputersystem
    Getcomputersystem = objcomputersystem.Model
    if Getcomputersystem = "VMware Virtual Platform" then Getcomputersystem = Right(Getcomputersystem, 17) end if

    if Getcomputersystem = "Virtual Machine" then
        Dim SysVarReg, Value
        Set SysVarReg = WScript.CreateObject("WScript.Shell")
        value = SysVarReg.RegRead ("HKLM\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters\PhysicalHostName")
        Getcomputersystem = Getcomputersystem & " on " & value
    end if

    GetComputerManufacturer = objComputerSystem.Manufacturer
Next

'---get Manufacturer--------------------
   'String cleaning - Manufacturer includes only first word, and the serial removes any spaces.
txtCount = InStr(GetComputerManufacturer," ") - 1
GetComputerManufacturer = Left(GetComputerManufacturer,txtCount)
if GetComputerManufacturer = "Hewlett-Packard" then GetComputerManufacturer = "" end if
if GetComputerManufacturer = "TOSHIBA" then GetComputerManufacturer = "Toshiba" end if
if GetComputerManufacturer = "VMware," then GetComputerManufacturer = Left(GetComputerManufacturer, 6) end if

if GetComputerManufacturer = "LENOVO" then
    Dim objWMIService, colItems
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colItems = objWMIService.ExecQuery("SELECT Version FROM Win32_ComputerSystemProduct")
    For Each objItem in colItems
        Getcomputersystem = objItem.Version & " " & objItem.Name
    Next
    GetComputerManufacturer = "Lenovo"
end if

if GetComputerManufacturer = "Microsoft Virtual Machine" then
    VMHost = WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters\PhysicalHostName")
    GetComputerManufacturer = GetComputerManufacturer & "on " & VMHost
end if
'---end Manufacturer-------

'---get serialnumber and BIOS version----------
GetSerialNumber = Replace(GetSerialNumber, " ", "")
For each objBIOS in colBIOS
    GetSerialNumber = objBIOS.SerialNumber
    GetBiosVers = objBIOS.SMBIOSBIOSVersion
Next
if GetComputerManufacturer = "VMware" then GetSerialNumber = Left(GetSerialNumber, 6) end if

'---end serialnumber-------

'---get processor architecture type---
Dim WshShell
Dim OsType
Set WshShell = CreateObject("WScript.Shell")
OsType = WshShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")
if OsType = "AMD64" then
OsType = "x64"
end if
'---end processor architecture type---

'---get Operating system---
Dim OSVersion
OSVersion = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName")
if OSVersion = "Microsoft Windows XP" then OSVersion = "Windows XP" end if
if OSVersion = "Microsoft Windows 2000" then OSVersion = "Windows 2000" end if
if OSVersion = "Microsoft Windows Server 2003" then OSVersion = "Windows Server 2003" end if
if OSVersion = "Microsoft Windows Server 2003 R2" then OSVersion = "Windows Server 2003 R2" end if
'---end Operating system---

'---get service pack version---
Const Impersonate = "winmgmts:{impersonationLevel=impersonate}!\\"
Set oWMI = GetObject(Impersonate & strComputer & "\root\cimv2")
Set QueryWMI = oWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem")
For Each oItem In QueryWMI
spVer = oItem.ServicePackMajorVersion
if spVer = 0 then spVer ="" else spVer = "SP" & spVer end if
Next
'---end service pack version---

'---get last boot time start----
Dim strBoot, strBootDate, strBootDay, strBootHour, strBootMins
Dim strBootMonth, strBootTime, strBootYear, strMsg, strQuery
Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
Set colItems = objWMIService.ExecQuery( "Select * from Win32_OperatingSystem", , 48 )
For Each objItem in colItems
strBootYear = Left( objItem.LastBootUpTime, 4 )
strBootMonth = Mid( objItem.LastBootUpTime, 5, 2 )
strBootDay = Mid( objItem.LastBootUpTime, 7, 2 )
strBootHour = Mid( objItem.LastBootUpTime, 9, 2 )
strBootMins = Mid( objItem.LastBootUpTime, 11, 2 )
strBootTime = strBootHour & ":" & strBootMins
Next
'---end last boot time----

'---get last windows update time------
Set objSession = CreateObject("Microsoft.Update.Session")
Set objSearcher = objSession.CreateUpdateSearcher
Set colHistory = objSearcher.QueryHistory(1, 1)
For Each objEntry in colHistory
 strHotfix = objEntry.Date
Next

'---end last windows update time------

'---get bitlocker status---

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2\Security\MicrosoftVolumeEncryption")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_EncryptableVolume",,48)
For Each objItem in colItems
    bitlockerdiveletter = objItem.DriveLetter
    bitlockerstatus = objItem.ProtectionStatus
    if bitlockerdiveletter = "C:" then
        if bitlockerstatus=1 then bitlockerstatusnote = "bitlocker enabled" else bitlockerstatusnote = "bitlocker disabled" end if
    end if
next
'---end bitlocker status---

'---define AD description text for clients and servers
If InStr(OSVersion, "Server") Then
    strMessage = GetComputerManufacturer & " " & trim(Getcomputersystem) & " / " & "SN: " & trim(GetSerialNumber) & " / " & OSVersion & " " & spVer  & " " & OsType & " / " & "Last Boot Time: " & strBootDay & "." & strBootMonth & "." & strBootYear & " / Last Update Time: " &  day(strHotfix) & "." & month(strHotfix) & "." & year(strHotfix) & " / Bios vers.: " & GetBiosVers
else
    strMessage = strUserName & " / " & "SN: " & trim(GetSerialNumber) & " / " & GetComputerManufacturer & " " & trim(Getcomputersystem) & " / " & OSVersion & " " & spVer  & " " & OsType & " / " & "Last Boot Time: " & strBootDay & "." & strBootMonth & "." & strBootYear & " / Last Update Time: " &  day(strHotfix) & "." & month(strHotfix) & "." & year(strHotfix) & " / Bios vers.: " & GetBiosVers & " / " & bitlockerstatusnote
end if
'---end define---

'WScript.Echo strMessage   'for testing

objComputer.Description = strMessage
objComputer.SetInfo

'---eof---

No comments:

Post a Comment