smf113 Posted August 6, 2011 Posted August 6, 2011 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.
Mobius Posted August 6, 2011 Posted August 6, 2011 (edited) 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 August 7, 2011 by Mobius
PsaltyDS Posted August 6, 2011 Posted August 6, 2011 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) 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
smf113 Posted August 6, 2011 Author Posted August 6, 2011 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.
PsaltyDS Posted August 6, 2011 Posted August 6, 2011 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) 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
smf113 Posted August 6, 2011 Author Posted August 6, 2011 Sounds good.Thank you very much Mobius and Psalty I truly appreciate the help.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now