Jump to content

Command Prompt Alternative


Recommended Posts

Hi, I am new to AutoIT so bare with me. I have gone through several threads and searched relatively high and low for the answer to my question. I am sure it's that I am over thinking the situation, or the answer is right in front of me but either way here it goes.

I am writing a script that basically rolls out Windows 7 PC's for my company, with that I need to do couple of things via the command prompt. 1 being importing a power plan e.g. "example.pow", and 2 being importing a firewall configuration e.g. "advfirewall.wfw".

here is my current solution (the one I would like to think there is a better way of doing):

- I read the file locations in from an .ini file to a variable. This is necessary because there will be more then one person setting up PC's and I don't know the file structure of their flash drives.

- Then I set the actual command to run the import to a variable.

- I then concatenate the to variables to one string.

- I open an instance of cmd.exe

- then I use the send command to the new complete string.

This seems to work but the thing is I would like to avoid opening the cmd.exe instance for the sake of keeping from having to rely on external programs, especially ones where if for some reason they don't open as elevated it might not work.

Here is the raw code (I essentially do the same thing for both items):

#RequireAdmin

$PowerScheme=IniRead("Setup.ini", "PowerScheme", "PowerLoc", "Not Found")

;Import power config settings

$PowerCMD1="Powercfg -Import "

$PowerCMD2=$PowerCMD1&$PowerScheme

Run("cmd.exe")

WinWaitActive("Administrator: C:\Windows\system32\cmd.exe")

Send($PowerCMD2)

Send("{ENTER}")

Sleep(5000)

Any help or assistance would be greatly appreciated.

Link to comment
Share on other sites

How about something like this since powercfg is not dependant on cmd.exe.

$PowerScheme=IniRead("Setup.ini", "PowerScheme", "PowerLoc", "")
IF $PowerScheme <> "" THEN
   $PowerCMD1="Powercfg.exe /Import "& $PowerScheme; Here we are always assuming that the first part of the powerscheme string starts with a valid parameter for  /import switch, is that right?
  $ExitCode = RunWait($PowerCMD1)
  IF @ERROR THEN
    MsgBox(16,@SCRIPTNAME,"Failed to execute:"& @LF & $PowerCMD1)
  ELSE
    MsgBox(48,@SCRIPTNAME,"PowerCfg returned an exit code of : "& $ExitCode)
  ENDIF
ENDIF
Edited by Mobius

wtfpl-badge-1.png

Link to comment
Share on other sites

You don't have to run cmd.exe first, unless you really need the native functions of the CMD console. That could be shortened to:

#RequireAdmin

$PowerScheme=IniRead("Setup.ini", "PowerScheme", "PowerLoc", "Not Found")

;Import power config settings
$PowerCMD = @SystemDir & '\Powercfg.exe -Import "' & $PowerScheme & '"'
$iRET = RunWait($PowerCMD, @SystemDir, @SW_MINIMIZE)

: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
Link to comment
Share on other sites

Very nice both solutions work wonderfully. So based on your comment Psalty would I be able to utilize the Netsh.exe to import the firewall configuration in a similar manner?

Thank you very much for your help.

Link to comment
Share on other sites

Yes, there are some examples with NetSh already on the forum:

#include <Constants.au3>

Global $iPID = Run(@SystemDir & "\NetSh.exe INT SHOW INT", @SystemDir, @SW_MINIMIZE, $STDOUT_CHILD)

Global $sLine
While 1
    $sLine &= StdoutRead($iPID)
    If @error Then ExitLoop
WEnd

ConsoleWrite("$sLine = " & $sLine & @LF)

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