2017-05-28 76 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