1

I'm trying to get the last password date on vbs. I'm using this code

On Error Resume Next
Wscript.Echo Day(now) & "/" & Month(Now) & "/" & Year(Now)
strComputer = "."
Set objWMIService= GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery  ("Select * from Win32_UserAccount Where LocalAccount = True")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("D:\users.txt", True)

For Each objItem in colItems
    Do While True
    if objItem.Name= "Administrator" then Exit Do
    if objItem.Name= "Guest" then Exit Do
    if objItem.Name= "Admin" then Exit Do
    if objItem.PasswordChangeable= "False" then Exit Do
    'Wscript.Echo "Name: " & objItem.Name
    objFile.Write objItem.Name & vbCrlf
    objFile.Write objItem.PasswordChangeable & vbCrlf

         Exit Do
    Loop
Next

But I can't acces to this data,

aynber
  • 22,380
  • 8
  • 50
  • 63
  • @user692942 Yes, it does serve a purpose. The OP is filtering the user list and attempting to skip system users. The script should only return real users. But the syntax isn't correct and this is not the best approach. – Nilpo Oct 03 '22 at 08:54
  • Just exclude them in the WMI query first. – user692942 Oct 03 '22 at 09:45

1 Answers1

1

There are two ways to go about this. It seems like you are attempting to filter or exclude some of the results of your WMI query by looping over them and passing through a bunch of conditionals. This is what it should look like.

On Error Resume Next
WScript.Echo Day(Now) & "/" & Month(Now) & "/" & Year(Now)

strComputer = "."
Set objWMIService = GetObject("winmgmts:" &_
    "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_UserAccount")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("D:\users.txt", True)

For Each objItem in colItems
    If objItem.Name = "Administrator" Then Continue
    If objItem.Name = "Guest" Then Continue
    If objItem.Name = "Admin" Then Continue
    If objItem.PasswordChangeable = "False" Then Continue
    'WScript.Echo "Name: " & objItem.Name
    objFile.WriteLine(objItem.Name)
    objFile.WriteLine(objItem.PasswordChangeable)
Next

objFile.Close
Set objFSO = Nothing
Set colItems = Nothing
Set objWMIService = Nothing

However, you're doing a lot of extra work here. If you really want to grab an entire WMI class, you should really skip the ExecQuery and just grab the entire class from the start. That would look something like this.

On Error Resume Next
WScript.Echo Day(Now) & "/" & Month(Now) & "/" & Year(Now)

strComputer = "."
Set colUsers = GetObject("winmgmts:" &_
    "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2:Win32_UserAccount")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("D:\users.txt", True)

For Each objUser in colUsers
    If objUser.Name = "Administrator" Then Continue
    If objUser.Name = "Guest" Then Continue
    If objUser.Name = "Admin" Then Continue
    If objUser.PasswordChangeable = "False" Then Continue
    'WScript.Echo "Name: " & objUser.Name
    objFile.WriteLine(objUser.Name)
    objFile.WriteLine(objItem.PasswordChangeable)
Next

objFile.Close
Set objFSO = Nothing
Set colUsers = Nothing
Set objWMIService = Nothing

As you can see, you're saving some keystrokes and maybe a few CPU cycles, but you're still making WMI give you the entire class full of data you don't want. This is where the query really comes into play. You should use the query to limit the result set to only the information that you are looking for.

On Error Resume Next
WScript.Echo Day(Now) & "/" & Month(Now) & "/" & Year(Now)

strComputer = "."
Set objWMIService = GetObject("winmgmts:" &_
    "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery( _
    "Select * from Win32_UserAccount " &_
        "Where LocalAccount = True " &_
            "And PasswordChangeable = True " &_
            "And Name <> 'Administrator' " &_
            "And Name <> 'Guest' " &_
            "And Name <> 'Admin'")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("D:\users.txt", True)

For Each objItem in colItems
    'WScript.Echo "Name: " & objItem.Name
    objFile.WriteLine(objItem.Name)
    objFile.WriteLine(objItem.PasswordChangeable)
Next

objFile.Close
Set objFSO = Nothing
Set colItems = Nothing
Set objWMIService = Nothing

This is a much better approach and you're letting WMI handle the heavy lifting instead of the script interpreter. Also, you should be taking advantage of the File object's WriteLine method. It handles line endings for you.

Nilpo
  • 4,675
  • 1
  • 25
  • 39