Solved

List User Accounts on Remote Machines and Change Account Password

Posted on 2009-05-07
18
1,008 Views
Last Modified: 2013-11-10
Hello,

We have an account "accountx" that is set up in the Users Group on a LOT of Windows servers.  This account is used to run Scheduled Tasks on these servers.  We have the need the need to change the password for "accountx".  

To do this, I need some utility that will take as input a list of computer names and find out if the account "accountx" is defined on that machine.....and if it is defined....then change the password for "accountx" to a given password.

Hope someone can help me. msyed1
0
Comment
Question by:msyed1
  • 8
  • 5
  • 2
  • +3
18 Comments
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 24327181
Have you thought of using an Active Directory Server? Then you would only have to change this on one machine.
0
 

Author Comment

by:msyed1
ID: 24327224
Definitely a great idea...but....at this time my need is to work with the existing environment.....msyed1
0
 
LVL 8

Assisted Solution

by:thetmanvn
thetmanvn earned 50 total points
ID: 24327308
Hi msyed1,

http://www.codeproject.com/KB/IP/PwdChange.aspx

Maybe this tool suit almost your needs.
0
 
LVL 15

Assisted Solution

by:veedar
veedar earned 50 total points
ID: 24332174
With psexec you can for example...

Show all accounts on remote machine polaris....
psexec \\polaris net user

Change the administrator password...
psexec \\polaris net user administrator somenewpassword

For detail run:  net help user
0
 
LVL 4

Accepted Solution

by:
daluu earned 300 total points
ID: 24342265
If you can or want to do this programmatically, you can use VBScript, etc. to read in the list of computers, and perform the password change following the code sample from this site. Assuming you know VBScript.

http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct04/hey1015.mspx
0
 

Author Comment

by:msyed1
ID: 24396148
I have been OOO.  Thank you for the suggestions.  I will need some more time to try out the suggestions and report back.
0
 

Author Comment

by:msyed1
ID: 24401000
Hello, I am using 'psexec \\computername net user'.  The problem I am having is that when I run the command manually from DOS, it does return all the user accounts that are defined on the machines, but when I run the same exact command I do not get the user accounts.  Please see below.  VB.net code is also attached.  Please let me know what I am doing wrong.  Also, is there another way to find out the user accounts defined on remote machines, besides using psexec ??

=====================================================================
This is what I see when I manually run 'psexec \\10.160.134.134 net user'
through the command line

C:\TaskRunnerAccountDiscovery>psexec \\10.160.134.134 net user
PsExec v1.94 - Execute processes remotely
Copyright (C) 2001-2008 Mark Russinovich
Sysinternals - www.sysinternals.com
User accounts for \\

---------------------------------------------------------------------
ASPNET                   dcuser                   Guest
IUSR_DZDCM301            IWAM_DZDCM301            nvtgpe
patrol                   SUPPORT_388945a0
The command completed with one or more errors.

net exited on 10.160.134.134 with error code 1.

C:\TaskRunnerAccountDiscovery>
========================================================================



=====================================================================
When I run the same exact command programatically from a batch file.... I don't receive
back the user account names.....this is all I receive....

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\msyed\My Documents\Visual Studio Projects\TaskRunnerAccountDiscovery\bin>psexec \\ net user

C:\Documents and Settings\msyed\My Documents\Visual Studio Projects\TaskRunnerAccountDiscovery\bin>exit

=======================================================================I  
    Public Function FindTaskRunnerAccountOnServer(ByVal strDomain As String) As String

        Dim myProcess As New System.Diagnostics.Process

        Dim s As String
 

        Dim strUserAccountInfo As String = "Not Available"

        Dim bolFoundTaskRunner As Boolean = False

        Dim startlocation, endlocation As Integer
 

        Try
 

            ''''''' Starting DOS Prompt

            myProcess.StartInfo.FileName = "cmd.exe"

            myProcess.StartInfo.UseShellExecute = False

            myProcess.StartInfo.CreateNoWindow = True

            myProcess.StartInfo.RedirectStandardInput = True

            myProcess.StartInfo.RedirectStandardOutput = True

            myProcess.StartInfo.RedirectStandardError = True
 

            myProcess.Start()
 

            Dim sIn As StreamWriter = myProcess.StandardInput

            Dim sOut As StreamReader = myProcess.StandardOutput

            Dim sErr As StreamReader = myProcess.StandardError
 

            sIn.AutoFlush = True
 

            log.Info("*****Now performing PSEXEC to display User Account Info for: " & strDomain)

            sIn.Write("psexec \\" & strDomain & " net user" & System.Environment.NewLine)

            sIn.Write("exit" & System.Environment.NewLine)
 

            '''''''' going into read mode - read the results of DOS command

            s = sOut.ReadToEnd()
 

            If Not myProcess.HasExited Then

                myProcess.Kill()

            End If
 

            sIn.Close()

            sOut.Close()

            sErr.Close()

            myProcess.Close()

            log.Info("Output of Read: " & s & vbCrLf)
 

            If s <> String.Empty Then

                strUserAccountInfo = s

            End If
 
 

            Return strUserAccountInfo
 

        Catch ex As Exception

            log.Error("Error occurred: " & ex.Message)

        End Try

    End Function

