0
好的,这段代码完美地工作,它从API中抽取我需要的所有数据。但我需要它来插入特定的数据。我如何去制作一个IF语句,它只是插入标签中名为供应的数据并将其插入到SQL中。我对PowerShell非常陌生,所以我不知道如何使这项工作。如果有人能指引我走向正确的方向。编写一条If语句将某些数据插入SQL
For循环使变量的对象
foreach($obj in $Json.devices)
{
$DeviceIdentifier = $obj.deviceId
$DeviceNombre = $obj.deviceName
$DomainNombre = $obj.domainName
$Tags =$obj.tags
Write-Host ($obj.deviceId) -BackgroundColor White -ForegroundColor darkblue
Write-Host ($obj.devicename) -BackgroundColor White -ForegroundColor red
Write-Host ($obj.domainname) -BackgroundColor White -ForegroundColor red
Write-Host ($obj.tags) -BackgroundColor White -ForegroundColor black
插入到MYSQL数据库
$cmd = $connection.CreateCommand()
$insert_stmt = "INSERT INTO [dbo].[Tags]([DeviceID],[DeviceName],[DomainName],[Tags])
VALUES ('$DeviceIdentifier','$DeviceNombre', '$DomainNombre','$tags')" -replace "\s+"," "
$cmd.CommandText = $insert_stmt
write-host $insert_stmt -BackgroundColor White -ForegroundColor DarkBlue
$cmd.ExecuteNonQuery()
}
$Connection.Close()
将在哪里,我的OBJ去后,为每个语句或在我的每一个? – Scrat50
使用'Where-Object',你可以在你绕过它之前过滤对象。只运行在剩下的元素上。用'if',你可以把它放在循环中,并评估你循环的每个元素的条件。 – BenH
让你谢谢你本我会试一试。 – Scrat50