[英]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
+=
因为=
正在覆盖每个条目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.