Open in new window

0
 
LVL 6

Assisted Solution

by:nettek0300
nettek0300 earned 100 total points
ID: 24401060
You could also try this using the pstools:

psexec \\computername cmd or psexec \\ipaddress cmd
net user

The first line simply opens a command line on the remote PC.  Net user then runs the command from the command line. To go one step further, you could put this into a batch file with the results going into a log file.  A second batch file could also be created to call the first file.

Batch file 1 would look something like this:
psexec \\computername cmd
net user > c:\file.log

Batch file 2 would look like this:
call filename computer1
call filename computer2

If you use the second batch file to run the script on all computers, you would need to substitute %1 for the computername so that it reads the information from batch file 2.  When you run this, you would actually run batch file 2 and it would then call batch file 1.  In batch file 2, the word filename is the actual name of the first batch file.  Both files need to be in the same directory to run correctly.
0
 

Author Comment

by:msyed1
ID: 24401141
nettek0300:  I am trying your first suggestion, but now for some reason when execution goes to the line:
 '''''''' going into read mode - read the results of DOS command
            s = sOut.ReadToEnd()
it has stayed at that statement (I am in DEBUG mode) for more than 1/2 hour.  Does ReadToEnd from DOS results take that long ??  I don't know if it is stuck or if it is working, or if it is slow or what...

I really can't do the 2nd option you suggested, because I am doing this in a VB.Net console program. and I don't know how I would run batch files from within the console program.  Also, I have an Arraylist of close to 120 servers that I need to go through in a loop....and find out all the user accounts set up on each computer.  msyed1.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:nettek0300
ID: 24401305
It should not take more than a couple seconds to connect.  I am not sure what the s=sOut... line means.  I am not that familiar with vb.net.  You can manually run the psexec command to get an idea of how quick it connects.  Maybe someone else can help you on this.
0
 
LVL 4

Expert Comment

by:daluu
ID: 24404241
You can also get a list of all user accounts using WMI + VB.NET. Unfortunately, there doesn't seem to be an available WMI method to change user passwords other than the VBScript option I posted earlier. Here's a code sample taken with MS WMI code creator. I tested with VBScript but here's the VB.NET version:
Imports System

Imports System.Management

Imports System.Windows.Forms
 

Namespace WMISample
 

    Public Class MyWMIQuery
 

        Public Overloads Shared Function Main() As Integer
 

            Try

                Dim searcher As New ManagementObjectSearcher( _

                    "root\CIMV2", _

                    "SELECT * FROM Win32_UserAccount") 
 

                For Each queryObj As ManagementObject in searcher.Get()
 

                    Console.WriteLine("-----------------------------------")

                    Console.WriteLine("Win32_UserAccount instance")

                    Console.WriteLine("-----------------------------------")

                    Console.WriteLine("Name: {0}", queryObj("Name"))

    		    Console.WriteLine("FullName: {0}", queryObj("FullName"))

		    Console.WriteLine("LocalAccount (true/false): {0}", queryObj("LocalAccount"))
 

                Next

            Catch err As ManagementException

                MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)

            End Try

        End Function

    End Class

End Namespace

Open in new window

0
 

Author Comment

by:msyed1
ID: 24415728
To all who are trying to help me with this:  

Is there something wrong with this code.  I am trying to run the command 'psexec \\computername net user' just the way you run it from the command line.... please check my workingdirectory, arguments etc.  This is a console program, when I run it from a batch file, it just sits at the following statement for more than 1/2 hour.  
log.Info("*****Now Reading Output of Command")
            s = myProcess.StandardOutput.ReadToEnd()

See attachement.

I hope one of you can see what I am doing wrong.  msyed1.
   



Public Function FindTaskRunnerAccountOnServer(ByVal strDomain As String) As String

        Dim myProcess As New System.Diagnostics.Process

        Dim s As String

        Dim strUserAccountInfo As String = "Not Available"
 

        Dim strNumbers As String = "0,1,2,3,4,5,6,7,8,9"

        Dim strSplitNumbers() As String = Split(strNumbers, ",")

        Dim i As Integer = 0
 

        'If this is an IP Address....bypass

        For i = 0 To strSplitNumbers.GetUpperBound(0)

            If strDomain.StartsWith(strSplitNumbers(i)) Then

                Return strUserAccountInfo

            End If

        Next
 

        Dim bolFoundTaskRunner As Boolean = False

        Dim startlocation, endlocation As Integer
 

        Try
 

            ''''''' Starting DOS Prompt

            myProcess.StartInfo.UseShellExecute = False

            myProcess.StartInfo.CreateNoWindow = True

            myProcess.StartInfo.RedirectStandardInput = True

            myProcess.StartInfo.RedirectStandardOutput = True

            myProcess.StartInfo.RedirectStandardError = True

            myProcess.EnableRaisingEvents = False
 

            myProcess.StartInfo.FileName = "C:\windows\system32\cmd.exe"

            myProcess.StartInfo.WorkingDirectory = "C:\TaskRunnerAccountDiscovery\"

            myProcess.StartInfo.Arguments = ("psexec \\" & strDomain & " cmd net user" & System.Environment.NewLine)
 

            log.Info("*****Now performing PSEXEC to display User Account Info for: " & strDomain)

            myProcess.Start()
 

            log.Info("*****Now Reading Output of Command")

            s = myProcess.StandardOutput.ReadToEnd()
 

            log.Info("*****Now going into WaitforExit()")

            myProcess.WaitForExit()
 

            log.Info("Output of Read: " & s & vbCrLf)
 

            If s <> String.Empty Then

                strUserAccountInfo = s

            End If
 

            Return strUserAccountInfo
 

        Catch ex As Exception

            log.Error("Error occurred: " & ex.Message)

        End Try

    End Function

