來自 CSV 的值而不是手動值

來自 CSV 的值而不是手動值
  • 問題:此 Powershell 程式碼手動定義了「值 1 和值 2」值,希望這兩個值來自 CSV 檔案。
#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 

期望的輸出。

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

答案1

Import-CSV您可以透過多種方式繼續,以下是一些範例:

$blockedConnectors = Import-CSV 'C:\path\to\file.csv'

# Example 1: import straight into your group
$blockedConnectorGroup = [pscustomobject]@{
  classification = "Blocked"
  connectors     = $blockedConnectors
}

# Example 2: only import specific objects from the csv
$SalesForce = $blockedConnectors | Where-Object Name -EQ 'SalesForce'
$Postgres   = $blockedConnectors | Where-Object Name -EQ 'PostgreSQL'

$blockedConnectorGroup = [pscustomobject]@{
  classification = "Blocked"
  connectors     = $SalesForce,$PostGres
}

相關內容