Output from jobs

I tripped over a little problem the other day that’s worth reporting.  I was running something like this:


$sb = {
$procs = get-service
$procs | Export-Csv test.csv -NoTypeInformation

Start-Job -ScriptBlock $sb -Name test


I was collecting some data and outputting a CSV.  My problem was more complex but this stands as a good example


I didn’t get the data I wanted


Thinking about it I put the full path to where I wanted the CSV


$sb = {
$procs = get-service
$procs | Export-Csv C:\MyData\scripts\Test\test.csv -NoTypeInformation

Start-Job -ScriptBlock $sb -Name test


And it works.


So where did my data go in the original version?


I ran this


$sb = {

$procs = get-service
$procs | Export-Csv test.csv -NoTypeInformation

Start-Job -ScriptBlock $sb -Name test


And then pulled the data from the job


£> Receive-Job -Id 10



Obvious really – a job runs in a new powershell process that doesn’t run your profile so it starts in the default location  – which is your home directory. And sure enough the CSV file is there


£> ls C:\Users\Richard\Documents\*.csv

    Directory: C:\Users\Richard\Documents

Mode                LastWriteTime     Length Name
—-                ————-     —— —-
-a—        14/09/2014     11:50      46042 test.csv


I can’t remember how many times I’ve told people that PowerShell jobs run in a separate process so I should have realised.  Excellent example of the more you know the more you need to learn

This entry was posted in Powershell Basics. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s