- 質問: この Powershell コードには、「値 1 と値 2」という手動で定義された値があり、これら 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
}