Home > Net >  To get Values from CSV instead of manual values
To get Values from CSV instead of manual values

Time:06-03

Question: This Powershell code have manual values defined "Value 1 & value 2" , want these two to be from CSV file.

#Value 1
        $blockedConnector1 = [pscustomobject]@{
        id = "/providers/Microsoft.PowerApps/apis/shared_salesforce"
        name = "Salesforce"
        type = "Microsoft.PowerApps/apis"
    }

#value 2
            $blockedConnector2 = [pscustomobject]@{
        id = "/providers/Microsoft.PowerApps/apis/shared_postgresql"
        name = "PostgreSQL"
        type = "Microsoft.PowerApps/apis"
    }

#Grouping of Connectors
    $blockedConnectors = @()
    $blockedConnectors  = $blockedConnector1
    $blockedConnectors  = $blockedConnector2
    $blockedConnectorGroup = [pscustomobject]@{
        classification = "Blocked"
        connectors = $blockedConnectors
    }

    $blockedConnectorGroup | Format-List 

Desired Output

classification : Blocked
connectors     : {@{id=/providers/Microsoft.PowerApps/apis/shared_salesforce; name=Salesforce; type=Microsoft.PowerApps/apis}, @{id=/providers/Microsoft.PowerApps/apis/shared_postgresql; name=PostgreSQL;type=Microsoft.PowerApps/apis}}

CodePudding user response:

If I understand correctly, you have a CSV file like this:

"id","name","type"
"/providers/Microsoft.PowerApps/apis/shared_salesforce","Salesforce","Microsoft.PowerApps/apis"
"/providers/Microsoft.PowerApps/apis/shared_postgresql","PostgreSQL","Microsoft.PowerApps/apis"

so all you have to do is to Import that data and use it in your $blockedConnectorGroup

$blockedConnectorGroup = [pscustomobject]@{
    classification = "Blocked"
    connectors     = (Import-Csv -Path 'D:\Test\blockedConnectors.csv')
}

$blockedConnectorGroup | Format-List

If you do not yet have such a CSV file, you can create it by exporting the PsCustomObjects you defined

[pscustomobject]@{
    id   = "/providers/Microsoft.PowerApps/apis/shared_salesforce"
    name = "Salesforce"
    type = "Microsoft.PowerApps/apis"
},
 [pscustomobject]@{
    id   = "/providers/Microsoft.PowerApps/apis/shared_postgresql"
    name = "PostgreSQL"
    type = "Microsoft.PowerApps/apis"
} | Export-Csv -Path 'D:\Test\blockedConnectors.csv' -NoTypeInformation
  • Related