How to set task scheduler's arguments from Power Shell [closed] - powershell

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to set "Add arguments" in the task scheduler.
Because we have a lot of windows servers and have to set add arguments on each server.
I know it has to manage command for task scheduler "tasks" but I don't know how to add only "Add arguments".
I want to know the command that can do what I want to do (first sentence).
Please ask me anything what you want to know about this problem. Thank you.

Learn by example (copied & pasted from an open elevated cmd window; note that ^^> is my admin command prompt):
^^> schtasks /query /TN SO_31969962 /V /FO LIST | findstr /R /C:"^Task To Run:" /C:"Start In"
Task To Run: D:\bat\SO\31969962.bat "1 st" second
Start In: D:\bat\SO\files
^^> schtasks /change /TN "\SO_31969962" /TR "D:\bat\SO\31969962.bat \"first\" second"
SUCCESS: The parameters of scheduled task "\SO_31969962" have been changed.
^^> schtasks /query /TN SO_31969962 /V /FO LIST | findstr /R /C:"^Task To Run:" /C:"Start In"
Task To Run: D:\bat\SO\31969962.bat "first" second
Start In: N/A
^^>
Here the Task To Run: … line corresponds to
Unfortunately, schtasks.exe fails in specifying “start-in” directory as you can see in above example (read entire thread of this link, google for schtasks start in directory).
Following PowerShell code snippet changes both Arguments and WorkingDirectory:
$Task = Get-ScheduledTask -TaskPath '\' -TaskName 'SO_31969962'
$Task.Actions[0].Arguments = 'bubu "foo bar"'
$Task.Actions[0].WorkingDirectory = '"D:\bat\Unusual Names"'
Set-ScheduledTask -InputObject $Task | Out-Null
Edit: following commented batch script shows possible approach of how-to construct a valid PowerShell one-line command (no need to run an existing .ps1 script):
#ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
rem related to D:\PShell\SO\41677069_ScheduledTask_Admin.ps1
rem show current parameters of a task (before change)
schtasks /query /TN "\SO_31969962" /V /FO LIST | findstr /R /C:"^Task To Run:" /C:"^Start In"
rem set auxiliary variables (note properly escaped inner double quotes)
set "_taskGet=$Task = Get-ScheduledTask -TaskPath '\' -TaskName 'SO_31969962'"
set "_taskArg=$Task.Actions[0].Arguments = '\""foo bar\"" bubu'"
set "_taskDir=$Task.Actions[0].WorkingDirectory = '\""D:\odds and ends\""'"
set "_taskSet=Set-ScheduledTask -InputObject $Task"
rem apply auxiliary variables (used merely to keep next line readable)
PowerShell -ExecutionPolicy Bypass -command "%_taskGet%;%_taskArg%;%_taskDir%;%_taskSet%"
rem show current parameters of a task (after change)
schtasks /query /TN "\SO_31969962" /V /FO LIST | findstr /R /C:"^Task To Run:" /C:"^Start In"
Result (read powershell /? or Get-Help 'about_powershell.exe' -ShowWindow; read also about_Execution_Policies):
^^> powershell -ExecutionPolicy Bypass -File "D:\PShell\SO\41677069_ScheduledTask_Admin.ps1"
^^> D:\bat\SO\41677069_ScheduledTask_Admin.bat
Task To Run: D:\bat\SO\31969962.bat bubu "foo bar"
Start In: "D:\bat\Unusual Names"
TaskPath TaskName State
-------- -------- -----
\ SO_31969962 Disabled
Task To Run: D:\bat\SO\31969962.bat "foo bar" bubu
Start In: "D:\odds and ends"
^^>

You'll need to modify the task action, not the task itself:
# Retrieve task
$Task = Get-ScheduledTask -TaskName "myScheduledTask"
# Retrieve action, modify argument
$Task.Actions[0] = "new arguments string go here"
# Update task
Set-ScheduledTask $Task

Related

passing quoted arguments from batch file to `powershell start` - self-elevation on demand

