Sign in to follow this  
Followers 0
mtmartis

If Then statement with Global variable

7 posts in this topic

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:

Share this post


Link to post
Share on other sites



using StringCompare() might be what your looking for


[spoiler]My UDFs: Login UDF[/spoiler]

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Thanks for the replies, my scripting skills are still pretty raw, it helps to see the proper structure syntax you've all provided. :mellow:

Share this post


Link to post
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]

Share this post


Link to post
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:

Share this post


Link to post
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
Sign in to follow this  
Followers 0