Jump to content
Sign in to follow this  
EllisDee

Problem checking string in cmd prompt

Recommended Posts

EllisDee

Hey,

I'm having trouble with this script I'm writing.

; Deployment Script
; This script requires full Administrative rights
#requireadmin
; Get the MAC Address of the computer
Send ("{LWin}")
Send ("cmd.exe")
Send ("{ENTER}")
WinWait ("C:\Windows\system32\cmd.exe")
Send ("ipconfig /All")
Send ("{ENTER}")
; Check if Corp. IS&T have been contacted
$yesnobox = MsgBox(4, "Check!", "Have you registered the MAC# with Corporate IS&T for ACS (Authentication)")
If $yesnobox = 7 Then
      Exit
      Else
; Disable Autotuning on network card
Send ("{LWin}")
Send ("cmd.exe")
Send ("{CTRLDOWN}{SHIFTDOWN}{ENTER}")
WinWait ("Administrator: C:\Windows\system32\cmd.exe")
Send ("{CTRLUP}{SHIFTUP}")
Send ("cls")
Send ("{ENTER}")
Send ("netsh interface tcp set global autotuning=disabled")
Send ("{ENTER}")
$CheckcmdWindow StringInStr ( "Administrator: C:\Windows\system32\cmd.exe", "OK.")
If $CheckcmdWindow = 1 Then
   Send ("exit")
   Send ("{ENTER}")
Else
MsgBox(0, "Error", "Autotuning the network card failed")
EndIf
EndIf

Please bear in mind I have no programming experiance or scripting experiance, I figured autoit was the simplest way to learn. You may notice the code is basic, if you can suggest ways to improve it, it would be appreciated.

Basically my problems is with the stringinstr command, I cannot seem to get it to read the OK. at the end of the command prompt to verify whether autotuning worked or not.

I have checked here and here

But don't really understand how to resolve my issue.

Any help is gratefully received,

Share this post


Link to post
Share on other sites
water

Welcome to AutoIt and the forum!

I would try to read the STDOUT stream as you have seen in the examples you mentioned. In function StdOutRead there is a good example.

Give it a try and if you have problems please post the code and we will try to help.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
EllisDee

Hi thanks for the reply,

I'm not sure how to use stdoutread to look for OK. when cmd.exe opens and the netsh command is run & if it sees OK. to continue or if cmd.exe shows an error then to display a msg box, I'm assuming the latter is going to be a IF/Else command.

Also the help file mentions a $pid, im not sure where to put this in the code I have created.

Thanks,

Share this post


Link to post
Share on other sites
water

This should give you the output of "ipconfig /all" in variable $line:

#include <Constants.au3>

