Feeds:
Posts
Comments

Archive for April, 2010

Blog is moving! You should check this post out on kevinpelgrims.com

This is part two of four in a series of articles on the new features in PowerShell 2.0. Last time we discussed background jobs, now it’s time for transactions!

Transactions

When you run scripts, generally, three things can happen:

  • The script runs, my favorite
  • The script fails, this is not cool, but you can probably fix it
  • The script runs only partially and dies unexpectedly in the middle of executing stuff. This is very bad and this is probably not what you want.

PowerShell 2.0 has the solution: transactions. They can help you to solve these issues and make sure the script just succeeds, or fails, but nothing inbetween.

Starting a transaction is pretty easy with the Start-Transaction command, but it doesn’t magically fix everything. You still have to use the -UseTransaction switch on your regular commands. Commands that use this switch will run inside the scope of the transaction, those who don’t will run on the outside. To commit the results of the transaction, use Complete-Transaction. If there is something wrong and you need to undo the changes, call Undo-Transaction. A little transactionsdemo in the new Microsoft PowerShell ISE:

This small script creates a transactionscope and creates a string object. Then we append some text to it twice. Once out of the transactionscope, the second time inside of the transactionscope. When we do a ToString(), we see that the text that is appended inside of the transaction is not visible at this point. This is kind of normal, because the transaction is not yet completed. Then we complete the transaction and do a ToString() again, and we can see the second text too.

Again, use the power of the shell for more info: Get-Help about_Transaction

Recources

A list of resources on the discussed topics:
I already told you about a few cool new features in PowerShell 2.0, now it’s time to play with them. I’ve divided one large article into four small ones, to make it a bit more readable. We’ll start with background jobstransactions and modules and end with remoting. These will become very useful for the PowerShell master.
Advertisements

Read Full Post »

Blog has moved! You should check this post out on kevinpelgrims.com

I already told you about a few cool new features in PowerShell 2.0, now it’s time to play with them. I’ve divided one large article into four small ones, to make it a bit more readable. We’ll start with background jobstransactions and modules and end with remoting. These will become very useful for the PowerShell master.

Background jobs

What do these things do again? Background jobs are commands that run asynchronously. When you run a background job, the command prompt returns immediately, even if the command is still running.
Background jobs can be really useful to run complex commands on the background, while continuing some other work. Let’s see how this works and start off with the commands, it’s actually pretty easy.

The basic (and most important) commands are Start-JobGet-Job and Stop-Job. A small demonstration:

As you can see, Start-Job starts a background job (surprise, surprise!) and shows us some info of the newly created job.

In the small script above I created a command that would normally pause the execution of it for 30 seconds. Now, because of the use of a background process we can immediately continue our work, while the first piece of script keeps running in the background.

Get-Job gets a list of running or completed jobs in the current session, as you probably expected. Using this command we also get a lot of useful information, like state and command of the job.

Using the Id we can find with the Get-Job command, we can also stop the execution a running job with Stop-Job.

PS D:\ProjectsPS> Stop-Job 3

If we want to get rid of the job completely, we simply remove it:

PS D:\ProjectsPS> Remove-Job 3

What if you want to run a command that returns data? Use Receive-Job!

$job = Start-Job -scriptblock {Get-Process}
Receive-Job -job $job

The Receive-Job command will output the data from the scriptblock used in Start-Job (in this case, a list of processes on the computer).

These are pretty basic uses of background jobs, but it demonstrates how powerful this can be when creating large scripts that have to execute fast. For more information on this topic, use the PowerShell help, it’s all there! Tip: Get-Help about_Jobs

Do try this at home!

Recources

A list of resources on the discussed topic:

Read Full Post »