none
Is it possible to disable "the run on AC power" using schtasks.exe cmdline switches RRS feed

  • Question

  • Hello all,

    I was writing some automation which was using schtasks.exe to schedule various events.  After wondering why my events were not launching I have identified that it is because all my systems run on battery and by default when you use schtasks.exe it set the Power Conditions to "Start the task only if the computer is on AC power"  Unfortunately after searching and reading technet, I have a bad feeling I cannot programmaticlly disable this condition at the command line.  Is there a way?  Also if there is not, is there something I can set in the registry to remove this as a default. 

    All my systems which my automation will be used for will be runing on battery.  Thus this is a killer, and I don't want to have to hand drive the UI, since my script needs to support all Windows OSes

    I have the same problem on Vista (only Windows XP is working, as it doesn't have this Power Condition).

    Thanks for reading and the help.
    Friday, February 26, 2010 6:04 PM

Answers

  • Hello cshorey,
    Not seeing an obvious commandline switch for this option.
    I did notice the option to point to an XML file.
    So it appears you can create an XML file with options in it.
    I used the GUI to create a task which does have the option, then queried that task with the xml switch to see what how the XML options were listed.
    Thanks, Darrell Gorter This posting is provided "AS IS" with no warranties, and confers no rights
    Saturday, February 27, 2010 8:12 AM

All replies

  • Hello cshorey,
    Not seeing an obvious commandline switch for this option.
    I did notice the option to point to an XML file.
    So it appears you can create an XML file with options in it.
    I used the GUI to create a task which does have the option, then queried that task with the xml switch to see what how the XML options were listed.
    Thanks, Darrell Gorter This posting is provided "AS IS" with no warranties, and confers no rights
    Saturday, February 27, 2010 8:12 AM
  • Hi Darrell,

    Thanks for the reply.  Yes I saw the XML option and I might have to consider it.  I am writing some automation, which needs to support XP, Vista, and Win7.  Thus, I wanted just build a command line execution string on the fly based off the OS, but I guess I could build an XML on the fly and then feed it via command line  for Vista and Win7.  That's a better option then trying to drive the UI.  I guess I am just disappointed that there are options which cannot be used via the command line switches.

    Thanks again. 

    Now I just need to figure out why I am having a 15% failure on my Win7 tasks not running as expected.

    chris
    Monday, March 1, 2010 6:50 PM
  • That worked in my case. I created the task manually, then right click on task in scheduler GUI and export to XML. Then later use the "/XML" from commandline.

    SchTasks /Create /TN "TASKNAME" /XML "PATH/FILE.xml"

    Thanks! helped a lot with automation..

    Monday, October 22, 2012 9:46 PM
  • This PS1 script creates a task via an xml, that runs powershell code and then deletes the task. You can edit any parameters you want for the task via the xml that's created and then deleted and of course the ps1 file that the task runs, you have to write yourself.

    #Region File browser
    
    Add-Type -AssemblyName System.Windows.Forms
    $message = "In the following window you must browse to the permanent location of the ooo.ps1 file";
    $title = "Find the location of the .ps1 file";
    $btn = [System.Windows.Forms.MessageBoxButtons]::OK;
    
    [System.Windows.Forms.MessageBox]::Show($message,$title,$btn)
    
    $fileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
        Multiselect = $false # Multiple files can be chosen
    	Filter = 'Script (*.ps1)|*ps1' # Specified file types
    }
     
    [void]$fileBrowser.ShowDialog();
    
    #Endregion /File browser
    
    
    #Region Global variables
    
    $xmlPath = "c:\temp\oooConfig.xml";
    $filePath = $fileBrowser.FileNames;
    $identity = [System.Security.Principal.WindowsIdentity]::GetCurrent();
    [String] ${userDomain},[String]  ${userAccount} = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name.split("\");
    
    #Endregion /Global variables
    
    
    #Region Write xml 
    
    [System.XML.XMLDocument]$oXMLDocument = New-Object System.XML.XMLDocument;
    $declaration = $oXMLDocument.CreateXmlDeclaration("1.0", "UTF-16", $null);
    [System.XML.XMLElement]$oXMLRoot = $oXMLDocument.CreateElement("Task");
    $oXMLDocument.InsertBefore($declaration, $oXMLRoot.DocumentElement);
    $oXMLDocument.appendChild($oXMLRoot);
    $oXMLRoot.SetAttribute("version","1.2");
    $oXMLRoot.SetAttribute("xmlns", "http://schemas.microsoft.com/windows/2004/02/mit/task");
    
    #RegistrationInfo
    [System.XML.XMLElement]$oXMLRegistrationInfo=$oXMLRoot.appendChild($oXMLDocument.CreateElement("RegistrationInfo"));
    #$oXMLSystem.SetAttribute("description","Document Management")
    [System.XML.XMLElement]$oXMLDate=$oXMLRegistrationInfo.appendChild($oXMLDocument.CreateElement("Date"));
    $oXMLDate.InnerText = "2019-04-08T14:21:26";
    [System.XML.XMLElement]$oXMLAuthor=$oXMLRegistrationInfo.appendChild($oXMLDocument.CreateElement("Author"));
    $oXMLAuthor.InnerText = $userAccount;
    
    #Triggers
    [System.XML.XMLElement]$oXMLTriggers=$oXMLRoot.appendChild($oXMLDocument.CreateElement("Triggers"));
    #no inner text
    [System.XML.XMLElement]$oXMLCalendarTrigger=$oXMLTriggers.appendChild($oXMLDocument.CreateElement("CalendarTrigger"));
    #no inner text
    [System.XML.XMLElement]$oXMLStartBoundary=$oXMLCalendarTrigger.appendChild($oXMLDocument.CreateElement("StartBoundary"));
    $oXMLStartBoundary.InnerText = "2019-04-08T07:00:00";
    [System.XML.XMLElement]$oXMLTriggerEnabled=$oXMLCalendarTrigger.appendChild($oXMLDocument.CreateElement("Enabled"));
    $oXMLTriggerEnabled.InnerText = "true";
    [System.XML.XMLElement]$oXMLTriggerScheduleByDay=$oXMLCalendarTrigger.appendChild($oXMLDocument.CreateElement("ScheduleByDay"));
    #no inner text
    [System.XML.XMLElement]$oXMLTriggerDaysInterval=$oXMLTriggerScheduleByDay.appendChild($oXMLDocument.CreateElement("DaysInterval"));
    $oXMLTriggerDaysInterval.InnerText = "1";
    
    #Principals
    [System.XML.XMLElement]$oXMLPrincipals=$oXMLRoot.appendChild($oXMLDocument.CreateElement("Principals"));
    #no inner text
    [System.XML.XMLElement]$oXMLPrincipal=$oXMLPrincipals.appendChild($oXMLDocument.CreateElement("Principal"));
    $oXMLPrincipal.SetAttribute("id","Author");
    [System.XML.XMLElement]$oXMLUserId=$oXMLPrincipal.appendChild($oXMLDocument.CreateElement("UserId"));
    $oXMLUserId.InnerText = $identity.Name;
    [System.XML.XMLElement]$oXMLLogonType=$oXMLPrincipal.appendChild($oXMLDocument.CreateElement("LogonType"));
    $oXMLLogonType.InnerText = "InteractiveToken";
    [System.XML.XMLElement]$oXMLRunLevel=$oXMLPrincipal.appendChild($oXMLDocument.CreateElement("RunLevel"));
    $oXMLRunLevel.InnerText = "HighestAvailable";
    
    #Settings
    [System.XML.XMLElement]$oXMLSettings=$oXMLRoot.appendChild($oXMLDocument.CreateElement("Settings"));
    #no inner text
    [System.XML.XMLElement]$oXMLMultipleInstancesPolicy=$oXMLSettings.appendChild($oXMLDocument.CreateElement("MultipleInstancesPolicy"));
    $oXMLMultipleInstancesPolicy.InnerText = "IgnoreNew";
    [System.XML.XMLElement]$oXMLDisallowStartIfOnBatteries=$oXMLSettings.appendChild($oXMLDocument.CreateElement("DisallowStartIfOnBatteries"));
    $oXMLDisallowStartIfOnBatteries.InnerText = "false";
    [System.XML.XMLElement]$oXMLStopIfGoingOnBatteries=$oXMLSettings.appendChild($oXMLDocument.CreateElement("StopIfGoingOnBatteries"));
    $oXMLStopIfGoingOnBatteries.InnerText = "false";
    [System.XML.XMLElement]$oXMLAllowHardTerminate=$oXMLSettings.appendChild($oXMLDocument.CreateElement("AllowHardTerminate"));
    $oXMLAllowHardTerminate.InnerText = "true";
    [System.XML.XMLElement]$oXMLStartWhenAvailable=$oXMLSettings.appendChild($oXMLDocument.CreateElement("StartWhenAvailable"));
    $oXMLStartWhenAvailable.InnerText = "false";
    [System.XML.XMLElement]$oXMLRunOnlyIfNetworkAvailable=$oXMLSettings.appendChild($oXMLDocument.CreateElement("RunOnlyIfNetworkAvailable"));
    $oXMLRunOnlyIfNetworkAvailable.InnerText = "false";
    [System.XML.XMLElement]$oXMLIdleSettings=$oXMLSettings.appendChild($oXMLDocument.CreateElement("IdleSettings"));
    #no inner text
    [System.XML.XMLElement]$oXMLIdleSettingsStopOnIdleEnd=$oXMLIdleSettings.appendChild($oXMLDocument.CreateElement("StopOnIdleEnd"));
    $oXMLIdleSettingsStopOnIdleEnd.InnerText = "true";
    [System.XML.XMLElement]$oXMLIdleSettingsRestartOnIdle=$oXMLIdleSettings.appendChild($oXMLDocument.CreateElement("RestartOnIdle"));
    $oXMLIdleSettingsRestartOnIdle.InnerText = "false";
    [System.XML.XMLElement]$oXMLAllowStartOnDemand=$oXMLSettings.appendChild($oXMLDocument.CreateElement("AllowStartOnDemand"));
    $oXMLAllowStartOnDemand.InnerText = "true";
    [System.XML.XMLElement]$oXMLSettingsEnabled=$oXMLSettings.appendChild($oXMLDocument.CreateElement("Enabled"));
    $oXMLSettingsEnabled.InnerText = "true";
    [System.XML.XMLElement]$oXMLSettingsHidden=$oXMLSettings.appendChild($oXMLDocument.CreateElement("Hidden"));
    $oXMLSettingsHidden.InnerText = "false";
    [System.XML.XMLElement]$oXMLSettingsRunOnlyIfIdle=$oXMLSettings.appendChild($oXMLDocument.CreateElement("RunOnlyIfIdle"));
    $oXMLSettingsRunOnlyIfIdle.InnerText = "false";
    [System.XML.XMLElement]$oXMLSettingsWakeToRun=$oXMLSettings.appendChild($oXMLDocument.CreateElement("WakeToRun"));
    $oXMLSettingsWakeToRun.InnerText = "true";
    [System.XML.XMLElement]$oXMLSettingsExecutionTimeLimit=$oXMLSettings.appendChild($oXMLDocument.CreateElement("ExecutionTimeLimit"));
    $oXMLSettingsExecutionTimeLimit.InnerText = "P3D";
    [System.XML.XMLElement]$oXMLSettingsPriority=$oXMLSettings.appendChild($oXMLDocument.CreateElement("Priority"));
    $oXMLSettingsPriority.InnerText = "7";
    
    #Actions
    [System.XML.XMLElement]$oXMLActions=$oXMLRoot.appendChild($oXMLDocument.CreateElement("Actions"));
    $oXMLActions.SetAttribute("Context","Author");
    [System.XML.XMLElement]$oXMLActionsExec=$oXMLActions.appendChild($oXMLDocument.CreateElement("Exec"));
    #no inner text
    [System.XML.XMLElement]$oXMLActionsCommand=$oXMLActionsExec.appendChild($oXMLDocument.CreateElement("Command"));
    $oXMLActionsCommand.InnerText = "powershell";
    [System.XML.XMLElement]$oXMLActionsArguments=$oXMLActionsExec.appendChild($oXMLDocument.CreateElement("Arguments"));
    $oXMLActionsArguments.InnerText = "-noprofile -executionpolicy bypass -file $filePath";
    
    $oXMLDocument.Save($xmlPath);
    
    #Endregion /Write xml
    
    
    #Region Create Task
    
    If($fileBrowser.FileNames -like "*\*") {
    
    	Write-Host $fileBrowser.FileNames;
    	#Schtasks /create  /SC daily  /TN "OOO Scheduler2" /RU $identity.Name /ST 11:00 /TR "powershell -noprofile -executionpolicy bypass -file $filePath";
    	Schtasks /create /XML "c:\temp\xmlConfig.xml" /tn "Task Name";	
    }
    else 
    {
        Write-Host "Cancelled by user"
    }
    
    #Endregion /Create Task
    
    
    [IO.File]::Delete($xmlPath);

    Tuesday, April 9, 2019 6:32 PM