无法使用PowerShell循环遍历数组

时间:2017-09-08 16:00:47

标签: arrays loops powershell

好的,我需要使用deviceId遍历一个数组。一切都插入正常,问题是obj标签不是从API中提取的,因为它是一个数组并且与设备ID相关联。所以我决定在脚本运行时创建另一个表,它可以拉取设备ID并插入它们,这样就可以从API中提取标签并将其插入到我可以在以后加入它们的另一个表中。问题是我为每个人做了另一个,只是插入最后一个ID,甚至没有标签被拉。有什么建议吗?

#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()

2 个答案:

答案 0 :(得分:1)

在两个循环的每次传递中,您都会在$DeviceIdentifier$DeviceIdentifier = $obj.deviceid / $DeviceIdentifier = $Tags.deviceid中存储一个值。在第二个中,您甚至会覆盖正在使用的集合。

您可以(例如)在使用$array = @()的循环之前创建一个数组,并在$array += $obj.deviceid的循环中为其添加值。

答案 1 :(得分:0)

sodawillow说的是准确而好的建议。不要重复使用这样的变量,并在循环之前定义数组以避免范围问题。也就是说,这看起来应该是一个简单的解决方案。您运行两个循环,但看起来您真的只想迭代相同的数组并将该数组的更新应用于两个不同的表。很酷,只需在循环的每次传递中放入两个SQL语句。另外,我正在将您的$cmd创建移出循环。没有必要每次都重新创建它。

$cmd = $connection.CreateCommand()

#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
    $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()

    Write-Host ($obj.deviceid) -BackgroundColor White -ForegroundColor DarkGreen
    Write-Host ($obj.tags) -BackgroundColor White -ForegroundColor black

    #Inserting into MYSQL database
    $insert_stmt2 = "INSERT INTO [dbo].[TagsTable]([DeviceID],[Tags])
                            VALUES ('$DeviceIdentifier','$tags')" -replace "\s+"," "
    $cmd.CommandText = $insert_stmt2
    write-host $insert_stmt2 -BackgroundColor White -ForegroundColor DarkBlue
    $cmd.ExecuteNonQuery()
}

$Connection.Close()