从 CSV 中获取值而不是手动值

To get Values from CSV instead of manual values

问题:此 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 

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

如果我没理解错的话,你有一个像这样的 CSV 文件:

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

所以您所要做的就是导入该数据并在您的 $blockedConnectorGroup

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

$blockedConnectorGroup | Format-List

如果您还没有这样的 CSV 文件,您可以通过导出您定义的 PsCustomObjects 来创建它

[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