WSH VBScript newbie question

Hi,

I am trying to create a batch file that sets passwordExpires to false so a user's password will never expire.  I am able to enumerate thru the users, but I can't figure out how to set the passwordExpires.  This is my .vbs so far:

Dim userName
userName = "Trevor Hartman"

Dim users
Set users = GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("Win32_useraccount")
For Each user In users
      if user.Name = userName then
            user.PasswordExpires = 1
            WScript.Echo user.PasswordExpires & " - " & user.Name
      end if
Next

Set users = Nothing

This obviously doesn't set the user.Password.Expires = 1 as this is just an emueration...  What is the correct way to do it?

Thanks,
Trevor
LVL 8
trevorhartmanAsked:
Who is Participating?
 
kristinawConnect With a Mentor Commented:
trev,

what you have is pretty good. i pulled up some info on the win32_useraccount to see what other attributes were available. see the following:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_useraccount.asp

note the attribute that is 'localaccount'.

you could add some code that says if user.localaccount = true then
user.passwordexpires = 1
user.setinfo '(makes the change effective)

also, the code seems to enumerate the local user accounts first. you might just set the code to break out of the loop whenever you the first user.localaccount = false.

let me know if you want a more specific code example.

kris.

0
 
kristinawCommented:
Trev,

can you elaborate on what you're trying to do? you're just trying to set the pass on a local machine for a particular user to never expire? the reason i'm asking is the script would be written differently if you're trying to set the flag for domain accounts.

also, if the computer is a member of a domain, the way you have this written it will enumerate domain accts that have privelidges on the local machine as well.

kris.
0
 
trevorhartmanAuthor Commented:
Yes I'm trying to set the pass to never expire for a particular user on the local machine.  I don't want to include domain accounts.
-Trevor
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
kristinawCommented:
or, in your example above, once you get a match on that name you could break out of the loop and finish. otherwise it will try to loop through all the domain accts that have privelidge on the machine which would take QUITE some time.

kris.
0
 
trevorhartmanAuthor Commented:
this:

user.passwordexpires = 1

doesn't actcually set the user.passwordexpires to 1 because it only modifies the temperary user object that is created in the enumeration.  the changes don't reflect on the actual account.  what is the correct way to set these settings?

thanks,
Trevor
0
 
kristinawCommented:
Trevor,

that is what the 'user.setinfo' line is for that i added in the above example.

that WILL make the change permanent. try it and see.

kris.
0
 
trevorhartmanAuthor Commented:
aah, missed that line. sorry bout that.  thanks for your help kris

Trevor
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.