Category Archives: CLI Secrets

Migrate scheduled tasks from 2003 to 2008

Well, the time has come for me to learn at last syntax for schtasks what I was reluctant to do. Some time you have to migrate a task or a bunch of them from one computer to another. In my case it was even more “interesting” task: migrate some tasks from Windows Server 2003 box to Windows 2008 R2. If you have only one it is no problem to move it manually, but what if there are many of them? Here it is: the moment of schtasks’ triumph! =)

What it can do for us is to export 2003’s tasks into an XML file. Suppose we have task “Command Prompt” which launches cmd.exe once:

image

image

image

Now let’s use our secret weapon (run it from 2008 box):

image

In Task.xml we now have the following content:

   1: <?xml version="1.0" encoding="utf-16"?>

   2: <Task version="1.1" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">

   3:   <RegistrationInfo>

   4:     <Author>trofimov</Author>

   5:   </RegistrationInfo>

   6:   <Triggers>

   7:     <TimeTrigger>

   8:       <Enabled>true</Enabled>

   9:       <StartBoundary>2011-04-10T23:43:00</StartBoundary>

  10:     </TimeTrigger>

  11:   </Triggers>

  12:   <Settings>

  13:     <Enabled>true</Enabled>

  14:     <DeleteExpiredTaskAfter>PT0S</DeleteExpiredTaskAfter>

  15:     <ExecutionTimeLimit>PT259200S</ExecutionTimeLimit>

  16:     <Hidden>false</Hidden>

  17:     <WakeToRun>false</WakeToRun>

  18:     <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>

  19:     <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>

  20:     <RunOnlyIfIdle>false</RunOnlyIfIdle>

  21:     <Priority>5</Priority>

  22:     <IdleSettings>

  23:       <Duration>PT600S</Duration>

  24:       <WaitTimeout>PT3600S</WaitTimeout>

  25:       <StopOnIdleEnd>false</StopOnIdleEnd>

  26:       <RestartOnIdle>false</RestartOnIdle>

  27:     </IdleSettings>

  28:   </Settings>

  29:   <Principals>

  30:     <Principal>

  31:       <UserId>System</UserId>

  32:     </Principal>

  33:   </Principals>

  34:   <Actions>

  35:     <Exec>

  36:       <Command>C:WINDOWSsystem32cmd.exe</Command>

  37:       <WorkingDirectory>C:WINDOWSsystem32</WorkingDirectory>

  38:     </Exec>

  39:   </Actions>

  40: </Task>

which we can now import to our W2008R2 box with schtasks or even through GUI:

image

image

image

Of course, doing that with schtasks is more efficient way to import more than one task, but GUI is much more spectacular 😉

Advertisements

%System%System32 secrets: change

CLIAnother old-timer here. I cannot remember when I last used it, but I guess it still can be useful in a number of situations. For example I used to use it to install new software on a terminal server or to cease users logins to it. Now I usually don’t touch terminal servers and as far as I know they have other means to complete these tasks. Anyway, Windows 2003 is still in place and we still have the command around.

It can the following:

 

  • Change logon setting: we can turn new logons to the TS. Just change logon disable.
  • Change port mappings. I haven’t use it at all and I hope you won’t have to either, because KB article says: “Changes the COM port mappings to be compatible with DOS applications”. No way I want be anywhere near this stuff anymore =)
  • Prepare a TS for installation of a software. .ini files mapping and all that stuff. To install some software you need to change user install and you have to change user execute.

And this is all it can… But I remember what wonderful bugs you could get in case you didn’t know the command…

The only thing I don’t know how to explain is why it is still present on Windows 7? Does anyone know the answer? =)))

%SystemRoot%System32 Secrets: certreq

CLIThe next two candidates for the series from System32 folder were bootcfg and cacls (I’m going through them alphabetically). But they are deprecated and, what’s important too, I’ve managed to learn theirs new variants. Moreover, I’ve already described BCDEdit, which is a successor to bootcfg (and I’ve managed to learn how to use the new one ;)). Therefore I’m skipping these two commands and go straight to certreq command.

So, certreq. It is more for advanced admin use, then for general user. But still it is good to remember of it… Just in case you need to:

  • create new request for a certificate, which can be later submitted to a CA
  • submit the request to a CA
  • retrieve a certificate from a CA
  • sign a certificate request
  • and all other stuff to deal with certificates =)

Of course, it is scriptable, but, to be honest, I’ve used it several times so far. Still it can become handy in scripting, on helpdesk and on a disconnected from your network box. So, keep in mind 😉

Further reading:

Certreq Syntax

Extended explanation of it

Advanced Certificate Enrollment and Management

%SystemRoot%System32 secrets: BITSAdmin

CLIAnother deprecated friend of mine. But I still like it, really. First of all because I haven’t still found enough time to get acquainted with all that *-BITSTransfer PowerShell comandlets. Second… Well, there is nothing for the “second”, naturally =) But still – it is a great command and I’d like to make a tribute to it with this article, because it is AWESOME! It is soooo powerful! Even though I used it usually just to be sure I would download the file regardless of network loss or whatever, it can do much more. Download or upload, retry these tasks, get some part of the file, set myriads of parameters, including authentication, use peer caching… Wow! =)

