<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Personal Weblog of John Wood &#187; asynchronous</title>
	<atom:link href="http://www.webofwood.com/tag/asynchronous/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webofwood.com</link>
	<description>Wordpress weblog of John Wood, a Database Administrator</description>
	<lastBuildDate>Fri, 16 Sep 2011 17:18:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Asynchronous Processing using Powershell Jobs</title>
		<link>http://www.webofwood.com/2010/03/17/asynchronous-powershell-jobs/</link>
		<comments>http://www.webofwood.com/2010/03/17/asynchronous-powershell-jobs/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 14:48:57 +0000</pubDate>
		<dc:creator>John Wood</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[asynchronous]]></category>
		<category><![CDATA[job]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://www.webofwood.com/?p=116</guid>
		<description><![CDATA[Powershell is a fantastic tool to use for management of multiple computers. I have been slowly converting many of our administrative functions from a hodge-podge mixture of CMD, BAT, VPS, Python, and Perl scripts. One daily administrative talks is copying a variety of backups from a variety of Windows servers to our data ’warehouse’ where [...]]]></description>
			<content:encoded><![CDATA[<p>Powershell is a fantastic tool to use for management of multiple computers. I have been slowly converting many of our administrative functions from a hodge-podge mixture of CMD, BAT, VPS, Python, and Perl scripts. One daily administrative talks is copying a variety of backups from a variety of Windows servers to our data ’warehouse’ where they are then copied to tape.</p>
<p>I recently found the “Jobs” cmdlets in Powershell V2.0. With “Jobs” you can asynchronously process multiple tasks (e.g. copying backups from many remote machines to data storage on the current local machine). In my case, running the copies synchronously results in the process spanning over to the next morning. Backups are stored daily. If a backup is taken on Wednesday, I can’t have it being stored in our warehouse under Thursday.</p>
<p><span id="more-116"></span></p>
<p>Using Powershell “Jobs”, I can accommodate copying all the backups within the daily timeframe. However, I did wrestle with using “Jobs” at first. Mainly because I couldn’t see what happened with a JOB on my console! The trick is to make sure you issue a Receive-Job cmdlet after a JOB has finished.</p>
<h3>Here a a couple of examples to try:</h3>
<p><span style="font-size: xx-small;">These example assume you are running Powershell V2.0 and you have enabled remoting on the machine your running this. Remoting does not have to be enabled on the remote servers but must be enabled on your local machine.</span></p>
<h5>Begin with a simple copy you know works.</h5>
<pre class="brush: ps"># Copies a directory and all files and sub-directories, I would
# suggest using something small to quickly see results
$source = "\\RemoteServer\d$\Backups # your remote path
$target = "W:\WoodyzTest"            # your local path
Copy-Item -Path $source -Destination $target -Recurse Verbose</pre>
<h5>Next, we’ll take the Copy-Item and put it in a Start-Job</h5>
<pre class="brush: ps">$source = "\\RemoteServer\d$\Backups # your remote path
$target = "W:\WoodyzTest"            # your local path
$job Start-Job -Scriptblock {
Copy-Item -Path $source -Destination $target -Recurse -Verbose
}
Wait-Job $job  # For this test, we’ll wait for the job to complete</pre>
<p>Run the above script, look at what is returned in the console, and then check to see of the copy worked.</p>
<p>Surprise! The directory was not copied! What happened? I have no idea because I did not see anything in the console. There were no error messages!</p>
<h5>OK, let’s now add a Receive-Job</h5>
<pre class="brush: ps">$source = "\\RemoteServer\d$\Backups # your remote path
$target = "W:\WoodyzTest"            # your local path
$job Start-Job -Scriptblock {Copy-Item -Path $source -Destination $target -Recurse -Verbose }
Wait-Job $job  # For this test, we’ll wait for the job to complete
Receive-Job $job  # will now display any results from the job</pre>
<p>Now what do you see on the console? We have yet another surprise! The console shows we have an error in our Copy-Item. But why? It worked before when we ran it outside of a JOB.</p>
<p>I took a SWAG at it and decided to try enclosing the entire original script inside the JOB Scriptblock:</p>
<h5>Here is the entire, final, working script:</h5>
<pre class="brush: ps">$job = Start-Job -ScriptBlock {
    $source = "\\Lcfltr01\d$\SQL Backups\lcfltr01"
    $target = "W:\WoodyzTest"
    Copy-Item -Path $source -Destination $target -Recurse -Verbose
}
Wait-Job $job
Receive-Job $job</pre>
<p>I have not read up on it yet, but deductive reasoning implies: because the JOB creates a new background process, the local variables are not available to the newly created thread. Makes sense to me.</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.webofwood.com/2010/03/17/asynchronous-powershell-jobs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

