Invoke-Expression无效

时间:2017-05-28 13:52:00

标签: powershell-v2.0 invoke-command

任何人都可以帮我解释为什么我的工件没有传递给我的脚本文件吗?

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

2 个答案:

答案 0 :(得分:1)

[string]::Format(...)调用将您的哈希表数组变为数组的字符串表示形式。哪个是字符串System.Object[]。如果输出变量$temp,您将看到正在获取

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

缺少脚本路径,因为您将其分配给变量$FilePath,但在$GenerateArtifcateReportScript中使用变量[string]::Format()

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

& $FilePath $outputPath $buildNumber $versionNumber $artifacts

答案 1 :(得分:0)

由于您正在调用带有字符串的Invoke-Expression,因此它无法处理传递复杂对象。

您可以尝试将其作为函数调用。找到你的脚本(并在其中命名函数。

喜欢这个

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