Solved

WMI Script to change the password for local user account

Posted on 2004-08-18
8
2,729 Views
Last Modified: 2012-06-27
I need a WMI script to change a password for local user account. (Will be running this script against multiple computers at the same time)
- servers are Not active directory
- I want to accomplish this using WMI scripting
- Script output should indicate success/failure of the job
Appreciate any help.

I am a sysadmin with little expereince in WMI scripting. Tried Microsoft script fom technet, does not slove my purpose
0
Comment
Question by:skn73
  • 4
  • 2
  • 2
8 Comments
 
LVL 57

Expert Comment

by:Pete Long
ID: 11830484
0
 

Author Comment

by:skn73
ID: 11830956
For a VB Solution see http://www.experts-exchange.com/Security/Win_Security/Q_20946526.html
http://www.microsoft.com/technet/community/scriptcenter/user/scrug74.mspx

I tried this script before i posted my question. Did not solve my purpose


Program: ResetPW.EXE: Remote Password Change Utility
http://wettberg.home.texas.net/resetpw.htm

I want to use WMI scripting.

Thanks for your time in posting the comment though.
0
 
LVL 57

Expert Comment

by:Pete Long
ID: 11831572
OK Sorry I couldnt be more help - good luck

if you get a working solution post back and let us know :)

best wishes
Pete
0
 
LVL 6

Expert Comment

by:Scott_Willcocks
ID: 11839367
changes local administrator if you know the group exchange for group you would like to change

strComputer = "MyComputer"
Set objUser = GetObject("WinNT://" & strComputer & "/Administrator, user")
objUser.SetPassword "testpassword"
objUser.SetInfo

this link gives lots of user scripts for wmi

http://www.microsoft.com/technet/community/scriptcenter/user/default.mspx
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:skn73
ID: 11852516
Sorry Scoot, We wrote the script ourselves My coworkers helped me.
 Here is the script that worked.

' Description: Accepts entry of username and password and changes the password for that
'              username on the supplied list of computers.
' Requirements: none
' Creation Date: 08-17-2004
' File Type: vbs
' Version:      1.0 original release

Option Explicit

Dim objFSO 'file system object

Dim WshShell 'windows scripting host shell

Dim objTextFile 'text file object

Dim objUser 'user object

Dim strUserName 'user name to modify

Dim strPassword 'new password to set

Dim arrComputers 'computer array from file

Dim strComputer 'single computer

Dim strFileName 'filename to read

Dim strLogFileName 'filename to write

Dim intCount 'counter

Const ForWriting = 2 'creates a new text file for writing

Const ForAppending = 8 'appends to a text file

strFileName = "computers.txt"

'change this to whatever you want to name the file
strLogFileName = "PasswordChangeResults.csv"

'Shell object
Set WshShell = WScript.CreateObject("WScript.Shell")

'file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")

