VBscript for workstation

Khaleda Parvin Fahmida (External) 6 Reputation points
2022-08-28T08:19:53.21+00:00

Hi, sometimes I face runtime error problems while running the VBScript for collecting data from the active directory. Currently running Windows 10 on version 20H2 19042.1889. What is the reason here and what is the solution? Please help.

![235534-image.png]1

'List users AD attribs -

'11oct10 - customized for Linde comps with SP. 88g6 is used also for users

'can run in Linde & boc.com & as.boc.com (but use 88g5 for AS & BOC domains as it pulls the Ext Assoc Account also)

' If you don't want the Ext Assoc A/c, you can run this as this is much faster

' this is an extension of vbs-88g1 - it gets Exchange details also.

'- Contains DisplayName attrib - which gives issue in Linde domain for user objects (due to ChuiS whose DispName has chinese chars, after KwongC)

'- resolved by converting each character of DisplayName attrib to ascii & reconverting back to character

' Specify directory for output file

strPath = "C:\Users\g5ub01\Desktop\VBScript"   

'ObjType = "User"   

Const ADS_RIGHT_ASSOCIATED_EXTERNAL_ACCOUNT = &H00004   

'On Error Resume Next   

ArrayOUs = Array(_

"ou=LGBD,OU=Workstations,dc=linde,dc=lds,dc=grp", _

"ou=LGIN,OU=Workstations,dc=linde,dc=lds,dc=grp", _

"ou=LGVN,OU=Workstations,dc=linde,dc=lds,dc=grp", _

"ou=LGID,OU=Workstations,dc=linde,dc=lds,dc=grp", _

"ou=LGMY,OU=Workstations,dc=linde,dc=lds,dc=grp", _

"ou=LGPH,OU=Workstations,dc=linde,dc=lds,dc=grp", _

"ou=LGSG,OU=Workstations,dc=linde,dc=lds,dc=grp", _

"ou=LGTH,OU=Workstations,dc=linde,dc=lds,dc=grp", _

"ou=LGLK,OU=Workstations,dc=linde,dc=lds,dc=grp" _

  )   



set fsoelog = CreateObject("Scripting.Filesystemobject")   

strDate = DatePart("yyyy",Date) _    

    & Right("0" & DatePart("m",Date), 2) _    

    & Right("0" & DatePart("d",Date), 2)    

'strTime = Right("0" & Hour(Now), 2) & Right("0" & Minute(Now), 2) & Right("0" & Second(Now), 2)       

strTime = Right("0" & Hour(Now), 2) & Right("0" & Minute(Now), 2)   

strFileName = "Output RSE Workstations " & strDate & strTime & ".txt"   

strFullName = fsoelog.BuildPath(strPath, strFileName)         

'strFileName = "vbs-88g3-Out-ListUsers-" & year(now) & month(now) & day(now) & hour(now) & minute(now) & ".txt"   

'strFullName = fsoelog.BuildPath(strPath, strFileName)   



Set tfelog = fsoelog.OpenTextFile(strFullName, 8, 1, 0) '8 - for append, 1 - create if not existing, 0 - ASCII   



tfelog.WriteLine "TotalCount" & ";" & "cn" & ";" & "AccountDisabled" & ";" & "LastLogon" & ";" & "CountFromToday"  & ";" & "CountGT21"   & ";" & "CountGT21AndEnabled" & ";" & "Count" & ";" & "DN" & ";" & "OU" & ";" & "OS" & ";" & "ServicePack"   

'tfelog.WriteLine("List of Users in India OU as on: " & Now)   

TotalCount = 1      

