Page 1 of 1 1
Topic Options
#131873 - 2004-12-30 02:02 PM FYI - Useful pieces of code...
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
Had some problems with netdom.exe. Not with the exe but the way I wanted to start it.

Code:

$domain ='Domain'
$password='Password'
$user='user'
$JOINDOMAIN = 1
$null ="NULL"
$objNetwork = CreateObject("WScript.Network")
$strComputer = $objNetwork.ComputerName
$objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" + $strComputer + "\root\cimv2:Win32_ComputerSystem.Name='" +$strComputer+ "'")
$ReturnValue = $objComputer.JoinDomainOrWorkGroup($Domain, $password, $Domain + "\" + $user, $NULL, $JOINDOMAIN)



Also had some problems with Net localgroup and long groupnames.

Code:


$names = "domain\user", "domain\domain admins", "domain\a_group_with_a_very_long_name"; etc.
For Each $name In $names
;Shell '%ComSpec% /c net localGroup Administrators "'+$name+'" /Add'
;?@ERROR + " | " + @SERROR

;If @ERROR = 1
$name=Split($name,'\')
$objGroup = GetObject("WinNT://" + @wksta + "/" + "Administrators")
$objGroup.Add ("WinNT://"+ $name[0] + "/" + $name[1])
?@ERROR + " | " + @SERROR
EndIf
Next




Both pieces of code solved my problems. Didn't find something like this at KORG. Thought it may be useful to let you know...

Happy new year!


Edited by Co (2004-12-30 09:12 PM)
_________________________
Co


Top
#131874 - 2004-12-30 02:27 PM Re: FYI - Useful peaces of code...
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
2 things...

1: it is pieces (not peace)
2: you rule


BTW.. have the code handy for moving computer to specific OU? What about renaming the computer?
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#131875 - 2004-12-30 07:35 PM Re: FYI - Useful peaces of code...
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
Oops.. hmmm OK pieces... Well, it sounds the same

{edit} peaces -> pieces {/edit}


Edited by Co (2004-12-30 09:14 PM)
_________________________
Co


Top
#131876 - 2004-12-30 07:39 PM Re: FYI - Useful peaces of code...
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/joindomainorworkgroup_method_in_class_win32_computersystem.asp
_________________________
Co


Top
#131877 - 2004-12-30 08:16 PM Re: FYI - Useful peaces of code...
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hmm...
that unsecure join sounds nice...
_________________________
!

download KiXnet

Top
#131878 - 2005-01-07 08:32 PM Re: FYI - Useful peaces of code...
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
DO you have a way of verifying the username and password is correct prior to attempting the join?

I'm doing this:
$objLocator = CreateObject('WbemScripting.SWbemLocator')
$objWBEM=$objLocator.ConnectServer($DC,'root\CIMV2',$DomainBox.text+'\'+$AdminBox.text,$PasswordBox.text)
if not @error

But I have to specify a PC ($DC) to connect to... is there a 'generic method?
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#131879 - 2005-01-07 09:02 PM Re: FYI - Useful pieces of code...
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
Code:
 



Break On
$System = CreateObject("Kixtart.System")

;KD START

;************* Form **************
$Form = $System.Form()
$Form.BackColor = 212,208,200
$Form.Height = 346
$Form.Left = 5
$Form.MaximizeBox = "False"
$Form.MinimizeBox = "False"
$Form.Text = "JoinDomain"
$Form.Top = 22
$Form.Width = 510
;**************************************

;************* Label1 **************
$Label1 = $Form.Controls.Label("Computer Name", 15, 120, 96, 22)
;**************************************

;************* Label5 **************
$Label5 = $Form.Controls.Label("Accounts to Add", 15, 180, 100, 23)
;**************************************

;************* Label6 **************
$Label6 = $Form.Controls.Label("Add to which OU", 15, 150, 100, 23)
;**************************************

;************* ComputerBox **************
$ComputerBox = $Form.Controls.TextBox("@wksta", 135, 120, 355, 20)
;**************************************

;************* OUCombo **************
$OUCombo = $Form.Controls.ComboBox("", 135, 150, 355, 21)
$OUCombo.DropDownWidth = 355
$OUCombo.Sorted = "True"
;**************************************

;************* AccountsBox **************
$AccountsBox = $Form.Controls.TextBox("", 135, 180, 355, 20)
;**************************************

;************* StartButton **************
$StartButton = $Form.Controls.Button("Start", 15, 225, 95, 82)
$StartButton.OnClick = "StartClick()"
;**************************************

;************* StatusBox **************
$StatusBox = $Form.Controls.ListBox("ListBox1", 135, 225, 355, 82)
;**************************************

;************* DomainGroup **************
$DomainGroup = $Form.Controls.GroupBox("Connect to Domain", 15, 15, 472, 79)
;**************************************

;************* Label7 **************
$Label7 = $DomainGroup.Controls.Label("Domain", 15, 20, 98, 18)
;**************************************

;************* Label8 **************
$Label8 = $DomainGroup.Controls.Label("Domain Account", 135, 20, 99, 15)
;**************************************

;************* Label9 **************
$Label9 = $DomainGroup.Controls.Label("Domain Password", 255, 20, 98, 16)
;**************************************

;************* DomainBox **************
$DomainBox = $DomainGroup.Controls.TextBox("", 15, 45, 100, 20)
;**************************************

;************* AdminBox **************
$AdminBox = $DomainGroup.Controls.TextBox("", 135, 45, 100, 20)
;**************************************

;************* PasswordBox **************
$PasswordBox = $DomainGroup.Controls.TextBox("", 255, 45, 100, 20)
$PasswordBox.PasswordChar = "*"
;**************************************

;************* ConnectButton **************
$ConnectButton = $DomainGroup.Controls.Button("Connect", 375, 20, 85, 18)
$ConnectButton.OnClick = "VerifyDomain()"
;**************************************

;************* ConnectStatusBox **************
$ConnectStatusBox = $DomainGroup.Controls.TextBox("Not Connected", 375, 45, 83, 20)
$ConnectStatusBox.BorderStyle = 1
$ConnectStatusBox.ReadOnly = "True"
$ConnectStatusBox.TextAlign = 2
;**************************************


;KD END

;make an array of your computer OUs in $arrOUs.. LDAP format
$arrOUs =

for each $item in $arrOUs
$OUs=$item
$OUCombo.additem($OUs)
next
$DC = "domainPC"
$DomainBox.Text = "domain"
$AdminBox.Text = ""
$PasswordBox.Text = ""
$OUCombo.Enabled = "False"
$StartButton.Enabled = "False"
$AccountsBox.Enabled = "False"
$ComputerBox.Enabled = "False"


$Form.Show
While $Form.Visible
$=Execute($Form.DoEvents())
Loop
Exit 1


Function VerifyDomain
$objLocator = CreateObject('WbemScripting.SWbemLocator')
$objWBEM=$objLocator.ConnectServer($DC,'root\CIMV2',$DomainBox.text+'\'+$AdminBox.text,$PasswordBox.text)
if not @error
$ConnectStatusBox.Text = "Connected"
$AccountsBox.Text = 'Domain Admins, ITStaff, '+$AdminBox.Text
$ConnectButton.Enabled = "False"
$AdminBox.Enabled = "False"
$PasswordBox.Enabled = "False"
$DomainBox.Enabled = "False"
$OUCombo.Enabled = "True"
$StartButton.Enabled = "True"
$AccountsBox.Enabled = "True"
$ComputerBox.Enabled = "True"
$ComputerBox.Setfocus
endif
Endfunction


Function StartClick
$newName = $ComputerBox.text
$domain = $DomainBox.text
$password = $PasswordBox.text
$user = $AdminBox.text
$OU = $OUCombo.text
$JOINType = 1 + 2 + 32

Status("Joining")
$objNetwork = CreateObject("WScript.Network")
if not @error
$strComputer = $objNetwork.ComputerName
$objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\"+$strComputer+"\root\cimv2:Win32_ComputerSystem.Name='"+$strComputer+"'")
$ReturnValue = $objComputer.JoinDomainOrWorkGroup($Domain, $password, $Domain+"\"+$user, $OU, $JOINtype)
If $ReturnValue
Status("Failed joining "+@wksta+" to "+$domain)
Status($ReturnValue)
return
EndIf
sleep 20

Status("Renaming")
$objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $strComputer + "\root\cimv2")
$colComputers = $objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each $objComputer in $colComputers
$err = $objComputer.Rename($NewName, $password, $Domain + "\" + $user)
If $err
Status("Failed Renaming "+@wksta+" to "+$NewName)
Status($err)
endif
Next

For Each $name In split($AccountsBox.text,',')
$name=trim($name)
Status("Adding User "+$name)
if not $name="Domain Admins"
$objGroup = GetObject('WinNT://' + @wksta + '/' + 'Administrators')
$objGroup.Add ('WinNT://'+ $domain + '/' + $name)
if @error
Status(@serror +' error adding '+$name)
EndIF
endif
Next
Status("Complete - Restart PC")
endif
endFunction


FUNCTION Status($text)
$StatusBox.additem($text)
$StatusBox.listindex=$StatusBox.listcount-1
EndFunction




Edited by Radimus (2005-01-10 03:00 PM)
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#131880 - 2005-01-10 03:07 PM Re: FYI - Useful pieces of code...
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
http://www.wheelerfam.com/kixtart/JoinDomain.kix

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#131881 - 2005-01-10 08:51 PM Re: FYI - Useful pieces of code...
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
Radimus,

I use the code in a configuration script which i use after a ghost image. I start the script with a form. My co-workers use this form to fillin all usernames and passwords which are needed during the script running.
Another department adds the computers to the domain. So I only have to join the computers to the domain...

I like your like script. Very nice...
_________________________
Co


Top
#131882 - 2005-01-11 03:33 AM Re: FYI - Useful pieces of code...
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
I used an older version of this based on netdom to do my win2k images.

The machines would run minisetup after the ghost image is installed, and minisetup would autoname and skip joining the domainso it runs unattended... after it completes and reboots with autoadminlogon, this form starts (or the older version) where it can be renamed, joined to domain and user accounts can be added to local admin
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 669 anonymous users online.
Newest Members
Audio, Hoschi, Comet, rrosell, PatrickPinto
17880 Registered Users

Generated in 0.062 seconds in which 0.025 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org