Local $foo = Run(@ComSpec & " /c ipconfig /all", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
    $line = $line & StdoutRead($foo)
    If @error Then ExitLoop
WEnd
MsgBox(0, "STDOUT read:", $line)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
EllisDee

I think I'm getting somewhere.

The problem is I am trying to run the netsh command. This requires the cmd.exe window opened with elevated privalages, this is why I had:

Send ("{LWin}")
Send ("cmd.exe")
Send ("{CTRLDOWN}{SHIFTDOWN}{ENTER}")
WinWait ("Administrator: C:\Windows\System32\cmd.exe")
Send ("{CTRLUP}{SHIFTUP}")
Send ("cls")
Send ("{ENTER}")
Send ("netsh interface tcp set global autotuning=disabled")
Send ("{ENTER}")

However I'm guessing with the stdreadout command you have to write it as you did in your previous post. I have looked here: And I still cannot get cmd.exe to run elevated.

I have tried a modification of the code you wrote so it ask's for ID & Psw. Note that I am on a domain, however if asking for local admin ID & Psw I would assume I use the @computername command rather than mydomainnname

#include <Constants.au3>
#requireadmin
$username = InputBox("Local Admin ID Required", "Please enter Username")
$pw = InputBox("Local Admin Psw Required", "Please enter Password", "", "*")
Local $foo = RunAs("$username", @ComputerName, "$pw", 2, @ComSpec & " /c netsh interface tcp set global autotuning=disabled", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
    $line = $line & StdoutRead($foo)
    If @error Then ExitLoop
WEnd
MsgBox(0, "STDOUT read:", $line)

The STDOUT Message I'm receiving with the above code is: Set global command failed on IPv4 The parameter is incorrect. This error message indicates that cmd.exe has not run with elevated permission.

I'm about to finish work today however I will be back on tomorrow,

Thanks again for help!

Share this post


Link to post
Share on other sites
water

Wouldn't this be enough? Because when you set #requireadmin the whole script runs with elevated privileges.

#include <Constants.au3>
#requireadmin
Local $foo = Run(@ComSpec & " /c netsh interface tcp set global autotuning=disabled", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
    $line = $line & StdoutRead($foo)
    If @error Then ExitLoop
WEnd
MsgBox(0, "STDOUT read:", $line)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

BTW: Make sure to grab the output of STDERR too:

#include <Constants.au3>
#requireadmin
Local $foo = Run(@ComSpec & " /c ipconfig /all", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
    $line = $line & StdoutRead($foo)
    If @error Then ExitLoop
WEnd
MsgBox(0, "STDOUT read:", $line)
$line = ""
While 1
    $line = $line & StderrRead($foo)
    If @error Then ExitLoop
WEnd
If $line <> "" Then MsgBox(0, "STDERR read:", $line)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
BrewManNH

This line:

Local $foo = RunAs("$username", @ComputerName, "$pw", 2, @ComSpec & " /c netsh interface tcp set global autotuning=disabled", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

won't run correctly because you have the variable names inside quotes, remove the quotes from around $username and $pw and it will probably work right. Or, use the other suggestions given above and run the whole script as an administrator.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
EllisDee

Thanks for the replies,

Wouldn't this be enough? Because when you set #requireadmin the whole script runs with elevated privileges.

#include <Constants.au3>
#requireadmin
Local $foo = Run(@ComSpec & " /c netsh interface tcp set global autotuning=disabled", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
    $line = $line & StdoutRead($foo)
    If @error Then ExitLoop
WEnd
MsgBox(0, "STDOUT read:", $line)

Unfortunatly this is not enough, I have already tried the #requireadmin command in my original script.

This line:

Local $foo = RunAs("$username", @ComputerName, "$pw", 2, @ComSpec & " /c netsh interface tcp set global autotuning=disabled", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

won't run correctly because you have the variable names inside quotes, remove the quotes from around $username and $pw and it will probably work right. Or, use the other suggestions given above and run the whole script as an administrator.

I have removed quotes however cmd.exe still doesn't run with elevated permission, this could be a problem with the localadmin ID & psw however.

I've done some more research on this:

A site with information regarding autotuning network card & commands: http://www.speedguide.net/faq_in_q.php?qid=247

I have tried using this: however my script doesn't run after the elevate pop up window.

I have tried this: http://www.fanhow.com/answers/question-6...-Command-Prompt-Shortcut-in-Wi using the following:

#include <Constants.au3>
#requireadmin
Local $foo = Run("C:testelevate.exe %windir%system32cmd.exe" & " /c netsh interface tcp set global autotuning=disabled", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
    $line = $line & StdoutRead($foo)
    If @error Then ExitLoop
WEnd
MsgBox(0, "STDOUT read:", $line)

However the stdout pop up is just blank.

(Elevated Command prompt.exe can be downloaded here: http://www.sevenforums.com/tutorials/3718-elevated-command-prompt-shortcut.html)

This site (http://blogs.msdn.com/b/tims/archive/200...ed-command-prompt-in-six-keyst) explains how to open the cmd.exe window with elevated privileges which is why initially I was doing the keystroke approach

; Deployment Script
; This script requires full Administrative rights
#requireadmin
; Get the MAC Address of the computer
Send ("{LWin}")
Send ("cmd.exe")
Send ("{ENTER}")
WinWait ("C:\Windows\System32\cmd.exe")
Send ("ipconfig /All")
Send ("{ENTER}")
; Check if Corp. IS&T have been contacted
$yesnobox = MsgBox(4, "Check!", "Have you registered the MAC# with Corporate IS&T for ACS (Authentication)")
If $yesnobox = 7 Then
      Exit
      Else
; Disable Autotuning on network card
Send ("{LWin}")
Send ("cmd.exe")
Send ("{CTRLDOWN}{SHIFTDOWN}{ENTER}")
WinWait ("Administrator: C:\Windows\System32\cmd.exe")
Send ("{CTRLUP}{SHIFTUP}")
Send ("cls")
Send ("{ENTER}")
Send ("netsh interface tcp set global autotuning=disabled")
Send ("{ENTER}")
Send ("Exit")
Send ("{ENTER}")
EndIf

The above works, but I wanted some sort of error checking, so when the netsh command is run the output from the command window will either be "OK." (continue with script) or "Set global command failed on IPv4 The requested operation requires elevation" (Stop script, report error in a pop up)

Edited by EllisDee

Share this post


Link to post
Share on other sites
water

This works fine for me and gives me the expected output:

#include <Constants.au3>
#requireadmin
Local $foo = Run(@ComSpec & " /c netsh interface show interface", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
    $line = $line & StdoutRead($foo)
    If @error Then ExitLoop
WEnd
MsgBox(0, "STDOUT read:", $line)
$line = ""
While 1
    $line = $line & StderrRead($foo)
    If @error Then ExitLoop
WEnd
If $line <> "" Then
    MsgBox(0, "STDERR read:", $line)
Else
    MsgBox(0, "STDERR read:", "No output from STDERR!")
EndIf

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
EllisDee

This works fine for me and gives me the expected output:

#include <Constants.au3>
#requireadmin
Local $foo = Run(@ComSpec & " /c netsh interface show interface", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
    $line = $line & StdoutRead($foo)
    If @error Then ExitLoop
WEnd
MsgBox(0, "STDOUT read:", $line)
$line = ""
While 1
    $line = $line & StderrRead($foo)
    If @error Then ExitLoop
WEnd
If $line <> "" Then
    MsgBox(0, "STDERR read:", $line)
Else
    MsgBox(0, "STDERR read:", "No output from STDERR!")
EndIf

Hi,

I don't see how this helps my situation as you have used the code "netsh interface show interface" rather than "netsh interface tcp set global autotuning=disabled"

If I use your autoit script with the correct code I notice that the problem where the command prompt is not being run in elevated mode still exists.

#include <Constants.au3>
#requireadmin
Local $foo = Run(@ComSpec & " /c netsh interface tcp set global autotuning=disabled", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
    $line = $line & StdoutRead($foo)
    If @error Then ExitLoop
WEnd
MsgBox(0, "STDOUT read:", $line)
$line = ""
While 1
    $line = $line & StderrRead($foo)
    If @error Then ExitLoop
WEnd
If $line <> "" Then
    MsgBox(0, "STDERR read:", $line)
Else
    MsgBox(0, "STDERR read:", "No output from STDERR!")
EndIf

Share this post


Link to post
Share on other sites
water

I was using "show interface" because I didn't want to change the interface settings. I just wanted to be sure that all output of the interface cmd could be grabbed by the script.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
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
Sign in to follow this  

×