Jump to content

*Figured It Out* Combining Two Scripts Into One


Recommended Posts

I have two scripts and I would like to combine them, the first script finds the Chassis Type of the computer, I would like to take the discovered chassis type in the first script and pass it to the second script which joins the computer to the domain, I hope to make this join Windows 7 computers to the proper OU of the domain, below is an example of the scripts

This script finds the chassis type

Dim $strComputer, $strChassis
Dim $objWMIService, $objChassis, $colChassis, $objItem
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$strComputer = "."
$objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colChassis = $objWMIService.ExecQuery("SELECT * FROM Win32_SystemEnclosure", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

For $objChassis in $colChassis
  For $objItem in $objChassis.ChassisTypes
    Select
    Case $objItem=1
        $strChassis = "Maybe Virtual Machine"
    Case $objItem=2
        $strChassis = "??"
    Case $objItem=3
        $strChassis = "Desktop"
    Case $objItem=4
        $strChassis = "Thin Desktop"
    Case $objItem=5
        $strChassis = "Pizza Box"
    Case $objItem=6
        $strChassis = "Mini Tower"
    Case $objItem=7
        $strChassis = "Full Tower"
    Case $objItem=8
        $strChassis = "Portable"
    Case $objItem=9
        $strChassis = "Laptop"
    Case $objItem=10
        $strChassis = "Notebook"
    Case $objItem=11
        $strChassis = "Hand Held"
    Case $objItem=12
        $strChassis = "Docking Station"
    Case $objItem=13
        $strChassis = "All in One"
    Case $objItem=14
        $strChassis = "Sub Notebook"
    Case $objItem=15
        $strChassis = "Space-Saving"
    Case $objItem=16
        $strChassis = "Lunch Box"
    Case $objItem=17
        $strChassis = "Main System Chassis"
    Case $objItem=18
        $strChassis = "Lunch Box"
    Case $objItem=19
        $strChassis = "SubChassis"
    Case $objItem=20
        $strChassis = "Bus Expansion Chassis"
    Case $objItem=21
        $strChassis = "Peripheral Chassis"
    Case $objItem=22
        $strChassis = "Storage Chassis"
    Case $objItem=23
        $strChassis = "Rack Mount Unit"
    Case $objItem=24
        $strChassis = "Sealed-Case PC"
    EndSelect
  Next
Next
If $strChassis = "Laptops" Then $strContainer = "LDAP://OU=Laptops,DC=Domain,DC=org"
Else
   $strContainer = "LDAP://OU=Desktops,DC=Domain,DC=org"
EndIf

I would like to get the $strContainer and pass it to this script

Dim $strUser, $strPassword, $strDomain
Const $JOIN_DOMAIN       = 1
Const $ACCT_CREATE       = 2
Const $ACCT_DELETE       = 4
Const $WIN9X_UPGRADE      = 16
Const $DOMAIN_JOIN_IF_JOINED  = 32
Const $JOIN_UNSECURE      = 64
Const $MACHINE_PASSWORD_PASSED = 128
Const $DEFERRED_SPN_SET    = 256
Const $INSTALL_INVOCATION   = 262144
 $objNetwork = ObjCreate("WScript.Network") 

; Declare $DOMAIN and the OU
$strDomain = "DOMAIN"
$strUser = "USERNAME"
$strPassword = "PASSWORD"

; Join $MACHINE to the $DOMAIN on the specified OU
$strComputer = $objNetwork.ComputerName
 $objComputer = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\" & _
 $strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
   $strComputer & "'")
$ReturnValue = $objComputer.JoinDomainOrWorkGroup($strDomain, _
  $strPassword, $strDomain & "\" & $strUser, $strOU, _
  $JOIN_DOMAIN + $ACCT_CREATE)

If $ReturnValue = 0 Then   ; if $MACHINE has $JOINED the $DOMAIN and account created successfully
MsgBox (0, "Joined", "Machine has $JOINED the $DOMAIN successfully. Please click OK to reboot")
ElseIf $ReturnValue = 2224 Then  ; if $MACHINE account already exists in the $DOMAIN
MsgBox (0, "Not Joined", "The computer account already exists." & @CRLF & "Computer will now join the $DOMAIN.")
 ; only join the $MACHINE to the $DOMAIN since account exists
