Powershell – Update Computer Description on Logon

This script updates the Computer’s AD object description when a user logs into it. YOu would run this as a logon task via a Group Policy

We currently use a VBS script to update a computers description in AD to indicate Make/Model serial number and last user logged in

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 

For Each objSMBIOS in objWMI.ExecQuery("Select * from Win32_SystemEnclosure") 
  serviceTag = replace(objSMBIOS.SerialNumber, ",", ".")
  manufacturer = replace(objSMBIOS.Manufacturer, ",", ".")
Next

For Each objComputer in objWMI.ExecQuery("Select * from Win32_ComputerSystem")
  model = trim(replace(objComputer.Model, ",", "."))
Wscript.Echo model
Next
'Set objSysInfo = CreateObject("ADSystemInfo") 
'Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName) 

'if NOT objComputer.Description = WshNetwork.UserName & " (" & serviceTag & " - " & manufacturer & " " & model & ")" then
'  objComputer.Description = WshNetwork.UserName & " (" & serviceTag & " - " & manufacturer & " " & model & ")"
'  objComputer.SetInfo
'end if

Well that is all greek to me an I understand powershell a bit so this is what I came up with.
Now I am a lazy guy and doing WMI queries does my head in. Luckly the guys at GoverLan have this free tool that allows you to browse the WMI tree and generate Powershell code to query it. So the bit below is a mashup of their code and mine

    $wqlQuery = "Select IdentifyingNumber,Name,SKUNumber,UUID,Vendor from Win32_ComputerSystemProduct"
    
    $wqlObjectSet = Get-WmiObject -namespace "ROOT\CIMV2"  -Impersonation 3  -Query $wqlQuery
    if ($wqlObjectSet -eq $null)
    {
        Write-Host "WMI Object not found..."
        return;
    }
    
    if (@($wqlObjectSet).Count -gt 0)
    {
        foreach ($objWMIClass in $wqlObjectSet)
        {
            Write-Host
            Write-Host " -------------------------------- "
            
            Write-Host " Identifying Number:  " $objWMIClass.IdentifyingNumber
            Write-Host " Name: .............. " $objWMIClass.Name
            Write-Host " SKU Number: ........ " $objWMIClass.SKUNumber
            Write-Host " UUID: .............. " $objWMIClass.UUID
            Write-Host " Vendor: ............ " $objWMIClass.Vendor
            Write-Host " User:............... " $env:USERNAME
            Write-Host " Computername:....... " $env:COMPUTERNAME

            $CurrentADComputerInfo = Get-ADComputer -Identity $env:COMPUTERNAME

            $NewADComputerInfoDescription = $env:USERNAME+" "+$objWMIClass.Name+" "+$objWMIClass.IdentifyingNumber
            $CurrentADComputerInfo.Description
            $NewADComputerInfoDescription
            if ($CurrentADComputerInfo.Description -ne $NewADComputerInfoDescription)
            {
            #If new description is different to current description then Update ADComputer Description
            Set-ADComputer -Identity $env:COMPUTERNAME -Description $NewADComputerInfoDescription
            Write-Host " Description has changed .e.g. user:"
            }
            else
            {
            #If new description is same  to current description then do nothing
            Write-Host " Description has NOT changed:"
            }
        }
    }
    else
    {
        Write-Host "No instance."
    }


 

 

One thought on “Powershell – Update Computer Description on Logon

Leave a Reply to Brandon Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.