I need help with my code, I'm new to PowerShell, so I'm stuck.
I try to get values from csv file and then i make Split() for storing them in to arrays.
After thаt im creating pathVariables object where i need to use all of my values.
$InputCSVfile = Read-Host -Prompt "Enter the full path of CSV file"
$Document = Import-Csv $InputCSVfile -Delimiter ";" | ForEach-Object {
# Here i get strings from CSV file and convert it to array
$FieldDataTypeArray = $_.FieldDataType.Split(",") #Output to console is Text Text
$IsPrimaryKey = $_.IsPrimaryKey.Split(",") #Output to console is TRUE FALSE
$IsRequired = $_.IsRequired.Split(",") #Output to console is TRUE FALSE
$FieldName = $_.FieldName.Split(",") #Output to console is Name LastName
$pathVariables = @{
Description = $_.CodeListDescription
Key = $_.CodeListKey
MetadataDefinition = @(
#I NEED HELP WITH THIS PART OF THE SCIRPT.
#I try to do some looping with for loop and foreach but i can't find good solution.
# Is there any options to have only one object for for all informations ?
# Is there a way for some kind of loop to change the object to be something
like this object:
EXAMPLE OBJECT START @{
DataType = $FieldDataTypeArray[index]
IsPrimaryKey = $IsPrimaryKey[index]
IsRequired = $IsRequired[index]
Name = $FieldName[index]
EXAMPLE OBJECT END }
@{
DataType = $FieldDataTypeArray[0]
IsPrimaryKey = $IsPrimaryKey[0]
IsRequired = $IsRequired[0]
Name = $FieldName[0]
},
@{
DataType = $FieldDataTypeArray[1]
IsPrimaryKey = $IsPrimaryKey[1]
IsRequired = $IsRequired[1]
Name = $FieldName[1]
}
)
Name = $_.CodeListName
StorageType = $_.CodeListStorageType
} | ConvertTo-Json
Invoke-RestMethod -Method POST -Uri https://$Server/api/{....} -Body ([System.Text.Encoding]::UTF8.GetBytes($pathVariables)) -ContentType 'application/json' -WebSession $session
question from:
https://stackoverflow.com/questions/65847242/powershell-arrays-and-object 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…