Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

Powershell Arrays and Object

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...