Jump to content
Sign in to follow this  
cesinha87

Help here please.....Not able to use the Run function correctly

Recommended Posts

Hello everyone, 

I am very new to AUTOIT and I am almost done with my script and I am having a hard time with the Run function....When I run the command locally in the CMD it works but when I run through the AUTOIT it doesn't

Note:  sqlcmd.exe command is available in the system where I am running the AutoIt Editor

 I've seen before here and I can't figure out. I am trying to write the final command in the Output tab (where the arrow is) to make sure the command and variables are passing correctly using CONSOLEWRITE and I am not able to do that. I tried MSGBOX as well

 

$command = Run (@ComSpec & " /c " & 'sqlcmd.exe' & ' -S ' & $db_server_name & '\SQLEXPRESS' & ' -i ' & $sql_script & ' -O ' & $output_log, '', @SW_HIDE)
ConsoleWrite ($command)

 

image.thumb.png.8b466a6fc8c84be0ca0545eaeb39a1a9.png

Share this post


Link to post
Share on other sites

Are you sure it is able to find the command from the script directory?

for testing purpose use:

ConsoleWrite("cmd=" & @ComSpec & ' /k sqlcmd.exe -S ' & $db_server_name & '\SQLEXPRESS -i ' & $sql_script & ' -O ' & $output_log & @CRLF)
$rc = Run(@ComSpec & ' /k sqlcmd.exe -S ' & $db_server_name & '\SQLEXPRESS -i ' & $sql_script & ' -O ' & $output_log, '', @SW_SHOW)
ConsoleWrite("rc=" & $rc & @CRLF)

and check the result.

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

It looks like it's running part of the command.
C:\Windows\system32\cmd.exe /k sqlcmd.exe -S WIN2K16 and it's supposed to run the entire command


sqlcmd.exe -S WIN2K16\SQLEXPRESS -i "C:\Temp\Delete Duplicate SQL Query.sql" -O C:\Windows\System32\McAfeeLogs\SQL_Script_log.txt

image.thumb.png.89df133b03f5350cfe023557eda62694.png

Share this post


Link to post
Share on other sites

The rest is on the next line which could mean there is an LF or CR character in the variable?
So how are these filled and what is their exact content?

Jos


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

You are right sir, there some kind LF or CR in the $db_server_name variable and I am not able to find or figure out what's going on. When I run the following script:

Same script as before without $db_server_name

ConsoleWrite("cmd=" & @ComSpec & ' /k sqlcmd.exe -S ' & '\SQLEXPRESS' & ' -i ' & $sql_script & ' -O ' & $output_log & @CRLF)
$rc = Run(@ComSpec & ' /k sqlcmd.exe -S ' & $db_server_name & '\SQLEXPRESS' & ' -i ' & $sql_script & ' -O ' & $output_log, '', @SW_SHOW)
ConsoleWrite("rc=" & $rc & @CRLF)

I get the right output

image.thumb.png.54f33c0e54ee926136e2f777ee19defe.png

Here is where I am getting the $db_server_name variable (Win2k16)

image.thumb.png.395871e9792de8649246ee2d7ac078eb.png

 

Global $read = $file

    $read = FileRead($file)
      $trim1 = StringInStr($read, 'db.database.name=')
    $position1 = StringInStr($read, "db.database.name=")
        ;MsgBox(0,"",$position1)
    $tmp = StringTrimLeft ($read, $position1+16)
     ;MsgBox (0, 'Database Name', $tmp)
      $position2 = StringInStr($tmp, 'db.instance.name=')
     ;MsgBox(0,"Position 2",$position2)
    $db_name = StringTrimRight ($tmp,StringLen ($tmp) - $position2+1)
     ;MsgBox (0, 'Database Name', $db_name)
    $trim3 = StringInStr($read, 'db.server.name=')
    $position3 = StringInStr($read, "db.server.name=")
        ;MsgBox(0,"",$position1)
    $tmp3 = StringTrimLeft ($read, $position3+14)
     ;MsgBox (0, 'Database Name', $tmp3)
    $position4 = StringInStr($tmp3, 'db.database.name=')
     ;MsgBox(0,"Position 2",$position4)
    $db_server_name = StringTrimRight ($tmp3,StringLen ($tmp3) - $position4+1)
     MsgBox (0, '$db_server_name', $db_server_name)


$sql_script = '"C:\Temp\Delete Duplicate SQL Query.sql"'
$output_log = "C:\Temp\output.log"


;Command to run: sqlcmd -S WIN2K16\SQLEXPRESS -i "C:\Temp\Delete Duplicate SQL Query.sql" -O C:\Windows\System32\McAfeeLogs\SQL_Script_log.txt


ConsoleWrite("cmd=" & @ComSpec & ' /k sqlcmd.exe -S ' & '\SQLEXPRESS' & ' -i ' & $sql_script & ' -O ' & $output_log & @CRLF)
$rc = Run(@ComSpec & ' /k sqlcmd.exe -S ' & $db_server_name & '\SQLEXPRESS' & ' -i ' & $sql_script & ' -O ' & $output_log, '', @SW_SHOW)
ConsoleWrite("rc=" & $rc & @CRLF)

 

image.png

Share this post


Link to post
Share on other sites

I believe I just found the issue :) . I had a paragraph in my variable $db_server_name . Changed the SringTrimRight to 

$db_server_name = StringTrimRight ($tmp3,StringLen ($tmp3) - $position4+2)
     MsgBox (0, '$db_server_name', $db_server_name)

instead

$db_server_name = StringTrimRight ($tmp3,StringLen ($tmp3) - $position4+1)
     MsgBox (0, '$db_server_name', $db_server_name)

and the command executed correctly. 

Thank you very mich Jos, you helped a lot

Share this post


Link to post
Share on other sites

While typing this post as answer to your previous reply I see you found the solutions so just bare this in mind for the next time. :) 

Answer to previous post: This is way too much information. Keep it simple and start posting a simple script that is runnable and has an issue. Describe the issue and  also post the SciTE output information in a simple quote (")  or codebox (<>) by using these symbols at the to of the forum post window.

Jos 


SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...