Open in new window

DosScreen.doc
0
 

Author Comment

by:msyed1
ID: 24416038
daluu:  I have never done WMI applications before, not even .net Windows applications.  So far i can only do .net Web applications and now I am learning console applications.  I don't know how to even test the WMI example you sent.  
0
 
LVL 4

Expert Comment

by:daluu
ID: 24416142
Oh, sorry to hear that. The samples I provide may seem more complicated then. However, I believe WMI should work fine whether you build a console application or web application, that's the beauty of .NET. Though it is often easier to debug and use it with a console app since WMI is for Windows/IT administration.

I suggest WMI here since it provides a more integrated solution than using PSExec or the other PS tools. Though if you find PS tools easier to work with, then go with that.
0
 
LVL 4

Expert Comment

by:daluu
ID: 24416181
Oh, sorry, you're right I forgot to mention how to run the WMI VB.NET code sample. I believe you can simpy compile it into an EXE from the .NET command line tools or with VS.NET and it should be executable from the command line. It will simply query for all the users on the local system and print out the user names, etc. on stdout.

The code was generated from the free MS WMI code creator. The hard part then, is how to customize the code, for example, like how to query against any computer, especially if you are new to WMI (I am new to it as well). It's too bad there are more WMI samples for VBScript than for .NET (C# or VB.NET).
0
 

Author Comment

by:msyed1
ID: 24425952
daluu:

OK.  I have given up on trying to get user accounts by using psexec, because no matter how many different ways i tried, it gets stuck at myProcess.StandardOutput.ReadToEnd().  

I am now using WMI.  The code snippet I have below is what I am using.  What's happening with this code though....is that I get a huge output of ALL the user accounts of ALLl the machines in my domain.  

What I need to be able to do is to pass in a given computername to this routine and for this code to get me the user accounts ONLY for that machine.  How do I pass in the computername of the REMOTE computer that I want the user accounts to be listed ??  

WMI experts....HELP!  msyed1.




    Public Function FindAccountsOnServer(ByVal strDomain As String) As String
 

        Dim strUserAccountInfo As String = "Not Available"

        Dim strTextFileBldr As New System.Text.StringBuilder

        Dim arlTextFileServers As New ArrayList

    

        Try

            Dim searcher As New ManagementObjectSearcher( _

    "root\CIMV2", _

    "SELECT * FROM Win32_UserAccount")
 

            For Each queryObj As ManagementObject In searcher.Get()
 

                strTextFileBldr.Append(queryObj("Name"))

                strTextFileBldr.Append(",")

                strTextFileBldr.Append(queryObj("FullName"))

                strTextFileBldr.Append(",")

                strTextFileBldr.Append(queryObj("LocalAccount"))
 

            Next

        Catch err As ManagementException

            log.Error("Error occurred while querying for WMI data: " & err.Message)

        End Try
 
 

        arlTextFileServers.Add(strTextFileBldr.ToString())
 

        strUserAccountInfo = strTextFileBldr.ToString()
 

        log.Info("strTextFileBldr: " & strTextFileBldr.ToString() & vbCrLf)
 

        Return strUserAccountInfo
 

    End Function

Open in new window

0
 
LVL 4

Expert Comment

by:daluu
ID: 24427955
That's a good question, unfortunately, I'm only a novice with WMI, especially on .NET. For some reason, there's more VBScript samples than .NET. You might want to edit this question's categorization to some other topics like .NET, VB.NET, and WMI, for help with WMI. If you aren't allowed to do that, contact one of the Forum moderators at EE or EE support.
0
 

Author Comment

by:msyed1
ID: 24443798
I have gotten started on WMI using .vb.net, but am having problems.  I will open a new question for the WMi questions.  For now, thanks to all who assisted me.  msyed1.  
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Ransomware continues to be a growing problem for both personal and business users alike and Antivirus companies are still struggling to find a reliable way to protect you from this dangerous threat.
This paper addresses the security of Sennheiser DECT Contact Center and Office (CC&O) headsets. It describes the DECT security chain comprised of “Pairing”, “Per Call Authentication” and “Encryption”, which are all part of the standard DECT protocol.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now