Re: Using WMI to disable a user account

Tech-Archive recommends: Fix windows errors by optimizing your registry




"Paul" <mail@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:B71B475B-AA52-414B-9E70-30407277BCD5@xxxxxxxxxxxxxxxx
Hi,

I want to use a WMI script to disable a user account. Here's the test code
I'm using:

compName = "localhost"
Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!//"& _
compName & "\root\cimv2")
Set users = wmi.ExecQuery("SELECT * FROM Win32_UserAccount")
For Each user In users
If user.Name = "whatever" Then
user.Disabled = True
End If
Next 'user

The code runs fine (on a Windows Server 2003 box; I'm logged in as
Administrator), but the user's account is not disabled. Any idea why?

Interesting. I had the same result on XP with a non-admin account, so it's
not just that you are testing against a name where the (upper/lower) case
doesn't match. I added some echo statements to make sure the .disabled
attribute has a boolean value and is modifiable, i.e.:

compName = "localhost"
Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!//"&
_
compName & "\root\cimv2")
Set users = wmi.ExecQuery("SELECT * FROM Win32_UserAccount")
For Each user In users
If user.Name = "zzz" Then
wscript.echo "disabling user [" & user.name & "]"
wscript.echo "[" & user.disabled & "]"
user.Disabled = not user.disabled
wscript.echo "[" & user.disabled & "]"
End If
Next 'user
wscript.echo "after FOR EACH loop"

Since this indicated that the attribute had indeed changed (although not on
the actual account), it seems to me that there might perhaps be some
requirement to write the property back to the sam database with something
like the ".SetInfo" method used in active directory. There might be, but it
isn't user.setinfo, as that threw an "object doesn't support this property
or method" exception.

Curious.

/Al


.



Relevant Pages

  • Re: Using WMI to disable a user account
    ... I want to use a WMI script to disable a user account. ... compName = "localhost" ... but the user's account is not disabled. ...
    (microsoft.public.windows.server.scripting)
  • Re: ssh tunnel problems
    ... to an account on my ISP's computer? ... but the other use of "localhost" is AFAIK correct). ... port forwarding for a reverse tunnel. ... firewall that is blocking the use of incoming ssh from the home computer ...
    (Fedora)
  • Re: ssh tunnel problems
    ... to an account on my ISP's computer? ... but the other use of "localhost" is AFAIK correct). ... port forwarding for a reverse tunnel. ... firewall that is blocking the use of incoming ssh from the home computer ...
    (Fedora)
  • Re: Mail Recieving and sending problem?
    ... Localhost is not a correct account name. ... This error is usually caused by scanning e-mail, ...
    (microsoft.public.windows.inetexplorer.ie6_outlookexpress)
  • Re: Not trying to be a pest, but dont understand some threads
    ... I also just unchecked "include the selected account (localhost) in ... this group and created a new group only including selected account ... smtp.mail.yahoo.co.uk under the smtp server. ... Outlook, I was able to send out emails through my yahoo.co.uk POP ...
    (microsoft.public.outlook.installation)