VBscript help

Hi Experts:

I have this code that lists local user account information. Could someone help me modify the code so it writes the output to a txt file?

Thanks

On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_Group  Where LocalAccount = True")
For Each objItem in colItems
    Wscript.Echo "Caption: " & objItem.Caption
    Wscript.Echo "Description: " & objItem.Description
    Wscript.Echo "Domain: " & objItem.Domain
    Wscript.Echo "Local Account: " & objItem.LocalAccount
    Wscript.Echo "Name: " & objItem.Name
    Wscript.Echo "SID: " & objItem.SID
    Wscript.Echo "SID Type: " & objItem.SIDType
    Wscript.Echo "Status: " & objItem.Status
    Wscript.Echo
Next
changjiaAsked:
Who is Participating?
 
sirbountyConnect With a Mentor Commented:
Sorry about that  - this should do it for you...
OutputFile = "C:\Output.txt"
 
Dim objNet : Set objNet = CreateObject("Wscript.Network")
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell : Set objShell = CreateObject("Wscript.Shell")
Dim objOut : Set objOut = objFSO.CreateTextFile(OutputFile)
 
strPC = objNet.Computername
 
Dim colAccts : Set colAccts = GetObject("WinNT://" & strPC)
colAccts.Filter = Array("user")
 
For Each strAcct in colAccts
  Dim objExec : Set objExec = objShell.Exec ("net user " & strAcct.Name)
  objOut.WriteLine objExec.StdOut.ReadAll
  objOut.WriteLine "===================="
  objOut.WriteLine 
Next
 
objOut.Close

Open in new window

0
 
sirbountyCommented:

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOut : Set objOut = objFSO.CreateTextFile("C:\Output.txt")
 
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_Group  Where LocalAccount = True")
For Each objItem in colItems
    objOut.WriteLine "Caption: " & objItem.Caption
    objOut.WriteLine "Description: " & objItem.Description
    objOut.WriteLine "Domain: " & objItem.Domain
    objOut.WriteLine "Local Account: " & objItem.LocalAccount
    objOut.WriteLine "Name: " & objItem.Name
    objOut.WriteLine "SID: " & objItem.SID
    objOut.WriteLine "SID Type: " & objItem.SIDType
    objOut.WriteLine "Status: " & objItem.Status
    objOut.WriteLine
Next
 
objOut.Close

Open in new window

0
 
changjiaAuthor Commented:
Hi Sirbounty:

Thanks for helping again.
The output file list all the local user groups instead user attributes. Is there a way to modify the script to output all the local user accounts?

Thanks
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
sirbountyCommented:
What is it you're trying to accomplish?

Something like this should list the local users:
Dim objNet : Set objNet = CreateObject("Wscript.Network")
strPC = objNet.Computername
 
Dim colAccts : Set colAccts = GetObject("WinNT://" & strPC)
colAccts.Filter = Array("user")
 
For Each strAcct in colAccts
  wscript.echo strAcct.Name & " properties:"
  Dim objUser : Set objUser = GetObject(strAcct.ADsPath)
  wscript.echo vbTab & objUser.givenName & " " & objUser.sn
  wscript.echo vbTab & objUser.CN
Next

Open in new window

0
 
changjiaAuthor Commented:
Hi Sirbounty:

I really need some script that shows what net user command shows. Please look below for an example.

C:\>net user testuser
User name                    testuser
Full Name                    test
Comment
User's comment
Country code                 000 (System Default)
Account active               No
Account expires              Never

Password last set            11/6/2008 11:49 AM
Password expires             2/4/2009 11:49 AM
Password changeable          11/7/2008 11:49 AM
Password required            Yes
User may change password     Yes

Workstations allowed         All
Logon script
User profile
Home directory
Last logon                   Never

Logon hours allowed          All

Local Group Memberships      *Users
Global Group memberships     *None
The command completed successfully.


The above command is from a net user in dos, can VB script do the same but to list the similar reports for all user accounts on local machine?

Thanks
0
 
sirbountyCommented:
Something like this work for you?
Dim objNet : Set objNet = CreateObject("Wscript.Network")
Dim objShell : Set objShell = CreateObject("Wscript.Shell")
strPC = objNet.Computername
 
Dim colAccts : Set colAccts = GetObject("WinNT://" & strPC)
colAccts.Filter = Array("user")
 
For Each strAcct in colAccts
  Dim objExec : Set objExec = objShell.Exec ("net user " & strAcct)
  wscript.echo objExec.StdOut.ReadAll
  wscript.echo
  Dim objUser : Set objUser = GetObject(strAcct.ADsPath)
  wscript.echo vbTab & objUser.givenName & " " & objUser.sn
  wscript.echo "=================="
Next

Open in new window

0
 
changjiaAuthor Commented:
I got error message when excute the script

LINE:9
Char:17
Error: Wrong number of arguments or invalid property assighment
Code: 800A01C2
Source: Microsoft VBScript runtime error

Thanks
0
 
changjiaAuthor Commented:
works great! Thanks Sirbounty!!!
0
 
sirbountyCommented:
Happy to help - thanx for the grade! :^)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.