$ReturnValue = $objComputer.JoinDomainOrWorkGroup($strDomain, _      
  $strPassword, $strDomain & "\" & $strUser, $strOU, _
  $JOIN_DOMAIN)
EndIf

Both scripts work when run by themselves with no problems

I was able to figure this out and posted the solution in post #4 (Since a pet peeve of mine is when in a forum someone says they resolved something or figured it out and never post what they've done to figure it out)

Edited by Elephant007
Link to comment
Share on other sites

I was able to figure this out by simply pasting the second code at the end of the first code

 

Feel free to use this, I use this script for imaging Windows 7 and having it join a domain unattended, since Microsoft's WAIK UnattendJoinDomain feature is buggy

 

This script finds out the chassis type, our domain is only concerned if the device is a Desktop or Laptop, after identifying the device, it will then move the device into the proper OU. I will highlight the areas that need to be changed so you can configure this for your own domain

 

 

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.6.1
 Author:         
 Script Function:
    This script is configured to find out the chassis type of a device, changing the device
    either read as a Desktop or Laptop.
    Then the script will join the device to the domain and puts it in the appropirate OU for my environment
    If you want to configure this for your domain, you need to make changes
    
    Change Chassis type if desired
    
    Change Lines
    86.   to the appropriate OU
    87.   to the appropriate OU
    106.  your domain
    107.  username with permission to join domain
    108.  password associated with username account

#ce ----------------------------------------------------------------------------

; Script Start 
AutoItSetOption ( 'TrayIconHide', 1 )

