Okay, ich muss ein Array mithilfe der deviceId durchlaufen. Alles fügt gut, das Problem ist, dass die Obj-Tags nicht aus der API gezogen wird, weil ein Array ist und an die Geräte-ID gebunden ist. Also habe ich beschlossen, eine andere Tabelle zu erstellen, während das Skript läuft, kann es die Geräte-IDs ziehen und einfügen, so dass die Tags aus der API gezogen und in die andere Tabelle eingefügt werden können, wo ich später beitreten kann. Das Problem ist, dass ich ein anderes für jedes getan habe und nur die letzte Identifikation einfüge und keine Umbauten sind sogar gezogen worden. Irgendwelche Vorschläge?Nicht in der Lage, ein Array mit Powershell durchzulaufen
#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()