2017-05-28 57 views
0

任何人都可以帮助我,为什么我的工件没有传递到我的脚本文件?调用 - 表达式不起作用

caller.ps1

$FilePath = "C:\Users\test\Desktop\hashtest\generate-artifact-report.ps1" 
$outputPath = "C:\Users\test\Desktop\hashtest\test.html"; 
$buildNumber=19; 
$versionNumber ="2.3.7"; 

$artifacts = @() 
$artifacts += @{Name="Test"; ExeLink="https://google.com"; MsiLink="https://google.com";} 
$artifacts += @{Name="Test Stagning"; ExeLink="https://google.com"; MsiLink="https://google.com";} 
$artifacts += @{Name="Test Stagning"; ExeLink="https://google.com"; MsiLink="https://google.com";} 

$temp = [string]::Format("{0} {1} {2} {3} {4}", $GenerateArtifcateReportScript, $outputPath, $buildNumber, $versionNumber, $artifacts) 
Invoke-Expression($temp) 

generate-artifact-report.ps1

[CmdletBinding()] 
Param(
    [Parameter(Mandatory=$True, Position =0)] 
    [string]$outputPath, 

    [Parameter(Mandatory=$True, Position =1)] 
    [string]$buildNumber, 

    [Parameter(Mandatory=$True, Position =2)] 
    [string]$versionNumber, 

    [Parameter(Mandatory=$True, Position =3)] 
    [System.Object[]]$artifacts 
) 

$Style = " 
<style> 
    .brandCol { width:250px; font-weight:bold; padding:10px; } 
    .fileCol { width:250px; text-align:center; } 
</style> 
" 

$BrandTable = " 
<h1>WorkSmart Artifact Download Links</h1> 
<div style='font-size:20px; padding:10px;'> 
    <strong>Build Number :</strong> $buildNumber<br /> 
    <strong>Version :</strong> $versionNumber<br /> 
</div> 
<table> 
    <tbody> 
     <tr> 
      <td class='brandCol'>Brands</td> 
      <td class='brandCol fileCol'>MSI</td> 
      <td class='brandCol fileCol'>EXE (With Prerequisite)</td> 
     </tr>"; 

foreach ($artifact in $artifacts) { 
    $name = $artifact.Name; 
    $exeLink = $artifact.ExeLink; 
    $msiLink = $artifact.MsiLink; 

    $BrandTable = $BrandTable + " 
    <tr> 
     <td class='brandCol'>$name</td> 
     <td class='fileCol'><a href='$msiLink'>Download</a></td> 
     <td class='fileCol'><a href='$exeLink'>Download</a></td> 
    </tr>"; 
} 

$BrandTable = $BrandTable + "</tbody> 
    </table> 
"; 

#Save the HTML Web Page 
ConvertTo-Html -Head $Style -PreContent $BrandTable | Out-File $outputPath 

回答

1

[string]::Format(...)通话轧液的哈希表的阵列到阵列的字符串表示。这是字符串System.Object[]。如果输出变量$temp你会看到你得到

C:\Users\test\Desktop\hashtest\test.html 19 2.3.7 System.Object[] 

脚本路径丢失,因为你把它分配给一个变量$FilePath,但在[string]::Format()使用变量$GenerateArtifcateReportScript

此外,您不想使用Invoke-Expression。这几乎总是工作的错误工具。使用call operator&)代替:

& $FilePath $outputPath $buildNumber $versionNumber $artifacts 
0

既然你调用调用,表达其需要一个字符串,它不能处理经过复杂的对象。

你可以尝试调用它作为一个函数和。源脚本(和其命名函数。

像这样

. generate-artifact-report.ps1 
Generate-ArtifactReport $outputpath $buildnumber $versionnumbee $artifacts # name the function inside the ps1 file