But again, usually I used it to download large files. Let’s take a look at one example.

Lets start with creating a download job:

BITSAdmin /CREATE /DOWNLOAD DownloadJob1

image

You can see that the job has been created and it has been assigned some GUID you can use later (but we’ll use it’s name in this example). Also as you can see we are being constantly notified about the command deprecation =( Let’s take a look at the job:

BITSadmin /LIST /VERBOSE

image

(Yeah, a LOT of information). Obviously, the job is currently empty (FILES: 0 / 0), so let’s add some files to it:

BITSadmin /ADDFILE DownloadJob1 <URL> <PathToSavedFile>

image

Added successfully and created a temp file already:

image

Let’s add one more:

image

and look at the second temporary file:

image

They are both of 0 bytes size yet. Now, once we have two files for our job to download, we can get more info from the job:

image

Here we can see both our files (JOB FILES) and… Can we just wait till the files get downloaded? No, because the job is not started at the moment (STATE: SUSPENDED). We need to start it and this is easy:

BITSADMIN /RESUME DownloadJob1

image

Now the job is in TRANSFERRING state, we can see how many bytes (BYTES) or files (FILES) has been transferred and so on. On this point something goes wrong and we get our network disconnected: image. Is it a problem for our downloads? Yes:

image

their state is TRANSIENT_ERROR. Should we worry about it? No, because as soon as network restores we’ll get our job QUEUD and then resumed automatically:

image

Looking at this big picture from time to time reentering /LIST command is boring, so we’ll monitor it in other way:

BITSadmin /MONITOR /REFRESH 1

image

which will refresh the state for our jobs occasionally (each 1 second in the example):

image

As soon as we get our files transferred:

image

we can just go to our download location and… Oh… Wait… What’s that?

image

The files do have appropriate size but their names… They are still temporary =( But don’t worry, just one more little step:

BITSadmin /COMPLETE downloadJob1

image

Oops. Seems like BITSadmin treats job names as case-sensitive. We should remember this, so let’s rewrite it in the correct way:

BITSadmin /COMPLETE DownloadJob1

image

Here we are! The files are here and no more job to do! I’m loving it © =)

image

Forgive me for that useless lesson: just couldn’t resist it Winking smile

If you are as amazed, as I am, here is some reading.

%SystemRoot%System32 secrets: BCDEdit

CLI

Ok, next item in our list is not to be actually very much used. Troubleshooting OS boot, creating some boot options, that’s it. But actually it is worth knowing about it. Nevertheless, what you can do with it can be quite awesome… If you need it =)

For example, you can enable and configure EMS (Emergency Management Services) for any boot entry in your list. Or you can enable kernel debugging. Some wicked tongues tell that you can even arrange a dual boot with some other OS if you want. I’m going to check it one of these days… Someday =)

For further reading refer to these documents:

http://technet.microsoft.com/en-us/library/cc709667(WS.10).aspx

http://technet.microsoft.com/en-us/library/cc731662(WS.10).aspx

http://download.microsoft.com/download/9/c/5/9c5b2167-8017-4bae-9fde-d599bac8184a/BCDedit_reff.docx

%SystemRoot%System32 Secrets: Auditpol

CLIThis command is very useful in case you need to fine-tune audit. For example you cannot set “Audit directory service changes” without setting “Audit directory service replication” using only GUI, because “There is no Windows interface tool available in Windows Server 2008 to view or set audit policy subcategories”. therefore, you need auditpol badly in case you need to set those subcategories. You also need it in order to script changes to or audit of SACL. You need it also to backup or restore those policies quickly (say you need to turn some auditing settings on for some time and turn them off later). You also can fully reset auditing policy.

Wow! While writing the text I become filled with awe. I definitely should have used it more =)

Syntax is quite excessive, so I just show you a very simple example:

image

Have fun! =)

%SystemRoot%System32 Secrets: Schtasks

CLIAfter my previous post about AT command I received some feed back from people who obviously enough hadn’t read my post in its entirety =) The feedback stated that “AT is deprecated and is to be replaced with schtasks”. You bet I knew that! =)

Nevertheless, schtasks is really more powerful and since my article touched more than one heart I decided to write next message not about auditpol, which is next in my alphabetical list of interesting apps in System32 folder, but about schtasks. Let’s begin.

Schtasks

Comparing to AT it is a huge advancement. Really, here are its subcommands: create, change, run, end, delete, query. Actually it can do everything you can do through Scheduled Tasks applet in Control Panel. And since it is a CLI command, everything is scriptable. But as usual, there is a payback for the power: the syntax description consists of 33 pages in MS Word with the font size 8.5. 33 pages! Still, they recommend to switch from AT to this command and, to be honest, you don’t have much choice if you need just a bit more than AT can give you. Moreover, if you want to learn the syntax, it can be rewarding:

schtasks /create /tn “Shutdown Friends Machine” /tr “shutdown /s /f /t 0” /sc minute /mo 5 /s friend

The command above replaces ALL the commands I was to enter using AT.