For iOUCnt = 0 to Ubound(ArrayOUs)      

    strLdapPath = ArrayOUs(iOUCnt)   



    'tfelog.WriteLine strLdapPath   



    Set objContainer = GetObject("LDAP://" & strLdapPath)   

    count = 1   

    'objContainer.Filter = Array(ObjType)   

    For Each objChild In objContainer   

        ValUserAcCtrl = objChild.UserAccountControl     'Get UserAccountControl Flags   



        'Is Account Disabled?   

        if (ValUserAcCtrl AND &h0002) = &h0002 then   

            Flag_AcDisabled = "1"   

        else Flag_AcDisabled = "0"   

        End If   



        ValNormal = Flag_AcDisabled    



        'Get Last Logon   

        On Error Resume Next   

        Set objDate = objChild.lastLogonTimeStamp   

        If (Err.Number <> 0) Then   

            On Error GoTo 0   

            dtmDate = #1/1/1601#   

        Else   

            On Error GoTo 0   

            lngHigh = objDate.HighPart   

            lngLow = objDate.LowPart   

            If (lngLow < 0) Then   

                lngHigh = lngHigh + 1   

            End If   

            If (lngHigh = 0) And (lngLow = 0 ) Then   

                dtmDate = #1/1/1601#   

            Else   

                dtmDate = #1/1/1601# + (((lngHigh * (2 ^ 32)) _   

                    + lngLow)/600000000 - lngBias)/1440   

            End If   

        End If   



        If (dtmDate = #1/1/1601#) Then   

            dtmDate = "Never"   

        End If     



        If dtmDate <> "Never" Then   

            SDateToday = Date   

            CountFromTodayLogon = DateDiff("d",dtmDate,SDateToday)   

            If CountFromTodayLogon > 21 then   

                CountGT21Logon = 1   

            Else   

                CountGT21Logon = 0   

            End If   

        Else   

            CountFromTodayLogon = 9999   

            CountGT21Logon = 1   

        End If   



        If (CountGT21Logon=1) AND (Flag_AcDisabled=0) Then   

            CountGT21AndEnabled = 1   

        Else   

            CountGT21AndEnabled = 0   

        End If         



        'Get Password Last Set   

        On Error Resume Next   

        Set objPwdDate = objChild.PwdLastSet   

        If (Err.Number <> 0) Then   

            On Error GoTo 0   

            dtmPwdDate = #1/1/1601#   

        Else   

            On Error GoTo 0   

            lngPwdHigh = objPwdDate.HighPart   

            lngPwdLow = objPwdDate.LowPart   

            If (lngPwdLow < 0) Then   

                lngPwdHigh = lngPwdHigh + 1   

            End If   

            If (lngPwdHigh = 0) And (lngPwdLow = 0 ) Then   

                dtmPwdDate = #1/1/1601#   

            Else   

                dtmPwdDate = #1/1/1601# + (((lngPwdHigh * (2 ^ 32)) _   

                    + lngPwdLow)/600000000 - lngBias)/1440   

            End If   

        End If   



        If (dtmPwdDate = #1/1/1601#) Then   

            dtmPwdDate = "Never"   

        End If     



        If dtmPwdDate <> "Never" Then   

            'SDateToday = Date   

            CountFromTodayPwd = DateDiff("d",dtmPwdDate,SDateToday)   

            If CountFromTodayPwd > 21 then   

                CountGT21Pwd = 1   

            Else   

                CountGT21Pwd = 0   

            End If   

        Else   

            CountFromTodayPwd = 9999   

            CountGT21Pwd = 1   

        End If   



        dispName = objChild.DisplayName  'get DisplayName   

        'convert each char to Asc & back (to manage Chinese chars in DispName)   

        FullAscii = ""   

        FullChar = ""   

        for i2 = 1 to Len(dispName)   

            EachAsc = Asc(mid(dispName,i2,1))   

            EachChar = Chr(EachAsc)   

            FullAscii = FullAscii & EachAsc   

            FullChar = FullChar & EachChar   

        next       

        FullDisplayName = FullChar   

        tfelog.WriteLine TotalCount & ";" & objChild.cn & ";" & valNormal & ";" & dtmDate  & ";" & CountFromTodayLogon & ";" & CountGT21Logon  & ";" & CountGT21AndEnabled & ";" & "1" & ";" & objchild.distinguishedName & ";" & ArrayOUs(iOUCnt)& ";" & objchild.OperatingSystem & ";" & objchild.OperatingSystemServicePack    

        strMsExchMastSID = ""       

        count = count + 1   

        TotalCount = TotalCount + 1   

    Next   

    'tfelog.WriteLine objChild.samAccountName & ":" & objChild.AccountDisabled & ":" & objChild.PasswordExpires & ":" & strCanoniPath & ":"   

    'count = count + 1   

    'tfelog.WriteLine(count-1)   

    'TotalCount = TotalCount + 1   

Next   

'Next   



tfelog.WriteLine("end")   

tfelog.WriteLine(TotalCount-1)   

tfelog.close   

WScript.echo "End"  
Windows for business Windows Client for IT Pros Directory services Active Directory
{count} votes

2 answers

Sort by: Most helpful
  1. Khaleda Parvin Fahmida (External) 6 Reputation points
    2022-08-28T09:53:04.98+00:00
    		tfelog.WriteLine TotalCount & ";" & objChild.cn & ";" & valNormal & ";" & dtmDate  & ";" & CountFromTodayLogon & ";" & CountGT21Logon  & ";" & CountGT21AndEnabled & ";" & "1" & ";" & objchild.distinguishedName & ";" & ArrayOUs(iOUCnt)& ";" & objchild.OperatingSystem & ";" & objchild.OperatingSystemServicePack 
    
    1 person found this answer helpful.

  2. Gary Reynolds 9,621 Reputation points
    2022-08-29T10:18:02.36+00:00

    Hi,

    The error is probably because the OperatingSystem attribute is not set on the object. The simplest option is to set the OperatingSystem value of the object to a variable, and use the On Error Resume Next to handle any errors.

    OperatingSystem = ""  
    On Error Resume Next  
    OperatingSystem = objchild.OperatingSystem  
          
    tfelog.WriteLine OperatingSystem  
    

    Gary.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.