- Вопрос: В этом коде 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
}