jackr57

Prevent script running when off the domain

8 posts in this topic

I have a script that creates Outlook signatures and it works fine. The problem is when laptop users take their laptop home, the script still runs and comes up with an error.

The error is:

line -1

error: error in expression

I tried updating the error handling to not display any error messages but it has not worked.

One other approach i was looking for was to see if i could check if the person is working off the enterprise lan and if they are not to run the script.

Any ideas?

 

#include <GUIConstants.au3>
#include <Misc.au3>
#include <File.au3>
#include <Process.au3>
#include <Constants.au3>
#include <string.au3>

Dim $UserObj
; - OLD PATH STOPPED WORKING WITH WIN 7 - DIM $sigpath              = ( @AppDataDir &'\microsoft\signatures' )
DIM $sigpath                = ( @AppDataDir &'\microsoft\signatures' ) 
Dim $msg
Dim $msg1
Dim $domain = @LogonDomain   ;pulls domain that user is logged into
Dim $username = @UserName    ;pulls username that is logged in
Const $ADS_NAME_INITTYPE_GC = 3
Const $ADS_NAME_TYPE_NT4 = 3
Const $ADS_NAME_TYPE_1779 = 1
;$oMyError = ObjEvent("AutoIt.Error", "ComError")
Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc")

$objRootDSE = ObjGet("LDAP://RootDSE")
; DNS domain name.
    $objTrans = ObjCreate("NameTranslate")
    $objTrans.Init ($ADS_NAME_INITTYPE_GC, "")
    $objTrans.Set ($ADS_NAME_TYPE_1779, @LogonDomain)
    $objTrans.Set ($ADS_NAME_TYPE_NT4, @LogonDomain & "\" & @UserName)
    $strUserDN = $objTrans.Get ($ADS_NAME_TYPE_1779)
    $UserObj = ObjGet("LDAP://" & $strUserDN)
DIM $fullname               = $UserObj.FullName
DIM $emailaddress           = $UserObj.EmailAddress
DIM $department             = $UserObj.Department
DIM $title              = $UserObj.Title
DIM $company                = $UserObj.company
DIM $faxnumber              = $UserObj.FaxNumber
DIM $officenumber           = $UserObj.TelephoneNumber
DIM $mobilenumber           = $UserObj.TelephoneMobile
DIM $mobile2                = $UserObj.Pager
DIM $webpage                = $UserObj.wWWHomePage
DIM $homeMDB                = $UserObj.get("homeMDB")
Dim $homeMDBtext            = StringSplit($homeMDB, ",")
Dim $mailboxname            = StringTrimLeft($homeMDBtext[4], 3)
Dim $Defaultsigname         = ("test")
Dim $sightm                 = (@LogonDomain & ".htm")
Dim $sigtxt                 = (@LogonDomain & ".txt")
Dim $sigrtf                 = (@LogonDomain & ".rtf")

call ("regsig")
call ("textsig")
call ("rtfsig")

FileDelete($sigpath & "\tmp.htm")
FileDelete($sigpath & "\tmp.rtf")
FileDelete($sigpath & "\tmp.txt")

Func regsig ()

FileInstall("sig.htm", $sigpath & "\tmp.htm",1)
$filename1 = $sigpath & "\tmp.htm"
_ReplaceStringInFile($filename1,"namenamename",$fullname)
_ReplaceStringInFile($filename1,"emailemailemail","<a href='mailto:"& $emailaddress &"'>" & $emailaddress & "<br /></a>")
_ReplaceStringInFile($filename1,"titletitletitle",$title)
_ReplaceStringInFile($filename1,"companycompanycompany",$company)
If $officenumber = "+5555555555" Then
  _ReplaceStringInFile($filename1,"offnumoffnumoffnum","Office: " & $officenumber)
    Else
  _ReplaceStringInFile($filename1,"offnumoffnumoffnum","Direct: " & $officenumber)
EndIf
If $mobilenumber <>"" Then
    _ReplaceStringInFile($filename1,"mobilemobilemobile", "Mobile: " & $mobilenumber)
Else
    _ReplaceStringInFile($filename1,"mobilemobilemobile<br />", "")
EndIf
If $mobile2 <>"" Then
    _ReplaceStringInFile($filename1,"mobile2mobile2mobile2", $mobile2)
Else
    _ReplaceStringInFile($filename1,"mobile2mobile2mobile2<br />", "")
EndIf
If $webpage <>"" Then
    _ReplaceStringInFile($filename1,"webpagewebpagewebpage","<a href='"& $webpage &"'>Download vCard<br /></a>")
Else
    _ReplaceStringInFile($filename1,"webpagewebpagewebpage", "")
EndIf
FileCopy($filename1,$sigpath & "\" & $sightm,1)
EndFunc

Func textsig ()
    
FileInstall("sig.txt", $sigpath & "\tmp.txt",1)
$filename1 = $sigpath & "\tmp.txt"
_ReplaceStringInFile($filename1,"namenamename",$fullname)
_ReplaceStringInFile($filename1,"emailemailemail",$emailaddress)
_ReplaceStringInFile($filename1,"titletitletitle",$title)
_ReplaceStringInFile($filename1,"companycompanycompany",$company)
If $officenumber = "555555555" Then
_ReplaceStringInFile($filename1,"offnumoffnumoffnum","Office: " & $officenumber)
    Else
  _ReplaceStringInFile($filename1,"offnumoffnumoffnum","Direct: " & $officenumber)
EndIf
If $mobilenumber <>"" Then
    _ReplaceStringInFile($filename1,"mobilemobilemobile", "Mobile: " & $mobilenumber)
Else
    _ReplaceStringInFile($filename1,"mobilemobilemobile", "")
EndIf
If $mobile2 <>"" Then
    _ReplaceStringInFile($filename1,"mobile2mobile2mobile2", $mobile2)
Else
    _ReplaceStringInFile($filename1,"mobile2mobile2mobile2", "")
EndIf
If $webpage <>"" Then
    _ReplaceStringInFile($filename1,"webpagewebpagewebpage",$webpage)
Else
    _ReplaceStringInFile($filename1,"webpagewebpagewebpage", "")
EndIf
FileCopy($filename1,$sigpath & "\" & $sigtxt,1)
EndFunc
    
Func rtfsig ()
    
FileInstall("sig.rtf", $sigpath & "\tmp.rtf",1)
$filename1 = $sigpath & "\tmp.rtf"
_ReplaceStringInFile($filename1,"namenamename",$fullname)
_ReplaceStringInFile($filename1,"emailemailemail",$emailaddress)
_ReplaceStringInFile($filename1,"titletitletitle",$title)
_ReplaceStringInFile($filename1,"companycompanycompany",$company)
If $officenumber = "555555555" Then
_ReplaceStringInFile($filename1,"offnumoffnumoffnum","Office: " & $officenumber)
    Else
  _ReplaceStringInFile($filename1,"offnumoffnumoffnum","Direct: " & $officenumber)
EndIf
If $mobilenumber <>"" Then
    _ReplaceStringInFile($filename1,"mobilemobilemobile", "Mobile: " & $mobilenumber)
Else
    _ReplaceStringInFile($filename1,"mobilemobilemobile","")
EndIf
If $mobile2 <>"" Then
    _ReplaceStringInFile($filename1,"mobile2mobile2mobile2", $mobile2)
Else
    _ReplaceStringInFile($filename1,"mobile2mobile2mobile2","")
EndIf
If $webpage <>"" Then
    _ReplaceStringInFile($filename1,"webpagewebpagewebpage",$webpage)
Else
    _ReplaceStringInFile($filename1,"webpagewebpagewebpage","")
EndIf
FileCopy($filename1,$sigpath & "\" & $sigrtf,1)
EndFunc
    
Func _ErrFunc()
;~  ConsoleWrite($oError.scriptline & ";" & $oError.source & ";" & $oError.description & @CRLF)
    Return
EndFunc
    
;Func ComError()
;    If IsObj($oMyError) Then
;       $HexNumber = Hex($oMyError.number, 8)
;        SetError($HexNumber)
;    Else
;        SetError(1)
;    EndIf
;    Return 0
;EndFunc

 

Share this post


Link to post
Share on other sites



Check @LogonServer. IIRC it is set to the name of the PC if there is no connection to a DC.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I am guessing the error comes here :

$objRootDSE = ObjGet("LDAP://RootDSE")
; DNS domain name.
    $objTrans = ObjCreate("NameTranslate")

Just put an error check for those 2 and you are set.

Your commented the error handler - activate it and put a check inside if Isobject($objRootDSE) - then exit

 

Share this post


Link to post
Share on other sites

I think i found a solution.

I added if else statement to check for a blank field and exit if so.

If $fullname == "" Then
    exit
Else

seems to work.

Share this post


Link to post
Share on other sites

That doesn't seem to be a good way of programming - maybe a user in AD doesnt have a fullname set - you should check for

$UserObj

And if it is not an object - exit. 

Because

$fullname               = $UserObj.FullName

Share this post


Link to post
Share on other sites

Hi :)

I'm in the same case for some of my applications. So , what I do is the following:

SplashTextOn  ("", "Checking your connection", 400, 100, -1, -1,  33, -1, -1, 700)
    Global $ping = Ping("DOMAIN NAME", 1500)
    If $ping > 0 Then
        Sleep (1000)
        SplashOff()
    Else
        SplashOff()
        Msgbox (16, "Not connected to domain", "You can only use this software when connected to the DOMAIN." & @CRLF & @CRLF & "Program will now exit!")
        _exit()
     EndIf

Of course, adapt it to your case and that should do the trick :)
Hope that helps.


~~~ Doom Shall Never Die, Only The Players ~~~

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

thanks for replies.

It was these lines that were generating the error when offline:

 

;DIM $homeMDB               = $UserObj.get("homeMDB")
;Dim $homeMDBtext           = StringSplit($homeMDB, ",")
;Dim $mailboxname           = StringTrimLeft($homeMDBtext[4], 3)
;Dim $Defaultsigname        = ("test")

I commented them out as I didn't think they were being used. The script still works without those lines. However when those lines are removed and the user was offline it would then create the signature with no data in it as there would be no active directory data to pull down.

So what i did was modify the first html sig function with if else statement.

Func regsig ()
If $fullname == "" Then
    exit
Else
FileInstall("sig.htm", $sigpath & "\tmp.htm",1)
$filename1 = $sigpath & "\tmp.htm"
_ReplaceStringInFile($filename1,"namenamename",$fullname)
_ReplaceStringInFile($filename1,"emailemailemail","<a href='mailto:"& $emailaddress &"'>" & $emailaddress & "<br /></a>")
_ReplaceStringInFile($filename1,"titletitletitle",$title)
_ReplaceStringInFile($filename1,"companycompanycompany",$company)
If $officenumber = "+55555555555" Then
  _ReplaceStringInFile($filename1,"offnumoffnumoffnum","Office: " & $officenumber)
    Else
  _ReplaceStringInFile($filename1,"offnumoffnumoffnum","Direct: " & $officenumber)
EndIf
If $mobilenumber <>"" Then
    _ReplaceStringInFile($filename1,"mobilemobilemobile", "Mobile: " & $mobilenumber)
Else
    _ReplaceStringInFile($filename1,"mobilemobilemobile<br />", "")
EndIf
If $mobile2 <>"" Then
    _ReplaceStringInFile($filename1,"mobile2mobile2mobile2", $mobile2)
Else
    _ReplaceStringInFile($filename1,"mobile2mobile2mobile2<br />", "")
EndIf
If $webpage <>"" Then
    _ReplaceStringInFile($filename1,"webpagewebpagewebpage","<a href='"& $webpage &"'>Download vCard<br /></a>")
Else
    _ReplaceStringInFile($filename1,"webpagewebpagewebpage", "")
EndIf
FileCopy($filename1,$sigpath & "\" & $sightm,1)
EndIf
EndFunc

This way if the fullname is blank it then exits the script.

Seems to work as desired although might not be the best solution.

Edited by jackr57

Share this post


Link to post
Share on other sites

When using my AD UDF you will get an error from_AD_Open when a connection to AD is not possbible.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

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