Jump to content

If Then statement with Global variable


Recommended Posts

I am always confused bewtween Dim,local,Global. Basically, I want to run a function that calls a Global variable, but a If-then is used to determine the path of that Global variable.

I am guessing even though the global variable gets declared in a If/Then statement, I still can't declare a global variable with the same name twice. And if that is the case, is there a way around this? No matter if the computer name begins with "USA" or not, it always moves the PC to the first $sTargetOU.

#include <AD.au3>

If StringInStr(@ComputerName,"USA") Then
    Global $sTargetOU = "OU=1,DC=domain,DC=com"
Else
    Global $sTargetOU = "OU=2,DC=domain,DC=com"
EndIf

;Connect to Active Directory
_AD_Open()

;Get FQDN of current machine name
Global $sObject = _AD_SamAccountNameToFQDN(@ComputerName & "$")

    ;Move current machine to OU location
Global $iValue = _AD_MoveObject($sTargetOU, $sObject)

; Close Connection to the Active Directory
_AD_Close()
#

FYI,he AD functions I am using are by the author, Water, posted here:

Link to comment
Share on other sites

You should use it like this:

#include <AD.au3> 

Global $sTargetOU, $sObject,  $iValue

If StringInStr(@ComputerName,"USA") Then
   $sTargetOU = "OU=1,DC=domain,DC=com" 
Else    
   $sTargetOU = "OU=2,DC=domain,DC=com" 
EndIf

;Connect to Active Directory
_AD_Open()  

;Get FQDN of current machine name 
$sObject = _AD_SamAccountNameToFQDN(@ComputerName & "{:content:}quot;)  

;Move current machine to OU location 
$iValue = _AD_MoveObject($sTargetOU, $sObject) 

; Close Connection to the Active Directory
 _AD_Close()

If you learn from It, it's not a mistake

Link to comment
Share on other sites

Your method is awkward, but works fine for me:

Global $aStrings[3] = ["USA12345", "12345", "123USA45"]

For $n = 0 To UBound($aStrings) - 1
    If StringInStr($aStrings[$n], "USA") Then
        Global $sTargetOU = "OU=1,DC=domain,DC=com"
    Else
        Global $sTargetOU = "OU=2,DC=domain,DC=com"
    EndIf
    ConsoleWrite($n & ": String = " & $aStrings[$n] & "; $sTargetOU = " & $sTargetOU & @LF)
Next

This would be a better way to do that check, though:

Global $sTargetOU = "OU=1,DC=domain,DC=com"

If Not StringInStr(@ComputerName, "USA") Then $sTargetOU = "OU=2,DC=domain,DC=com"

:mellow:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Apologies if this is a stupid question....but can you tell me where the _AD stuff is coming from. I dont have AD.au3 in my include folder either.

I am currently using V3.3.6.1, which I believe is the latest version.

Thanks

[font='Comic Sans MS']Eagles may soar high but weasels dont get sucked into jet engines[/font]

Link to comment
Share on other sites

Apologies if this is a stupid question....but can you tell me where the _AD stuff is coming from. I dont have AD.au3 in my include folder either.

I am currently using V3.3.6.1, which I believe is the latest version.

Thanks

There's a link in the first post :mellow:
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...