I am trying to write a PS script with an array for multiple items using ConvertTo-Json. I need this script to use in Azure DevOps pipeline. For example, using variables I could convert multiple items into Json. The script works for a single item as I want, but cannot figure out how to feed multiple items as one variable.
# Variables
$Roles = "Reader"
$Name = "User1"
$Email = "[email protected]"
$UserList = ConvertTo-Json @(@{Roles = @("$Roles"); Name = "$Name"; Email = "$Email"})
# Output for a single item as I need
PS C:\> $UserList
[
{
"Email": "[email protected]",
"Name": "User1",
"Roles": [
"Reader"
]
}
]
What I need to achieve is to add more items into every single variable, so an output would be as below with two users
[
{
"Email": "[email protected]",
"Name": "User1",
"Roles": [
"Reader"
]
},
{
"Email": "[email protected]",
"Name": "User2",
"Roles": [
"Reader"
]
}
]
I tried to use variables using an array as @("Reader"; "Reader"), but the output is not what I need. Can someone please help to achieve it? Thanks!
ConvertTo-Jsonto get that desired output.