Examples:
SC GetKeyName "task scheduler"
SC GetDisplayName schedule
SC start schedule
SC QUERY schedule
SC QUERY type= driver
SC QUERY state= all |findstr "DISPLAY_NAME STATE" >svc_installed.txt
SC \\myServer CONFIG myService obj= LocalSystem password= mypassword
SC CONFIG MyService binPath=c:\myprogram.exe obj=".\LocalSystem"
password=""
Watch out for extra spaces:
SC QUERY state= all
Works
SC QUERY sTate =all
Fails!
“There is always room at the top” ~ Daniel Webster
SCHTASKS
Create, delete, edit, list, start or stop a scheduled task. The task can be created on the local or a
remote computer.
Syntax:
SCHTASKS /Create [ Connect_Options] Create_Options /TN taskname
SCHTASKS /Delete [ Connect_Options] /TN taskname [/F]
SCHTASKS /Query [ Connect_Options] [/FO format] [/NH] [/V]
SCHTASKS /Run [ Connect_Options] /TN taskname
SCHTASKS /End [ Connect_Options] /TN taskname
SCHTASKS /Change [ Connect_Options] {[/RU username] [/RP password]
[/TR taskrun]} /TN taskname
Connect_Options:
/S system # Remote system (default is local)
[/U username [/P password]] # Submit job under this name
Create_Options:
/TR taskrun # Pathname of the executable to run
/ST starttime # HH:MM (24 hour)
[/RU username [/RP password]] # Run job as this user
/SC schedule [/MO modifier] # When to run, see below
[/D day] # Day = MON,TUE,WED,THU,FRI,SAT,SUN
[/M months] #
Month=JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC.
[/I idletime] # 1 - 999 minutes (ONIDLE task
only)
[/SD startdate] [/ED enddate] # Start and end date "dd/mm/yyyy"
options:
/TN A name for the task
/F Force delete, ignore warnings even if the task is currently
runnning.
/FO Output format: TABLE, LIST, CSV
/NH No header
/V Verbose output
“We don‟t wake up for less than $10,000 a day” ~ Linda Evangelista
SET
Display, set, or remove CMD environment variables. Changes made with SET will remain only
for the duration of the current CMD session.
Syntax
SET variable
SET variable= string
SET /A "variable= expression"
SET " variable="
SET /P variable=[ promptString]
SET "
Key
variable : A new or existing environment variable name e.g. _num
string : A text string to assign to the variable.
expression : Arithmetic expression
Arithmetic expressions (SET /a)
The expression to be evaluated can include the following operators:
+ Add set /a "_num=_num+5"
+= Add variable set /a "_num+=5"
- Subtract (or
unary
)set /a "_num=_num-5"
-= Subtract variable set /a "_num-=5"
* Multiply set /a "_num=_num*5"
*= Multiply variable set /a "_num*=5"
/ Divide set /a "_num=_num/5"
/= Divide variable set /a "_num/=5"
%
Modulus
set /a "_num=5%%2"
! Logical negation 0 (FALSE)
⇨ 1 (TRUE) and any non-zero value
(TRUE)
⇨ 0 (FALSE)
~
One's complement
(bitwise negation)
& AND set /a "_num=5&3" 0101
AND
0011 = 0001
(decimal 1)
&= AND variable set /a "_num&=3"
| OR set /a "_num=5|3" 0101
OR
0011 = 0111
(decimal 7)
|= OR variable set /a "_num|=3"
^ XOR set /a "_num=5^3" 0101
XOR
0011 = 0110
(decimal 6)
^= XOR variable set /a "_num=^3"
<< Left
Shift
. (sign bit
⇨ 0)
>> Right
Shift
. (Fills in the sign bit such that a negative
number always remains negative.)
Neither ShiftRight nor ShiftLeft will detect
overflow.
<<= Left Shift variable set /a _num<<=2
>>= Right Shift variable set /a _num>>=2
( ) Brackets group expressions set /a "_num=(2+3)*5"
, Commas separate expressions set /a "_num=2,_result=_num*5"
See
Arithmetic
examples below and
this forum thread
for more.
also see
SetX
,
VarSearch
and
VarSubstring
for more on variable
manipulation.
Variable names are not case sensitive but the contents can be.
The number one problem people run into with SET is having extra spaces around either the
variable name or the
string
, SET is not forgiving of extra spaces like many other scripting
languages.
Variables can contain spaces, variable
names
can also contain spaces, but this is not
recommended.
Display a variable:
Type SET without parameters to display all the current environment variables.
Type SET with a variable name to display that variable
SET _department
or use ECHO:
ECHO [%_department%]
The SET command invoked with a string (and no equal sign) will display a wildcard list of all
matching variables
Display variables that begin with 'P':
SET p
Display variables that begin with an underscore
SET _
Set a variable:
Example of storing a text string:
C:\> SET _dept=Sales and Marketing
C:\> set _
_dept=Sales and Marketing
One variable can be based on another, but this is not dynamic
E.g.
C:\> set xx=fish
C:\> set msg=%xx% chips
C:\> set msg
msg=fish chips
C:\> set xx=sausage
C:\> set msg
msg=fish chips
C:\> set msg=%xx% chips
C:\> set msg
msg=sausage chips
Avoid starting variable names with a number, this will avoid the variable being mis-interpreted as
a
parameter
%123_myvar% < > %1 23_myvar
To display undocumented system variables:
SET "
Prompt for user input
The /P switch allows you to set a variable equal to a line of input entered by the user.
The Prompt string is displayed before the user input is read.
@echo off
Set /P _dept=Please enter Department || Set _dept=NothingChosen
If "%_dept%"=="NothingChosen" goto :sub_error
If /i "%_dept%"=="finance" goto sub_finance
If /i "%_dept%"=="hr" goto sub_hr
goto:eof
:sub_finance
echo You chose the finance dept
goto:eof
:sub_hr
echo You chose the hr dept
:sub_error
echo Nothing was chosen
The Prompt string can be empty. If the user does not enter anything (just presses return) then
the variable will be unchanged and an errorlevel will be set.
To place the first line of a file into a variable:
Set /P _MyVar= The
CHOICE
command is an alternative to SET /P (but accepts only one character/keypress.)
Variable names with spaces
A variable can contain spaces and also the variable name itself may contain spaces, therefore
the following assignment:
SET _var =MyText
will create a variable called
"_var "
- note the trailing space
To avoid problems with extra spaces, issue SET statements in parentheses, like this:
(SET _department=Some Text)
Alternatively:
SET "_department=Some Text"
Note: To actually include a bracket in the variable, use an
escape
character.
The SET command will set ERRORLEVEL to 1 if the variable name is not found in the current
environment.
This can be detected using the
IF
ERRORLEVEL command
Delete a variable
Type SET with just the variable name and an equals sign:
SET _department=
Better still, to be sure there is no trailing space after the = use:
(SET _department=)
or
SET "_department="
Arithmetic expressions (SET /a)
Placing expressions in "quotes" is optional for simple arithmetic but required for any expression
using logical operators.
Any SET /A calculation that returns a fractional result will be rounded down to the nearest whole
integer.
Examples:
SET /A "_result=2+4"
(=6)
SET /A "_result=5"
(=5)
SET /A "_result +=5"
(=10)
SET /A "_result=2 <<3"
(=16) { 2 Lsh 3 = binary 10 Lsh 3 = binary 10000 = decimal 16 }
SET /A "_result=5 %%2"
(=1) { 5/2 = 2 + 2 remainder 1 = 1 }
In a batch script, the Modulus operator (
%
) must be doubled up to (
%%
).
SET /A will treat any character string in the expression as an environment variable name. This
allows you to do arithmetic with environment variables without having to type any % signs to get
the values.
SET /A _result=5 + _MyVar
Chia sẻ với bạn bè của bạn: |