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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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
sirbountyCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
changjiaAuthor Commented:
works great! Thanks Sirbounty!!!
0
sirbountyCommented:
Happy to help - thanx for the grade! :^)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.