Okay i need to loop through an array using the deviceId. Everything inserts fine, the problem is that the obj tags is not pulled from the API, because is an array and is tied to the device id. So i decided to create another table while the script runs it can pull the device ID's and insert them so the tags can be pulled from the API and inserted into the other table where i can later join them. The problem is i did another for each and is just inserting the last ID and no tags are even been pulled. Any suggestions?
#For loop giving variables to the objects
foreach($obj in $Json.devices)
{
$DeviceIdentifier = $obj.deviceid
$DeviceNombre = $obj.deviceName
$DomainNombre = $obj.domainName
$Description = $obj.description
$Tags = $obj.tags
$location = $obj.location
$Os = $obj.os
Write-Host ($obj.devicename) -BackgroundColor White -ForegroundColor red
Write-Host ($obj.domainname) -BackgroundColor White -ForegroundColor red
Write-Host ($obj.deviceid) -BackgroundColor White -ForegroundColor DarkGreen
Write-Host ($obj.tags) -BackgroundColor White -ForegroundColor black
Write-Host ($obj.description) -BackgroundColor White -ForegroundColor blue
Write-Host ($obj.os) -BackgroundColor White -ForegroundColor DarkBlue
#Inserting into MYSQL database
$cmd = $connection.CreateCommand()
$insert_stmt = "INSERT INTO [dbo].[Tags]([DeviceID],[Device Name],[Domain Name],[Description],[Tags],[Location],[Os])
VALUES ('$DeviceIdentifier','$DeviceNombre', '$DomainNombre','$Description','$tags','$location','$Os')" -replace "\s+"," "
$cmd.CommandText = $insert_stmt
write-host $insert_stmt -BackgroundColor White -ForegroundColor DarkBlue
$cmd.ExecuteNonQuery()
}
foreach($Tags in $DeviceIdentifier)
{
$DeviceIdentifier = $Tags.deviceid
$Tags = $obj.tags
Write-Host ($obj.deviceid) -BackgroundColor White -ForegroundColor DarkGreen
Write-Host ($obj.tags) -BackgroundColor White -ForegroundColor black
#Inserting into MYSQL database
$cmd = $connection.CreateCommand()
$insert_stmt = "INSERT INTO [dbo].[TagsTable]([DeviceID],[Tags])
VALUES ('$DeviceIdentifier','$tags')" -replace "\s+"," "
$cmd.CommandText = $insert_stmt
write-host $insert_stmt -BackgroundColor White -ForegroundColor DarkBlue
$cmd.ExecuteNonQuery()
}
$Connection.Close()