PowerShell函数返回两个True

时间:2016-07-08 12:50:26

标签: powershell

所以在我的功能结束时我做了:

void DataSetsToExcel(List<DataSet> dataSets, string fileName)
    {
        try
        {
            Excel.Application xlApp = new Excel.Application();
            Excel.Workbook xlWorkbook = xlApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
            Excel.Sheets xlSheets = null;
            Excel.Worksheet xlWorksheet = null;

            int i = 0;

            foreach (DataSet dataSet in dataSets)
            {
                System.Data.DataTable dataTable = dataSet.Tables[0];
                int rowNo = dataTable.Rows.Count;
                int columnNo = dataTable.Columns.Count;
                int colIndex = 0;

                //Create Excel Sheets
                xlSheets = xlWorkbook.Sheets;
                xlWorksheet = (Excel.Worksheet)xlSheets.Add(xlSheets[1],
                               Type.Missing, Type.Missing, Type.Missing);
                xlWorksheet.Name = dataSet.Tables[i].ToString();

                //Generate Field Names
                foreach (DataColumn dataColumn in dataTable.Columns)
                {
                    colIndex++;
                    xlApp.Cells[1, colIndex] = dataColumn.ColumnName;
                }

                object[,] objData = new object[rowNo, columnNo];

                //Convert DataSet to Cell Data
                for (int row = 0; row < rowNo; row++)
                {
                    for (int col = 0; col < columnNo; col++)
                    {
                        objData[row, col] = dataTable.Rows[row][col];
                    }
                }

                //Add the Data
                Excel.Range range = xlWorksheet.Range[xlApp.Cells[2, 1], xlApp.Cells[rowNo + 1, columnNo]];
                range.Value2 = objData;

                //Format Data Type of Columns 
                colIndex = 0;
                foreach (DataColumn dataColumn in dataTable.Columns)
                {
                    colIndex++;
                    string format = "@";
                    switch (dataColumn.DataType.Name)
                    {
                        case "Boolean":
                            break;
                        case "Byte":
                            break;
                        case "Char":
                            break;
                        case "DateTime":
                            format = "dd/mm/yyyy";
                            break;
                        case "Decimal":
                            format = "$* #,##0.00;[Red]-$* #,##0.00";
                            break;
                        case "Double":
                            break;
                        case "Int16":
                            format = "0";
                            break;
                        case "Int32":
                            format = "0";
                            break;
                        case "Int64":
                            format = "0";
                            break;
                        case "SByte":
                            break;
                        case "Single":
                            break;
                        case "TimeSpan":
                            break;
                        case "UInt16":
                            break;
                        case "UInt32":
                            break;
                        case "UInt64":
                            break;
                        default: //String
                            break;
                    }
                    //Format the Column accodring to Data Type
                    xlWorksheet.Range[xlApp.Cells[2, colIndex],
                          xlApp.Cells[rowNo + 1, colIndex]].NumberFormat = format;
                }

                i++;
            }

            //Remove the Default Worksheet
            ((Excel.Worksheet)xlApp.ActiveWorkbook.Sheets[xlApp.ActiveWorkbook.Sheets.Count]).Delete();

            //Save
            xlWorkbook.SaveAs(fileName,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                Excel.XlSaveAsAccessMode.xlNoChange,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value,
                System.Reflection.Missing.Value);

            xlWorkbook.Close();
            xlApp.Quit();
            GC.Collect();
        }
        catch (Exception ex)
        {

            throw;
        }
    }

在此之前只有作业。

结果变量:

    $assemblyVersionLine -match "(\d+\.)(\d+\.)(\d+\.)(\*|\d+)"

    $assemblyVersion = $matches[0]

    $true 
    $assemblyVersion
}

有这些值:

$result

为什么?

我只期待

True
True
1.0.0.0

1 个答案:

答案 0 :(得分:3)

PowerShell会自动输出遇到的任何未明确抑制的值。此外,此行中使用的-match运算符:

$assemblyVersionLine -match "(\d+\.)(\d+\.)(\d+\.)(\*|\d+)"

返回一个未被抑制的布尔值(True),因此PowerShell会输出它。另一个True本身就来自$true

您可以通过将结果重定向到True来取消第一个$null

$assemblyVersionLine -match "(\d+\.)(\d+\.)(\d+\.)(\*|\d+)" > $null

现在,您只需从True行输出一个$true