I am writing a Windows batch file that automatically escalates itself to administrative permissions, provided the user clicks "Yes" on the User Access Control dialog that appears.
I am using a technique I learned here to detect whether we already have admin rights and another from here to escalate. When appropriate, the following script, let's call it foo.bat, re-launches itself via a powershell-mediated call to runas:
#echo off
net session >NUL 2>NUL
if %ERRORLEVEL% NEQ 0 (
powershell start -wait -verb runas "%~dpfx0" -ArgumentList '%*'
goto :eof
)
echo Now we are running with admin rights
echo First argument is "%~1"
echo Second argument is "%~2"
pause
My problem is with escaping quotes in the -ArgumentList. The code above works fine if I call foo.bat one two from the command prompt, but not if one of the arguments contains a space, for example as in foo.bat one "two three" (where the second argument should be two words, "two three").
If I could even just get the appropriate behavior when I replace %* with static arguments:
powershell start -wait -verb runas "%~dpfx0" -ArgumentList 'one "two three"'
then I could add some lines in foo.bat that compose an appropriately-escaped substitute for %*. However, even on that static example, every escape pattern I have tried so far has either failed (I see Second argument is "two" rather than Second argument is "two three") or caused an error (typically Start-Process: A positional parameter cannot be found that accepts argument 'two'). Drawing on the docs for powershell's Start-Process I have tried all manner of ridiculous combinations of quotes, carets, doubled and tripled quotes, backticks, and commas, but there's some unholy interaction going on between batch-file quoting and powershell quoting, and nothing has worked.
Is this even possible?
You've run into a perfect storm of two quoting hells (cmd and PowerShell), garnished with a PowerShell bug (as of PowerShell Core 6.2.0).
To work around the bug, the batch file cannot be reinvoked directly and must instead be reinvoked via cmd /c.
LotPings' helpful answer, which takes that into account, typically works, but not in the following edge cases:
If the batch file's full path contains spaces (e.g., c:\path\to\my batch file.cmd)
If the arguments happen to contain any of the following cmd metacharacters (even inside "..."): & | < > ^; e.g., one "two & three"
If the reinvoked-with-admin-privileges batch file relies on executing in the same working directory it was originally called from.
The following solution addresses all these edge cases. While it is far from trivial, it should be reusable as-is:
#echo off
setlocal
:: Test whether this invocation is elevated (`net session` only works with elevation).
:: If already running elevated (as admin), continue below.
net session >NUL 2>NUL && goto :elevated
:: If not, reinvoke with elevation.
set args=%*
if defined args set args=%args:^=^^%
if defined args set args=%args:<=^<%
if defined args set args=%args:>=^>%
if defined args set args=%args:&=^&%
if defined args set args=%args:|=^|%
if defined args set "args=%args:"=\"\"%"
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
" Start-Process -Wait -Verb RunAs -FilePath cmd -ArgumentList \"/c \"\" cd /d \"\"%CD%\"\" ^&^& \"\"%~f0\"\" %args% \"\" \" "
exit /b
:elevated
:: =====================================================
:: Now we are running elevated, in the same working dir., with args passed through.
:: YOUR CODE GOES HERE.
echo First argument is "%~1"
echo Second argument is "%~2"
pause
This is my batch for that purpose:
::ElevateMe.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#echo off & setlocal EnableExtensions DisableDelayedExpansion
Set "Args=%*"
net file 1>nul 2>&1 || (powershell -ex unrestricted -Command ^
Start-Process -Verb RunAs -FilePath '%comspec%' -ArgumentList '/c %~f0 %Args:"=\""%'
goto :eof)
:: Put code here that needs elevation
Echo:%*
Echo:%1
Echo:%2
Pause
Sample output:
one "two three"
one
"two three"
Drücken Sie eine beliebige Taste . . .
If you want the elevated cmd to stay open, use -ArgumentList '/k %~f0 %Args:"=\""%
The only approved way to elevate is to use a manifest. This emulates Unix's SUDO.EXE.
To run a command and stay elevated
RunAsAdminconsole <Command to run>
To elevate current cmd window or create a new elevated one
RunAsAdminconsole
From https://pastebin.com/KYUgEKQv
REM Three files follow
REM RunAsAdminConsole.bat
REM This file compiles RunAsAdminconsole.vb to RunAsAdminconsole.exe using the system VB.NET compiler.
REM Runs a command elevated using a manifest
C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc "%~dp0\RunAsAdminconsole.vb" /win32manifest:"%~dp0\RunAsAdmin.manifest" /out:"%~dp0\RunAsAdminConsole.exe" /target:exe
REM To use
rem RunAsAdminconsole <Command to run>
pause
RunAsAdmin.manifest
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="*"
name="Color Management"
type="win32"
/>
<description>Serenity's Editor</description>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
'RunAsAdminConsole.vb
'Change cmd /k to cmd /c to elevate and run command then exit elevation
imports System.Runtime.InteropServices
Public Module MyApplication
Public Sub Main ()
Dim wshshell as object
WshShell = CreateObject("WScript.Shell")
Shell("cmd /k " & Command())
End Sub
End Module
----------------------------------------