Dim $strComputer, $strChassis
Dim $objWMIService, $objChassis, $colChassis, $objItem
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$strComputer = "."
$objWMIService = ObjGet("winmgmts:" & "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
$colChassis = $objWMIService.ExecQuery("SELECT * FROM Win32_SystemEnclosure", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

For $objChassis in $colChassis
  For $objItem in $objChassis.ChassisTypes
    Select
    Case $objItem=1
        $strChassis = "Maybe Virtual Machine"
    Case $objItem=2
        $strChassis = "??"
    Case $objItem=3
        $strChassis = "Desktop"
    Case $objItem=4
        $strChassis = "Desktop" ;"Thin Desktop"
    Case $objItem=5
        $strChassis = "Desktop" ;"Pizza Box"
    Case $objItem=6
        $strChassis = "Desktop" ;"Mini Tower"
    Case $objItem=7
        $strChassis = "Desktop" ;"Full Tower"
    Case $objItem=8
        $strChassis = "Laptop" ;"Portable"
    Case $objItem=9
        $strChassis = "Laptop"
    Case $objItem=10
        $strChassis = "Laptop" ;"Notebook"
    Case $objItem=11
        $strChassis = "Hand Held"
    Case $objItem=12
        $strChassis = "Docking Station"
    Case $objItem=13
        $strChassis = "All in One"
    Case $objItem=14
        $strChassis = "Laptop" ;"Sub Notebook"
    Case $objItem=15
        $strChassis = "Desktop" ;"Space-Saving"
    Case $objItem=16
        $strChassis = "Lunch Box"
    Case $objItem=17
        $strChassis = "Main System Chassis"
    Case $objItem=18
        $strChassis = "Lunch Box"
    Case $objItem=19
        $strChassis = "SubChassis"
    Case $objItem=20
        $strChassis = "Bus Expansion Chassis"
    Case $objItem=21
        $strChassis = "Peripheral Chassis"
    Case $objItem=22
        $strChassis = "Storage Chassis"
    Case $objItem=23
        $strChassis = "Rack Mount Unit"
    Case $objItem=24
        $strChassis = "Sealed-Case PC"
    EndSelect
  Next
Next
If $strChassis = "Laptop" Then
    $strContainer = "OU=OUCONTAINER,DC=DOMAINNAME,DC=ORG"
Else
    $StrContainer = "OU=OUCONTAINER,DC=DOMAINNAME,DC=ORG"
EndIf

Dim $strUser, $strPassword, $strDomain
Const $JOIN_DOMAIN       = 1
Const $ACCT_CREATE       = 2
Const $ACCT_DELETE       = 4
Const $WIN9X_UPGRADE      = 16
Const $DOMAIN_JOIN_IF_JOINED  = 32
Const $JOIN_UNSECURE      = 64
Const $MACHINE_PASSWORD_PASSED = 128
Const $DEFERRED_SPN_SET    = 256
Const $INSTALL_INVOCATION   = 262144
 $objNetwork = ObjCreate("WScript.Network") 

; Declare $DOMAIN and the OU
$strDomain = "DOMAIN"
$strUser = "USERNAME WITH PERMISSIONS TO JOIN COMPUTERS TO DOMAIN"
$strPassword = "PASSWORD ASSOCIATED WITH USERNAME"

; Join $MACHINE to the $DOMAIN on the specified OU
$strComputer = $objNetwork.ComputerName
 $objComputer = ObjGet("winmgmts:{impersonationLevel=Impersonate}!\\" & _
 $strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
   $strComputer & "'")
$ReturnValue = $objComputer.JoinDomainOrWorkGroup($strDomain, _
  $strPassword, $strDomain & "\" & $strUser, $strContainer, _
  $JOIN_DOMAIN + $ACCT_CREATE)

If $ReturnValue = 0 Then
ElseIf $ReturnValue = 2224 Then  ; if cinoyterbame account already exists in the domain
 ; only join the computername to the domain since account exists
$ReturnValue = $objComputer.JoinDomainOrWorkGroup($strDomain, _      
  $strPassword, $strDomain & "\" & $strUser, $strContainer, _
  $JOIN_DOMAIN)
EndIf
Edited by Elephant007
Link to comment
Share on other sites

  • 1 month later...

Paste the second script in the include file and include, then compile. Include basically pastes the file at the end of the first file when compiling, and runs it from the first file when testing... however, I usually stay as far away from includes as possible (I run it without each include, then add all the included functions and variables neccesary, reducing compiled file space, and probably also processing time). This is a trick my brother showed me and then I adapted it to make it run more easily and smoothly... but I havent made a script for it yet... maybe I should then...

Link to comment
Share on other sites

  • Moderators

Mikeman27294,

Include basically pastes the file at the end of the first file when compiling

Actually, the included file is inserted into the main script at the point of the #include directive. :huh2:

i [...] then add all the included functions and variables neccesary, reducing compiled file space, and probably also processing time

If you use Obfuscator with certain options it will remove all unused functions and variables from the #include files for you when you compile. So no need to do it manually - or develop a script. Look in the Obfuscator Help file in SciTE or search the forum for more details. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Include basically pastes the file at the end of the first file when compiling,

This is incorrect, the #include preprocessor command inserts the "included" file at the point that the #include directive is seen in the script, not at the end of it. So if you want to be sure you're not hopelessly fubar'ing your script the #includes should always be at the start of the script.

... however, I usually stay as far away from includes as possible (I run it without each include, then add all the included functions and variables neccesary, reducing compiled file space, and probably also processing time). This is a trick my brother showed me and then I adapted it to make it run more easily and smoothly... but I havent made a script for it yet... maybe I should then...

You can use the Obfuscator directives to strip unused functions and variables from your script before compiling them so that you don't need to do it this way if you don't feel that it's necessary. You may speed up the compiling process, but you also increase the time needed to use those functions by making sure all of the needed support functions and variable declarations are there as well. If you find yourself using the same functions over and over again, a good rule of thumb is to create your own include file with just those functions and their dependencies in it that you'll be using and then #include that in your script so you don't have to recreate the wheel every time.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Mikeman27294,

Actually, the included file is inserted into the main script at the point of the #include directive. :huh2:

If you use Obfuscator with certain options it will remove all unused functions and variables from the #include files for you when you compile. So no need to do it manually - or develop a script. Look in the Obfuscator Help file in SciTE or search the forum for more details. ;)

M23

Hahaha ok... I guess I was told wrong then... I'll remember that one then, thanks. Is Obfuscator included with the full autoit suite, or with scite full or what?

Link to comment
Share on other sites

  • Moderators

Mikeman27294,

Obfuscator is included with the full SciTE4AutoIt3 package which you can download from here. :huh2:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...