'puts the text file in the same directory where the script is executed
Set objTextFile = objFSO.OpenTextFile (WshShell.CurrentDirectory & "\" & strLogFileName, ForWriting, True)

objTextFile.writeline "Computer,Status"

'get the username
strUserName = inputbox ("Enter username for password change","Username Entry")

'get the password
strPassword = inputbox ("Enter new password","Password Entry")

If (strUserName = "" Or strPassword = "") Then

      msgbox "Username and password must contain data.  Please run the script again."
      
      wscript.quit
      
End If

'read text file into an array skipping blanks
arrComputers = GetTextFileLines(strFileName,True)

'go through each item in the array
For intCount = 0 To ubound (arrComputers)

      strComputer = trim(arrComputers(intcount))

      'ping the computer first
      If (ping(strComputer)=True) Then      
      
            'get the WMI object
            On Error Resume Next

            err.clear
            
            Set objUser = GetObject("WinNT://" & strComputer & "/" & strUserName & ",user")

            If (err.number = 0) Then
                        
                  objUser.SetPassword strPassword

                  objUser.SetInfo
                  
                  If (err.number = 0) Then
                  
                        On Error goto 0
                        
                        objTextFile.writeline strComputer & "," & "Success"
                        
                  Else
                  
                        On Error goto 0
                  
                        objTextFile.writeline strComputer & "," & "Unable to set password, error number " & err.number

                  End If
      
                  
            Else
            
                  'no user exists or not access
                  On Error goto 0
                  
                  objTextFile.writeline strComputer & "," & "No user exists or you do not have access to " _
                        & "this user, Error number " & err.number
                  
                  err.clear
            
            End If

      Else
      
            'can't ping the machine
            objTextFile.writeline strComputer & "," & "No Ping Response"
      
      End If      

Next 'arrComputers

'cleanup and close

Set objUser = Nothing

objTextFile.Close

Set objFSO = Nothing

Set WshShell = Nothing

Set objTextFile = Nothing

msgbox "done"

' Description: This function pings a specified computer and returns true if ping is a success.
' Requirements: none
' Creation Date: 10-31-2002
' Author: from - http://chrislewis.blogspot.com/

Function Ping(strHost)
  Dim objShell 'shell object
  Dim objStdOut 'std out to read
  Dim strLine 'line to read
  Dim objRegExp 'expression to examine
  Dim strPattern 'pattern to look for
  Dim wshShell
 
  Set WshShell = CreateObject( "WScript.Shell" )


  Ping = False

  Set objShell = WshShell.Exec("PING.EXE " & strHost)  'The command line
  Set objStdOut = objShell.StdOut 'Reads the output from the command
  Set objRegExp = New RegExp
  objRegExp.IgnoreCase = True

  Do While Not objStdOut.AtEndOfStream
    strLine = objStdOut.ReadLine
    strLine = Trim(strLine)
    'Test for "Unknown Host" - DNS Failed
    objRegExp.Pattern = "^Unknown"
    If objRegExp.Test(strLine) Then
      Ping = False
      Exit Function
    End If

    'DNS Succeeded, but box off-line
    objRegExp.Pattern = "100% loss"
    If objRegExp.Test(strLine) Then
      Ping = False
      Exit Function
    End If

    'Got a Reply From,
    objRegExp.Pattern = "^Reply from"
    If objRegExp.Test(strLine) Then
      'Check for "Destination host unreachable."
      objRegExp.Pattern = "Destination host unreachable\."
      If objRegExp.Test(strLine) Then
        Ping = False
      Else
        'S/B OK here
        Ping = True
      End If
      Exit Function
    End If
  Loop

  Set objShell = Nothing
  Set objStdOut = Nothing
  Set objRegExp = Nothing
End Function

' ----------------------------------------------------------------
' Function: Read text file and load into array
' Description: Used to read a text file into an array.
' Example: GetTextFileLines("c:\temp\test.txt",True)
' The example reads the test.txt file in the c:\temp directory into an array without the blank lines.
' Requirements:
' Creation Date: 10-23-2003
' Author: Tim McGue inspired by - http://visualbasic.ittoolbox.com/code/d.asp?d=2255&a=s
' ----------------------------------------------------------------

Function GetTextFileLines(strFileName,bolDropEmpty) 'parameters: file name to read, boolean to drop blank lines

Dim objFSO 'file system object

Dim intCount 'counter

Dim objTextStream 'text stream object

Dim arrLinesRead 'array of the lines read from the text file

'set variables
Set objFSO = CreateObject("Scripting.FileSystemObject")

'ensure the file exists
If Not (objFSO.fileexists(strFileName)) Then

      msgbox "The file " & strFileName & " does not exist."
      
      Exit Function

End If

'Open a text stream
Set objTextStream = objFSO.OpenTextFile(strFileName)

'read the entire file
arrLinesRead = objTextStream.ReadAll

'split on CRLF
arrLinesRead = split(arrLinesRead,vbCrlf)

'go through each line in the array
For intCount = 0 To ubound(arrLinesRead)

      'trim each line
      arrLinesRead(intcount) = trim(arrLinesRead(intcount))
      
      'if the line is blank replace it with a CRLF character
      If (arrLinesRead(intcount) = "") Then
      
            arrLinesRead(intcount) = vbCrlf
      
      End if

Next

If (bolDropEmpty) Then 'need to remove blanks

      'cleanup blank lines on CRLF
      arrLinesRead = filter(arrLinesRead,vbCrlf,False)

End If

'return the array
GetTextFileLines = arrLinesRead

'cleanup
objTextStream.close

Set objFSO = Nothing

Set objTextStream = Nothing

End Function
0
 
LVL 6

Expert Comment

by:Scott_Willcocks
ID: 11852946
nice i am lazy why not query the domain and create a list of computers of your domain listing

   Set fso = CreateObject("Scripting.FileSystemObject")
   Set tsr = fso.OpenTextFile("COMPUTER.txt", 1) 'computers on the domain for reading
   Set tst = fso.OpenTextFile("COMPUTER.txt", 2) 'computers on the domain for writing to
   Set tss = fso.OpenTextFile("server.txt", 2) 'list of servers on the network for writing to

on error resume next
query domain for list of all computers ont the domain

sDomain       = "YOUR DOMAIN"
Set oDomain       = GetObject("WinNT://" & sDomain)
oDomain.Filter       = Array("Computer")
'filter out all servers on the domain the servers naming convention is mkc_ or mkc-
      For Each oADobject In oDomain
            if instr(oADobject.Name, "SERVERNAMAING CONVENTION" ) then
                  tss.write oADobject.Name
                  tss.write vbcrlf
                  
            elseif instr(oADobject.Name, "OTHERSERVERNAMING CONVENTION" ) then
                  tss.write oADobject.Name
                  tss.write vbcrlf
            
                  tss.write oADobject.Name
                  tss.write vbcrlf
            
            
            elseif instr(oADobject.Name, "PCNAMING CONVENTION" ) then
                  tst.write oADobject.Name
                    tsT.write vbcrlf
            
            end if
      next
      odomain = nothing

      tst.close
      tss.close  
                tso.close


the server naming convention to filter out any servers from the list
pcnaming is the list of pcs you want to include

for instance in pc naming all the PC in our domain are "XYZ" and a four digit number

so we capture the text file with all XYZ so add xyz where pc naming convention is

but we don't want to run this on servers so all servers have "XYZ"_digit or name
so we add "xyz_" in server naming and filter out those

also we have servers with "xyz-" as naming convention so we will filter those out to server text so we only update workstations

now you can get the script to query the domain and build a list of computers itself.

0
 
LVL 6

Expert Comment

by:Scott_Willcocks
ID: 11853023
but I would also amend the script to check to see if computer exists and is empty if empty run the domain query and populat the computer.txt then towards the end of the script remove any successful computer names from the computer.txt

so that it only queries the domain and does it once for a list of pc's

then when you run iot again it doesn't recreate the list of computers.

then I'd get it to run the script to ping and if succesfull change the password on that pc.

Then after it has done that remove the computer from the computer.text file so it doesn't ping or query for that pc again.

so you can keep on running that script until it has finnally done all the machines in the list. knowing that your not running it against the whole list again and again and you will know which machines it has run against and which need to be powered up.
0
 
LVL 6

Accepted Solution

by:
Scott_Willcocks earned 250 total points
ID: 11853127
we used a similar script to that to query the domain and build a list from wmi of all the dell serial numbers and what computer name the sersial number beleonged to for an audit as we had on our pc inventory all the computer names and who they belonged to but when leased the pc's only had serial numbers to identify which ones went back so when the leasing company said we want these serials we had to match a serial to computer name. An oversight on the managers part just list the PC's boy and get them on the desk top.

Oh that was fun as we have 4,000 plus pcs we has to automate it. hope your script works well the problem with the ping method was tyhe time it takes to run .

say you have 4000 pcs each ping is three requests each request takes say on average 60 miliseconds so each pc will need 1.5 seconds per request x 3 equals 4.5 seconds per machine to ping back plus the time it takes to run other parts of the script say half a second so 5 seconds per machine times 4000 so this would take 5.5 hours to run approx so i would filter out any successful attempts so it would only run the script on the machines that it needed.

don't make the same mistake we did :)

when we first wrote it we didn't filter out so thius script would run from begining of the day to the end for a few days before we decided to filter out. OOPPSSS

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
kerberos errors 7 544
Windows Explorer and WinZXip 4 496
IP Addresses List On Network 8 282
Cannot Upgrade Microsoft Installer on Windows 2000 29 57
NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

10 Experts available now in Live!

Get 1:1 Help Now