1

I have some pretty simply code which aims to convert each file in a directory into HTML. My problem is that although a job is successfully created for each file, the scriptblock does not run, ever.

$convert = {

Param(
    [parameter(ValueFromPipeline=$true)]
    $file
)

$content = Get-Content -Path $file.FullName
$outDir= New-Item -Type dir -Path $file.FullName + "\HTMLFiles"
$outFile = $outDir + $file.Name +  ".html"

foreach($line in $content) {
     #move the content into a variable and add some html tags
     $html = $html + '<tr>' + $line + '</tr>' +'<br>'
}
#convert the variable to .html and save the result as a file
ConvertTo-Html -Head $style -Body $html | Out-File -FilePath $outFile -Encoding "ASCII"
#empty the variable
$html = " "
}

Function main
{
Param(
   [parameter(Position=0, Mandatory=$true, ValueFromPipeLine=$true)]
   $target = $args[0]
)
#stores some html styling code
$path = $pwd.Path + "\style.txt"
$style = Get-Content -Path $path
#collect all files in the dirctory
$files = Get-ChildItem -Path $target -Recurse

foreach($file in $files) {
#for each file in the collection start a job which runs the given scriptblock (scriptblock is not working)
Start-Job -Name $file.name -ScriptBlock $convert -ArgumentList $file
}
#clean-up
Write-Host "Finished jobs"
Wait-Job *
Remove-Job -State Completed
}

main($args[0])

I'm fairly new to powershell and have played around with ways to solve this, but can't seem to figure it out.

user3445141
  • 11
  • 1
  • 2

1 Answers1

-1
  • Change: I removed the parameters from the script block and the function.
  • Why: because the argument is passed in by Start-Job, and also for the function via the syntax function name (argument1, argument2) {}.

I also took away from the brackets from the function call, because in Powershell you call functions like: function "argument1" "argument2"


$convert = {    
    $content = Get-Content -Path $file.FullName
    $outDir= New-Item -Type dir -Path $file.FullName + "\HTMLFiles"
    $outFile = $outDir + $file.Name +  ".html"

    foreach($line in $content) {
        #move the content into a variable and add some html tags
        $html = $html + '<tr>' + $line + '</tr>' +'<br>'
    }
    #convert the variable to .html and save the result as a file
    ConvertTo-Html -Head $style -Body $html | Out-File -FilePath $outFile -Encoding "ASCII"
    #empty the variable
    $html = " "
}

Function main ($args)
{
    $target = $args
    #stores some html styling code
    $path = $pwd.Path + "\style.txt"
    $style = Get-Content -Path $path
    #collect all files in the dirctory
    $files = Get-ChildItem -Path $target -Recurse

    foreach($file in $files) {
        #for each file in the collection start a job which runs the given scriptblock (scriptblock is not working)
        Start-Job -Name $file.name -ScriptBlock $convert -ArgumentList $file
    }
    #clean-up
    Write-Output "Finished jobs"
    Wait-Job *
    Remove-Job -State Completed
}

main $args[0]
Vasili Syrakis
  • 9,321
  • 1
  • 39
  • 56
  • The parameter in the script block is required. Try running `$foo = 'foo'; Start-Job -ScriptBlock { "_${foo}_" } -ArgumentList $foo | Wait-Job | Receive-Job` and take a look at the output. – Ansgar Wiechers Mar 24 '14 at 17:17