Re: Changing the local admin password base on the computer's OU




"Myrddin" <MyrddinMT@xxxxxxxxx> wrote in message
news:58d2752d-39a5-47cf-b9cd-8878be894b3f@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hello, I've been banging my head against the wall on this issue, hope
somone can help me out.
I have computers spread on several physical sites across the globe,
with a different OU for each site. I need to be able to change the
local admin password on each computer, depending on which OU it is a
part of.
The intent is to put thsi script in a GPO that runs everytime the
computer starts up, allowing us to cahnge local admin passwords pretty
easily.

Our AD is setup so that we have an OU for each site, then an OU for
computers in that site, then different OU's based on the department
the computer is part of (eg Real time 3D, Modeling, etc.)

This is what I've tried :

On Error Resume Next

' //////////////////////////////////////////////////////////////
' ///////////////////////// VARIABLES //////////////////////////
' //////////////////////////////////////////////////////////////

Dim WshNetwork, Sh, fso, WSHShell
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set Sh = WScript.CreateObject("WScript.Shell")
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

ComputerName = WshNetwork.ComputerName

Set oGroup_TEST1 = GetObject("LDAP://CN="; & ComputerName &
",OU=TEST1,OU=Computers,OU=Modeling,DC=<domain>,DC=xx")
Set oGroup_TEST2 = GetObject("LDAP://CN="; & ComputerName &
",OU=TEST2,OU=Computers,OU=3D,DC=<domain>,DC=xx")

'Change the local admin pwd for OU TEST1
If oGroup_TEST1.IsMember("WinNT://<domain>/" & ComputerName) =
true Then
Set Shell = Wscript.CreateObject ("Wscript.Shell" )
strComputer = Shell.ExpandEnvironmentStrings ("%COMPUTERNAME
%" )
Set objUser = GetObject("WinNT://" & strComputer & "/
Administrator, user" )
objUser.SetPassword "TEST1"
objUser.SetInfo
End If

'Change the local admin pwd for OU TEST2
If oGroup_TEST2.IsMember("WinNT://<domain>/" & ComputerName) =
true Then
Set Shell = Wscript.CreateObject ("Wscript.Shell" )
strComputer = Shell.ExpandEnvironmentStrings ("%COMPUTERNAME
%" )
Set objUser = GetObject("WinNT://" & strComputer & "/
Administrator, user" )
objUser.SetPassword "TEST2"
objUser.SetInfo
End If

This doesn't seem to work though : no matter how many IF conditions I
set, the last one is always applied. i added a simple Echo telling me
which password was set and I could see that the password was actually
first changed to TEST1, then to TEST2 : it's as if the IF conditions
don't matter at all.

I'm not sure if the problem lies within my IF conditions or from the
GetObject, but I'm totally lost here.
i had a previous version of this script that instead made a check on
the computer's IP address to check with subnet it was part of
(different subnets for different sites) but we've decided to change
that in favor of an OU membership check.

Any help would be massively appreciated.

My Recommendations:

1. Do not use "On Error Resume Next". It makes troubleshooting nearly
impossible. I suspect your If (and End If) statements are ignored and the
code within each is running.

2. Your script appears to check for group membership. This will work if you
have one group for each OU, but it requires you to be sure that all
computers in the OU are members of the group. That seems more work than
necessary, and subject to error.

3. When you seem to bind to the groups, you are actually binding to the
computer objects, not a group. I would expect errors, but you don't see them
because of "On Error Resume Next". I suspect you do not have the required
groups.

4. When you check group membership you pass a WinNT provider ADsPath to an
object bound with the LDAP provider. That cannot work. I think all of these
If statements are being skipped.

5. The only reliable way to check which OU an object is in is to bind to the
object and use the Parent method to retrieve the ADsPath of the parent
OU/Container. Then you can compare the Distinguished Name of the Parent
object with the full Distinguished Name of the OU. Most any other method can
fail in certain conditions.

6. I see no reason to retrieve the value of the environment variable
%COMPUTERNAME%. This is the NetBIOS name of the computer, but so is
wshNetwork.ComputerName. When you bind to the group/computer objects you
assume that the common name of the computer is the same as the NetBIOS name.
This does not have to be, but probably is.

7. Finally, computer passwords can be changed in a Startup script, but there
are two potential downsides. First, it will run repeatedly. Second, you
never know when the password has been changed. I prefer changing computer
passwords remotely myself from my computer. This requires a bit more code,
and requires the computers to be running, but it can be coded so you can run
it repeatedly only on the computers that have not yet gotten the change
applied.

I'll post suggested code shortly, when I get a chance.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


.



Relevant Pages

  • Re: AD group logon script question
    ... The OU is the parent container of the user object, ... It is straightforward to test for group membership. ... the logon script should check group membership to decide ... > objUser.Put "primaryGroupID", intPrimaryGroupToken ...
    (microsoft.public.scripting.vbscript)
  • Re: AD group logon script question
    ... In most cases the "convenience" refers to ease of administration. ... Hence Richard's point that the flexibility of group membership makes it more ... not a parent ... could simulate it in script, but you cannot permit a resource to an OU. ...
    (microsoft.public.scripting.vbscript)
  • Re: Login Script Help
    ... They are currently attempting to implement a login script ... the default printer based on group membership. ... group membership and then only install the printers as per the ... Set objGroup1 = ...
    (microsoft.public.windows.server.active_directory)
  • Re: AD group logon script question
    ... > logon scripts linked on this page: ... Group membership can be easily modified. ... > The purpose of OU's is to group objects for your own convenience, ... the logon script should check group membership to ...
    (microsoft.public.scripting.vbscript)
  • RE: Changing local admin PW using vb logon script - can it be encrypted?
    ... > True enough, but to quote a tall, hairy dewd I've worked with ... > will only buy you 5 minutes while they search for the script ... I missed the tall hairy "dewd" reference, and I agree that security must ... local admin ought to have more protection than that. ...
    (Focus-Microsoft)