返回数组而不是哈希表

时间:2018-04-19 08:31:42

标签: powershell powershell-v5.0

我有以下powershell脚本:

$reportsFolder = $PSScriptRoot;
$reportServerDbInstance = "localhost";
$dbName = "dbname";

function Execute-Query{
    Param(
        [parameter(position=0)]
        $query,
        [parameter(position=1)]
        $execDb 
        )
    Invoke-Sqlcmd -Query $query -ServerInstance $reportServerDbInstance -Database $execDb -ErrorAction 'Stop'
}

function Test-ReportQuery($query, $execDb) {

    Execute-Query -query $query -execDb $execDb

}


function Test-ReportQueries($reportName, $queries) {
    foreach($q in $queries.GetEnumerator()) {
        try {
            Test-ReportQuery -query $q.Value -execDb $dbName
        }
        catch {
            $errorMsg = "Dataset `"$($q.Name)`" for report `"$reportName`" failed. Check the query";
            Write-Error $errorMsg
            Write-Error $q.Value
            Write-Error $_;
        }
    }
}


function Prepare-QueriesFromReportForTest ($folder, $name) 
{     
    [OutputType([System.Collections.Hashtable])]

    $tmpReportString = gc $(Join-Path $folder "$name.rdl");

    [xml]$report = $tmpReportString;

    foreach($ds in $report.Report.DataSources.DataSource) {
        if ($ds.DataSourceReference -ne "rdb_custom") {
            Write-Host "Report $name skipped as datasource is not rdb_custom";
            return;
        }
    }

    $paramsFromReport = $report.Report.ReportParameters;

    $params = @{}
    $tmpParams = @{}

    foreach ($p in $paramsFromReport.ReportParameter ) {
        $value = "''";
        if ($p.DataType -eq "DateTime" )  {
            $value = "GETDATE()";
        }
        if ($p.DataType -eq "Boolean") {
            continue;
        }
        if ($p.DataType -eq "Float" )  {
            $value = "0";
        }
        if ($p.DataType -eq "Integer" )  {
            $value = "0";
        }
        9009
        $params.add($p.Name, $value);
        $tmpParams.add($p.Name, $value);
    }

    $testQueries = $null;
    $testQueries = @{};

    foreach($q in  $report.Report.DataSets.DataSet) {

        if ($q.Query -eq $null) {
            continue;
        }

        #getting the dataset parameters that <> to report parameters
        foreach($p in $q.Query.QueryParameters.QueryParameter) {
            foreach($p1 in $tmpParams.GetEnumerator()) {
                if (($p.Value -eq "=Parameters!$($p1.Name).Value".Replace("@","")) -and ($p.Name -ne "@$($p1.Name)") ) {
                    $params.add($p.Name.Replace("@",""), $p1.Value);
                }

            }
        }

        $query = $q.Query.CommandText.Trim().Replace(";","").ToLower();

        foreach ($param in $params.GetEnumerator()) {
            $query = $query.Replace("@$($param.Name.ToLower())", $param.Value );
        }

        if ($query.Substring(0,6).ToLower() -eq "select") {
            $testQueries.add($q.Name, "SELECT * FROM (SELECT TOP(0) " + $query.Substring(6, $query.Length -6) + ") a");
        }
    }
    $testQueries
    #return $testQueries;
}


Get-ChildItem $reportsFolder -Filter Water_Queue_Detail_Report.rdl | 
Foreach-Object {
    $fileName = $_.BaseName;

    Write-Host "Testing $fileName report";

    $queriesToTest = Prepare-QueriesFromReportForTest -folder $reportsFolder -name $fileName

    if ($queriesToTest -ne $null) {
        Test-ReportQueries -reportName $fileName -queries $queriesToTest;
    }

}

当我在完成它之前调试Prepare-QueriesFromReportForTest函数时,它在哈希表中具有正确的值:

enter image description here

然而变量$queriesToTest得到一些奇怪的值(某些整数9009)+期望值:

enter image description here

我检查了两个对象的类型,我看到在函数中对象是哈希,但是函数返回数组。

出了什么问题?

1 个答案:

答案 0 :(得分:1)

Prepare-QueriesFromReportForTest函数在9009循环中只有一行只包含数字foreach。删除:

if ($p.DataType -eq "Integer" )  {
    $value = "0";
}
9009
$params.add($p.Name, $value);
$tmpParams.add($p.Name, $value);