Vivaed

Run(@ComSpec...) with schtasks

3 posts in this topic

#1 ·  Posted (edited)

I have this button in an app I am building:

When you click it, it then opens a FileOpenDialog() I select and XML, then it asks for a name input, enter the name, then it should run the following code:

schtasks.exe /create /tn XXXX-task /XML C:\WBRESO~1\WB-LOO~1\TESTIN~1.XML

It works when I put it in the CMD and run it, I get SUCCESS! and the new task has been created in Task Scheduler.

C:\Users\vivaed>schtasks.exe /create /tn XXXX /xml C:\WBRESO~1\WB-LOO~1\TESTIN~1.XML
SUCCESS: The scheduled task "XXXX" has successfully been created.

But when I run it in:

(This is just a snippet of the code, there is a lot more)

#include <AutoItConstants.au3>

Global $wbResources = 'C:\"WB Resources"\WB-LoopChecker\'

Case $Button4
                $getXML = FileOpenDialog("Find task to import", $wbResources ,"XML (*.xml)")

                If @error Then
                    ; Display the error message.
                    MsgBox($MB_SYSTEMMODAL, "", "No file was selected.")
                Else
                    Local $taskName = InputBox("Name this task","File: " & @CRLF & $getXML & @CRLF & "Name this task:" & @CRLF,"XXXX-task")
                    $schedTask = 'schtasks.exe /create /tn ' & $taskName & ' /XML ' & FileGetShortName($getXML)
                    Run(@ComSpec & " /c " & $schedTask, @SW_HIDE)
                    ConsoleWrite($wbResources & @CRLF)
                    ConsoleWrite($schedTask & @CRLF)
                EndIf

Here I dont get any errors but the when I refresh the Task Scheduler, there is no new task.... :(

 

Edited by Vivaed

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

you either need #RequireAdmin at the top of your script, or you have a quotes issue in the string you pass to Run().

to troubleshoot the 2nd option i see you do a ConsoleWrite() to a part of the string. do that for the whole string, including the @ComSpec part - the exact complete string you pass to Run().

EDIT: how are you checking for errors for saying that you hare not getting any?

EDIT: perhaps this will be of some use to you:

 

Edited by orbs

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Change the /c to /k and remove the @SW_HIDE so you can see the CMD window and the errors generated.

Jos

edit: Actually you have an error in your Run command. the second parameter should be the workdir, not the Window status!

Edited by Jos
1 person likes this

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          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

  • Similar Content

    • BlazerV60
      By BlazerV60
      Hello all,
      I've written the code below which launches chrome in incognito mode and then proceeds to go to the autoit website.
      From my understanding, the Run() command is also supposed to output the PID number related to the application that got launched from the Run command.
      However when I run the below lines, it outputs a PID number that is different from the newly launched chrome browser's PID number, does anyone know why and possibly explain how I could retrieve the accurate PID number associated with the newly launched browser?
      Global $iPid = Run(@ComSpec & ' /c start chrome.exe https://www.autoitscript.com/forum/ -incognito' ,"", "") msgbox(0,"",$iPid) Thank you,
      Brian
    • hcI
      By hcI
      Hello I would like to know if there is a way to return a sentence in cmd when I launch from it (because I add arguments).
      For example, diskpart.exe which help to manage the key and hdd connected, when you launch it with the parameter "/f" the app return a sentence saying that it don't recognize the parameter "/f" and it return the sentence in the cmd where i started the application, not a new one.
      That's what I want to do but I couldn't find anything that would solve my problem on internet and on AutoIt like ConsoleWrite / ConsoleWriteError (don't work).
       
      Thanks
    • Duck
      By Duck
      I'm attempting to capture the output from the command line tool PSEXEC. I'm using AutoIT to run an instance of PSEXEC against a remote PC to audit Local Admins in my environment using net.exe (C:\Windows\System32> net localgroup administrators). However the usual trick I use to capture command line output does not appear to work well with PSEXEC, as the bottom portion of the output is missing from the return. Any ideas or recommendations are greatly appreciated.  
       
      Here is what I'm working with: 
      ;This script will read from a list of hosts and report who has local admin privileges on the machine #RequireAdmin Global $fileName = @ScriptDir & '\test.txt' ;hostlist, one host per line readHostList() ;Read list of hosts Func readHostList() Local $file = FileOpen($fileName, 0) While 1 $line = FileReadLine($file) If @error = -1 Then ExitLoop ConsoleWrite($line & @CRLF) ;MsgBox(0,0,$line) getLocalAdmins($line) WEnd FileClose($file) EndFunc ;run PSEXEC to list local admins Func getLocalAdmins($remotePC) Local $testFile = @ScriptDir &'\test234.txt' FileOpen($testFile, 1) Local $psexec = 'psexec \\' & $remotePC & ' net localgroup administrators' FileWriteLine($testFile, _RunCmd($psexec) ) FileClose($testFile) EndFunc ;Used to return CLI output Func _RunCmd($sCommand) Local $nPid = Run(@Comspec & " /c" & $sCommand, @SystemDir, @SW_Hide, 8), $sRet = "" If @Error then Return "ERROR:" & @ERROR ProcessWait($nPid) While 1 $sRet &= StdoutRead($nPID) If @error Or (Not ProcessExists ($nPid)) Then ExitLoop WEnd Return $sRet EndFunc  
      ## If i manually run the command on the remote PC via PSEXEC I will get the following output: 
      PsExec v2.11 - Execute processes remotely
      Copyright (C) 2001-2014 Mark Russinovich
      Sysinternals - www.sysinternals.com
      Starting net on PCNAME... on PCNAME...
      net exited on PCNAME with error code 0.
      -------------------------------------------------------------------------------
      admin
      Administrator
      Alias name     administrators
      Domain\Domain Admins
      Comment        Administrators have complete and unrestricted access to the computer/domain
      Members
      The command completed successfully.
       
      ## The returned output from running the above script is as follows:
      PsExec v2.11 - Execute processes remotely
      Copyright (C) 2001-2014 Mark Russinovich
      Sysinternals - www.sysinternals.com
      Alias name     administrators
      Connecting to PCNAME...
      Starting PSEXESVC service on PCNAME...
      Connecting with PsExec service on PCName...
      Starting net on PCNAME..
      net exited on PCNAME with error code 0.
       
      **Note to test this script PSEXEC must be in the system dir or the path in the script changed 
      PSEXEC tool: https://docs.microsoft.com/en-us/sysinternals/downloads/psexec
    • rootx
      By rootx
      I need help to understand why the run function does not work, but if i use it directly inside cmd shell the command works. $where is the name of the jpg file
      ShellExecute(@ComSpec ," /k C:\Users\root\Desktop\test\exiftool.exe -p $FileName "&'"'&$where&'"'&" > "&'"'&@ScriptDir&"\out.txt"&'"',@ScriptDir);<----- work Run("C:\Users\root\Desktop\test\exiftool.exe -p $FileName "&$where&" > "&@ScriptDir&"\out.txt",@ScriptDir);<--- doesn't work I would use the run command!
      Thx
    • nacerbaaziz
      By nacerbaaziz
      Hello
      How are you
      I have a question, please.
      I've created a window and i need
      when i run the script if it detect an other copy from the same script was runing
       the script close the first copy Automatically and run a new copy
      I want an example to understand it