Блок сценария запуска Powershell не выполняется

У меня есть довольно простой код, который предназначен для преобразования каждого файла в каталоге в HTML. Моя проблема в том, что хотя задание успешно создается для каждого файла, блок сценария никогда не запускается.

$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])

Я новичок в powershell и пробовал решить эту проблему, но не могу понять.


person user3445141    schedule 24.03.2014    source источник
comment
какую версию powershell вы используете? (вот как его получить stackoverflow.com/questions/1825585/ )   -  person ДМИТРИЙ МАЛИКОВ    schedule 24.03.2014
comment
Где ваши звонки о приеме на работу?   -  person David Brabant    schedule 24.03.2014
comment
Ему не нужно получать задания, они делают все, что им нужно, и все.   -  person Vasili Syrakis    schedule 24.03.2014
comment
@VasiliSyrakis Получение вывода, сгенерированного в заданиях, может дать некоторые указания на то, почему они не работают должным образом.   -  person Ansgar Wiechers    schedule 24.03.2014


Ответы (1)


  • Изменение: я удалил параметры из блока скрипта и функции.
  • Почему: потому что аргумент передается Start-Job, а также для функции через синтаксис function name (argument1, argument2) {}.

Я также убрал за скобки вызов функции, потому что в Powershell вы вызываете такие функции, как: 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]
person Vasili Syrakis    schedule 24.03.2014
comment
Параметр в блоке скрипта является обязательным. Попробуйте запустить $foo = 'foo'; Start-Job -ScriptBlock { "_${foo}_" } -ArgumentList $foo | Wait-Job | Receive-Job и посмотрите на результат. - person Ansgar Wiechers; 24.03.2014