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

相关内容