简体   繁体   English

ForEach 嵌套循环 PowerShell

[英]ForEach Nested Loop in PowerShell

I have this piece of code where I am extracting table names from the adventureworks.bim file using a for each loop.我有这段代码,我在其中使用 for each 循环从adventureworks.bim文件中提取表名。 However, I am missing something here because I required a Table name per object and not the final table in the loop.但是,我在这里遗漏了一些东西,因为我需要每个 object 的表名,而不是循环中的最终表。 The details are as below详情如下

cls
$BIM = "C:\Users\Desktop\adventureworks.bim"
$origmodel = (Get-Content $BIM -Raw) | Out-String | ConvertFrom-Json
   ForEach($table in $origmodel.Model.tables.name)
   {
     $ColumnProperty = $origmodel.Model.tables.columns | ForEach-Object {
                                                            [pscustomobject] @{
                                                              'Table Name' = $table
                                                              'Object Name' = $_.name
                                                              'DataType' = $_.dataType
                                                            }
                                                        }
                 }
 $ColumnProperty | ConvertTo-Csv -NoTypeInformation

Result I Get结果我得到

"Table Name", "Object Name", "Datatype"
"Date","RowNumber-2662979B-1795-4F74-8F37-6A1BA8059B61","int64"
"Date","CurrencyKey","int64"
"Date","Currency Code","string"
"Date","CurrencyName","string"
"Date","RowNumber-2662979B-1795-4F74-8F37-6A1BA8059B61","int64"
"Date","CustomerKey","int64"
"Date","GeographyKey","int64"
"Date","Customer Id","string"
"Date","Title","string"
"Date","First Name","string"
"Date","Middle Name","string"
"Date","Last Name","string"
"Date","Name Style","boolean",
"Date","Birth Date","dateTime",
"Date","Marital Status","string"
"Date","Suffix","string"
"Date","Gender","string"
"Date","RowNumber-2662979B-1795-4F74-8F37-6A1BA8059B61","int64"
"Date","DateKey","int64"
"Date","Date","dateTime",
"Date","Day Number Of Week","int64"
"Date","Day Name Of Week","string"
"Date","Day Of Year","int64"
"Date","Week Of Year","int64"
"Date","Month Name","string"

Result I Need我需要的结果

"Table Name", "Object Name", "DataType"
"Currency","RowNumber-2662979B-1795-4F74-8F37-6A1BA8059B61","int64"
"Currency","CurrencyKey","int64"
"Currency","Currency Code","string"
"Currency","CurrencyName","string"
"Customer","RowNumber-2662979B-1795-4F74-8F37-6A1BA8059B61","int64"
"Customer","CustomerKey","int64"
"Customer","GeographyKey","int64"
"Customer","Customer Id","string"
"Customer","Title","string"
"Customer","First Name","string"
"Customer","Middle Name","string"
"Customer","Last Name","string"
"Customer","Name Style","boolean",
"Customer","Birth Date","dateTime",
"Customer","Marital Status","string"
"Customer","Suffix","string"
"Customer","Gender","string"
"Date","RowNumber-2662979B-1795-4F74-8F37-6A1BA8059B61","int64"
"Date","DateKey","int64"
"Date","Date","dateTime",
"Date","Day Number Of Week","int64"
"Date","Day Name Of Week","string"
"Date","Day Of Year","int64"
"Date","Week Of Year","int64"
"Date","Month Name","string"

Try this on for size:试试这个尺寸:

$BIM = "C:\Users\Desktop\adventureworks.bim"
$origmodel = Get-Content $BIM -Raw | ConvertFrom-Json

ForEach ($table in $origmodel.Model.tables) {
  $ColumnProperty += $table.columns | ForEach-Object {
    [pscustomobject] @{
      'Table Name'  = $table.name
      'Object Name' = $_.name
      'DataType'    = $_.dataType
    }
  }
}
$ColumnProperty | ConvertTo-Csv -NoTypeInformation

Corrections更正

  • No need to use Out-String无需使用Out-String
  • ColumnProperty needed a += as = was overwriting every entry ColumnProperty 需要一个+=因为=正在覆盖每个条目
  • Just a few mix-ups around the foreach loops.只是围绕foreach循环的一些混淆。

You need to loop over .model.tables first then an inner loop for each columns :您需要先遍历.model.tables ,然后对每一columns进行内部循环:

$req = Invoke-RestMethod https://raw.githubusercontent.com/TabularEditor/TabularEditor/master/TabularEditorTest/AdventureWorks.bim
$req.model.tables | ForEach-Object {
    foreach($column in $_.columns) {
        [pscustomobject]@{
            Table      = $_.name
            ObjectName = $column.name
            DataType   = $column.dataType
        }
    }
} | Export-Csv path\to\export.csv -NoTypeInformation

Output to the console would look like this for the first few objects:对于前几个对象,控制台的 Output 看起来像这样:

Table               ObjectName                                     DataType
-----               ----------                                     --------
Currency            RowNumber-2662979B-1795-4F74-8F37-6A1BA8059B61 int64
Currency            CurrencyKey                                    int64
Currency            Currency Code                                  string
Currency            CurrencyName                                   string
Customer            RowNumber-2662979B-1795-4F74-8F37-6A1BA8059B61 int64
Customer            CustomerKey                                    int64
Customer            GeographyKey                                   int64
Customer            Customer Id                                    string
Customer            Title                                          string
...
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM