Jump to content

Small script works, but cannot add an "IF" statement for command line parameters


Recommended Posts

I have a small script that is basically a front end for PC Anywhere, it allows us to input just an IP address and connect to our clients. The script seems to be working great, basically it comes up and asks for an IP, then passes the IP as a command line option to PC Anywhere. I have used some REGREAD and REGWRITE commands to keep the IP in registry so it will default to the last IP address connected to. If users hit CANCEL it will exit, and if they hit OK without typing anything, it makes sure to notify them. After a little tweaking, it seems to be working good.

I would like to add what I would think should be a simple IF statement. I would like to add this near the beginning, so "IF" the script (or .exe once compiled) is given an IP address as a command line parameter, it will launch a different command and bypass the rest of the script.

I am not sure what I am doing wrong, but it could be something with nesting different layers of "IF" statements that is giving me the trouble. Here is my current, working code as it is now:

if RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Helpdesk\PCA", "IPAddress") = "" then RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Helpdesk\PCA", "IPAddress", "REG_SZ", "192.168.")
$lastipaddress = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Helpdesk\PCA", "IPAddress")
    $bLoop = 1
    While $bLoop = 1
$ipaddress = InputBox("v1.1", "Please type in the IP Address" &@crlf& "to connect via pcAnywhere",$lastipaddress,"",100,-1)
if @error=1 then 
 $bLoop = 0
 Else
        If $ipaddress = "" Then
            MsgBox(4096, "Error", "You need to type in an IP ADDRESS.  Please try again!")
        Else
            RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Helpdesk\PCA", "IPAddress", "REG_SZ", $ipaddress)
            Run("C:\Program Files\Symantec\pcAnywhere\awrem32.exe C:\Documents and Settings\All Users\Application Data\Symantec\pcAnywhere\Remotes\connection.chf /c"&$ipaddress)
            exit
        EndIf
    EndIf
WEnd

I have tried adding an "IF $CMDLINE[1] = "" " statement, but I keep getting various errors when I try to run the scripts.

I would LIKE to, if a command line parameter does exist, just run a seperate command and then bypass the rest of the script.

If there is no command line parameter, I would like to simply run the script.

I have tried so many variations of IF and THEN and ELSE and ENDIF that I cant remember exactly what I have tried to get which errors, but lets just say that I cannot get it working at all. If anyone could tell me what I could add to the code above to do what I would like, I would greatly appreciate it.

If there is a more efficient way to do even the part of my script thats already working, I would be open to any ideas. I am new to AUTOIT and its a little confusing, and most of my experience in the past has been with batch files instead of actual scripting languages, but I am fairly happy with the way my script currently runs (minus the feature that I would like to add and cannot get going).

Thanks!

Link to comment
Share on other sites

$CmdLine[0] gives you the number of parameters.

If $CmdLine[0]=0 then
  do something
Else
  do something different
endif
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Not tested (I dont have PC Anywhere) but I think it should work like you want

If RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Helpdesk\PCA", "IPAddress") = "" Then RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Helpdesk\PCA", "IPAddress", "REG_SZ", "192.168.")
$lastipaddress = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Helpdesk\PCA", "IPAddress")
$bLoop = 1
If $CmdLine[0] < 1 Then
    While $bLoop = 1
        $ipaddress = InputBox("v1.1", "Please type in the IP Address" & @CRLF & "to connect via pcAnywhere", $lastipaddress, "", 100, -1)
        If @error = 1 Then
            $bLoop = 0
        Else
            If $ipaddress = "" Then
                MsgBox(4096, "Error", "You need to type in an IP ADDRESS.  Please try again!")
            Else
                RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Helpdesk\PCA", "IPAddress", "REG_SZ", $ipaddress)
                Run("C:\Program Files\Symantec\pcAnywhere\awrem32.exe C:\Documents and Settings\All Users\Application Data\Symantec\pcAnywhere\Remotes\connection.chf /c" & $ipaddress)
                Exit
            EndIf
        EndIf
    WEnd
Else
    If StringInStr($CmdLine[1], ".") Then
        RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Helpdesk\PCA", "IPAddress", "REG_SZ", $CmdLine[1])
        Run("C:\Program Files\Symantec\pcAnywhere\awrem32.exe C:\Documents and Settings\All Users\Application Data\Symantec\pcAnywhere\Remotes\connection.chf /c" & $CmdLine[1])
    Else
        MsgBox(0, "Error", "Invalid IP address passed to command line")
    EndIf
EndIf

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

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...