2017-05-28 7 views
0

Kann mir jemand helfen, warum meine Artefakte nicht an meine Skriptdatei übergeben werden?Invoke-Expression funktioniert nicht

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 

Antwort

1

Der [string]::Format(...) Anruf mangles Array von Hash-Tabellen in die String-Darstellung des Arrays. Welches ist die Zeichenfolge System.Object[]. Wenn Sie die Variable Ausgabe $temp Sie werden sehen, dass Sie

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

Das Skript Pfad fehlt, wenn es darum, weil man es auf eine Variable $FilePath, weisen aber eine variable $GenerateArtifcateReportScript in [string]::Format() verwenden.

Außerdem möchten Sie sowieso Invoke-Expression nicht verwenden. Es ist fast immer das falsche Werkzeug für den Job. Verwenden Sie die call operator (&) statt:

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

Da Sie Invoke-Expression sind aufrufen, die einen String, nicht damit umgehen können komplexe Objekte übergeben.

Sie können versuchen, es als Funktion aufzurufen und. Quelle Ihr Skript (und die Funktion in es nennen.

Like this

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