簡體   English   中英

ForEach 嵌套循環 PowerShell

[英]ForEach Nested Loop in PowerShell

我有這段代碼,我在其中使用 for each 循環從adventureworks.bim文件中提取表名。 但是,我在這里遺漏了一些東西,因為我需要每個 object 的表名,而不是循環中的最終表。 詳情如下

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

結果我得到

"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"

我需要的結果

"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"

試試這個尺寸:

$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

更正

  • 無需使用Out-String
  • ColumnProperty 需要一個+=因為=正在覆蓋每個條目
  • 只是圍繞foreach循環的一些混淆。

您需要先遍歷.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 看起來像這樣:

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