views:

54

answers:

2

Right now, I'm trying to use a Powershell script to read through a textfile and execute all of the Powershell scripts mentioned in it, but I can't get any results. I've tried specifying it this way:

Start-Job -ScriptBlock{powershell.exe -noexit $val} -name $jobnum and this way: Start-Job -ScriptBlock{$val}

($val is the value of the line of text) but it doesn't run the script that's written on that line.

And I've tried this way: Start-Job -FilePath($val) -name $jobnum

But I get this error: Start-Job : Only PowerShell script files are allowed for FilePath parameter. Specify a file with .ps1 extension.

Even though the value of $val is a legitimate path to a file with ps1 extension!

My text lines look like this: C:\Users\me\Desktop\notepad.ps1

How do I get my Powershell script to read in text and run the ps1 scripts that have a path given in the text?

A: 

Never mind, the problem was because I was doing -FilePath($val) instead of -FilePath $val.

John
+1  A: 

Concerning

Start-Job -ScriptBlock {powershell.exe -noexit $val} -name $jobnum

you need to specify -ArgumentList parameter like this:

Start-Job -ScriptBlock {param($v) .. your command using $v} -name $jobnum -argumentlist $val

Otherwise the $value is not known to the job.

stej
Thanks! I came back to the program this morning, needing to change it so it could run any type of script, and your answer was a huge time saver.
John
Another question: why doesn't this work for lines of text that work with variables, like this value of $val:$env:the = 50shouldn't it create the environmental variable the and set it equal to 50?
John
If you set `$env:the=50`, it correctly sets the variable in your powershell session. However, `Start-Job` creates a *new process* with its own set of variables. Check this using e.g. ProcessExplorer (from former SysInternals).
stej