generating batch file to back up enabled/disabled state of Scheduled Tasks

From a batch file I am trying to:
Capture the task name and enabled/disabled state of all scheduled tasks in Windows 10 x64
Export these to a second batch file with TASKNAME and SCHEDULED_TASK_STATE populated in the following command for each task:
schtasks /Change /TN "\TASKNAME" /SCHEDULED_TASK_STATE
The idea is for this second batch file to serve as a one-click backup/restore of the enabled/disabled state of all existing tasks. This way, the cumbersome process of selectively enabling/disabling tasks one-by-one via a GUI tool (e.g., Task Scheduler, Autoruns, taskschedulerview-x64, etc.) can be easily undone (or redone).
For the source batch file, the command
SCHTASKS /Query /FO LIST /v
... will retrieve a list of all tasks and the two values I am interested in -- 'TaskName' and 'Scheduled Task State.'
With info gleaned from other stackoverflow-ers, below is as far as I have gotten using a temp files approach, but it's only half done and I'm in way over my head :)
echo
:: Get list and parameters of all tasks; find values for TaskName; save values to temp file 1
SCHTASKS /Query /FO LIST /v| findstr /r /C:"TaskName: " >"%USERPROFILE%\Desktop\1-SCHTASKS_LIST_QUERY_NAMES.TXT"
:: Re-get list and parameters of all tasks; find values for Scheduled Task State; save values to temp file 2
SCHTASKS /Query /FO LIST /v| findstr /r /C:"Scheduled Task State: " >"%USERPROFILE%\Desktop\2-SCHTASKS_LIST_QUERY_STATES.TXT"
:: Set delimiter/assign variable to TaskName values in temp file 1; populate schtasks/change command with value; (missing steps to get/populate task state); save to target batch file
for /f "tokens=2 delims=: " %%n in (%USERPROFILE%\Desktop\1-SCHTASKS_LIST_QUERY_NAMES.TXT) do #echo schtasks /Change /TN "%%n">>"%USERPROFILE%\Desktop\BACKUPS\BACKEDUP_scheduled_tasks_state.bat"
pause
Rather than the above, I would prefer if the source batch file:
Executes the command SCHTASKS /Query /FO LIST /v
Does all processing/parsing in-place, i.e., assign variables to the two values I need for each task - 'TaskName' and 'Scheduled Task State' -- without creating temp files, and
Inserts these values to 'schtasks /Change' commands for all tasks and export to the target batch file.
Any help will be much appreciated.
As you need only taskname and status, you don't need /v (verbose)
schtasks will then output taskname, next run time and status.
Get a format of csv instead of list, so each task is on only one line (much easier to parse).
Use find to exclude the header lines.
use a for loop around to get the desired tokens (1=taskname, 3=status) and set them into variables.
#echo off
setlocal enabledelayedexpansion
set n=0
for /f "tokens=1,3 delims=," %%a in ('schtasks /query /fo csv^|find "\"') do (
set /a n+=1
set name[!n!]=%%~na
set state[!n!]=%%~b
)
echo ----- %n% tasks found -----
rem set name
rem set state
set /a num=%random% %% %n% +1
echo example: !name[%num%]! has status of !state[%num%]!
delayed expansion is used, because variables (n) are changed and used within a code block.
In regards to #Stephan's answer below....
COMMENTS
The task 'Status' value (as used by Stephan) is a little different than 'Scheduled Task State.' In order to export the command 'schtasks /Change /TN' in the backup batch file I'm trying to create, I need the 'Scheduled Task State' at the end of the line.
The task state is only available with verbose results; as such I've changed line 4 of Stephan's code to be:
:: Added /v (verbose); changed tokens to "2" (for task name in CSV) and "12" (for task state in CSV)
for /f "tokens=2,12 delims=," %%a in ('schtasks /query /fo csv /v^|find "\"') do (
PENDING
I am still unsure of how to append Stephan's modified code in order to:
Reference his variables to populate the command 'schtasks /Change /TN "\TASKNAME" /SCHEDULED_TASK_STATE,' and
Export a batch file with this command for every scheduled task in the current system (as a backup/restore file). Example of how this exported batch file would look:
schtasks /Change /TN "GoogleUpdateTaskMachineUA" /DISABLE
schtasks /Change /TN "GoogleUpdateTaskMachineCore" /DISABLE
schtasks /Change /TN "DropboxUpdateTaskMachineCore" /DISABLE
schtasks /Change /TN "DropboxUpdateTaskMachineUA" /DISABLE
schtasks /Change /TN "\Microsoft\Windows\Application Experience\ProgramDataUpdater" /DISABLE
...but including all tasks on the current system.
***** UPDATE *****
I've realized another issue: the command to get task states uses the language "Enabled" or "Disabled" (with "d" at the end); but the command to set a task's state needs the language "ENABLE" or "DISABLE" (neither with "d" at the end). So, the plot thickens: how to also delete the unnecessary end-of-value "d" when outputting to the target batch file?

Zabbix script timeout

I'm trying to run powershell script for cleaning disk on agent from Zabbix. Seems that script is running too long and instead response I got error.
powershell -NonInteractive C:\Scripts\CleanDisk.ps1 -deleteLogsOlderThanDays 10
Script deletes some logs and temp folders and prints statistics. If there is only few folders to delete it work fines. But if script runs too long then dialog windows with script result ends with error Get value from agent failed: ZBX_TCP_READ
Guess that it's because connection to client timeout. Is there some way how to get over this limitation?
Thx
My colleagues found usable workaround. Instead of starting script which will run long time it's better to only schedule script with schtask.exe. So I modified script, now it contains two parts. One is responsible for scheduling and starting scheduled task (schedule it self but with different arguments), second heavy and long running does the action. Result of scheduling will appear in execution script dialog box in zabbix, result of long running action is going to log file...
Here is example of powershell script StartCleanDisk.ps1. In this case task will be scheduled and immediately executed by scheduler.
StartCleanDisk.ps1 -deleteLogsOlderThanDays 10 -startAsTask 1
In this case task will directly executed.
StartCleanDisk.ps1 -deleteLogsOlderThanDays 10 -startAsTask 0
StartCleanDisk.ps1 content:
[CmdletBinding()]
param
(
[parameter(Mandatory=$true)]
[int]
$deleteLogsOlderThanDays,
[bool]
$startAsTask = $false
)
if ($startAsTask)
{
Write-Output "Scheduling task for cleaning disk ...";
$taskname = "CleanDisk"
$logFile = "X:\logs\Tasks\cleaningDisk.log";
$task = "powershell $PSScriptRoot\StartCleanDisk.ps1 -deleteLogsOlderThanDays $deleteLogsOlderThanDays -startAsTask 0 > $logFile 2>&1";
& schtasks /create /ru "System" /tn $taskname /tr $task /sc once /ST 23:59 /F /V1 /Z;
Write-Output "Task Clean disk created...";
& schtasks /run /tn $taskname;
Write-Output "Task $taskname started... Please chek $logFile";
exit 0;
}
#####################
# Script begins here
# PUT HERE COMMANDS FOR DELETING
#####################
Start as a hidden process
UserParameter=Key[*],powershell -NoProfile -ExecutionPolicy Bypass Start-Process powershell -ArgumentList \\Path\SYSVOL\..\scripts\Files\Zabbix\Key.$1.ps1 "$2" "$3" -Windowstyle Hidden
Where Key[*] is an active item in Zabbix agent for start powershell at windows host,
$1 for variation of name PowerShell scrips, "$2" "$3" "next" for any parameters
if $1="Ring" then the name of the script will Key.Ring.ps1

2 url's in powershell with cmd

I have create the following schedule task but I would like to add a second url which will run after the end of the first. Could anyone help με how can I do it??
Thanks in advance.
schtasks /create /tn "My Task Title" /tr "powershell -ExecutionPolicy unrestricted -Command \"(New-Object Net.WebClient).DownloadString(\\\"Url1\\\")\"" /sc DAILY /ru username /rp pass
If you have multiple URLs it would be best to move some of the logic into a PowerShell script file to push some of the logic away from the command line. Not that you could put this all in the command line but for ease and readability using the -File parameter of powershell would be a better way to go. First you would need to create a file called "Get-WebStrings.ps1" with the following contents.
# Check to be sure we have at least one argument.
If($args.Count -gt 0){
# Treat each argument as a URL that we need to download.
ForEach($singleURL in $args){
# Download the string
(New-Object Net.WebClient).DownloadString($singleURL)
# Optional depending on your needs
# (New-Object Net.WebClient).DownloadString($singleURL) | Out-Null
}
}
What this script will do it take the arguments sent to it as treat each one as a url and download the data. By default this would output to console. If merely performing the download is all you wish then you could pipe the output into Out-Null (see commented code above).
Next you would need to create the task in the command line much like you have already done. Note that this file needs to be accessible on the local system where the task is being called! Each URL is placed inside single quotes in the string.
schtasks /create /tn "My Task Title" /tr "powershell -ExecutionPolicy unrestricted -File 'C:\Temp\Get-WebStrings.ps1' 'https://www.google.com' 'http://www.purple.com'" /sc daily /ru username /rp pass
If you were to look at the Action inside Task Scheduler it would look like this after that was run.
-ExecutionPolicy unrestricted -File "C:\Temp\Get-WebStrings.ps1" "https:\\www.google.com" "https:\\employee.firstair.ca"

How to execute a scheduled task with "schtasks" without opening a new command line window?

I have a batch file that creates a scheduled task using schtasks like this:
schtasks /create /tn my_task_name
/tr "...\my_path\my_task.bat"
/sc daily
/st 10:00:00
/s \\my_computer_name
/u my_username
/p my_password
It works OK except the fact that when my_task.bat is executed - a new command line window is opened (and closed after execution).
I would like to avoid opening this new window (i.e. to run the task in quiet mode, in the background).
I thought to use
start /b ...\my_path\my_task.bat
but I don't know how, because since I have to call start from the batch file I need to precede it with cmd /c, which again causes the new window to open.
How could I solve this problem ?
You can do this by specifying /RU option for schtasks. This option
specifies the user account (user
context) under which the task runs.
For the system account, valid values
are "", "NT AUTHORITY\SYSTEM" or
"SYSTEM".
And thus, try this
schtasks /create /tn my_task_name
....
/st 10:00:00
/ru "SYSTEM"
....
You can use the Windows Shell Scripting extensions to execute a batch file in invisible mode.
Create a plain text file and name it <scriptname>.vbs
Paste the following code in the .vbs file
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0
Set WshShell = Nothing
Change the name and path of you batch file according to your need
Save the .vbs file and schedule the same in schtasks