[英]Write an If statement to Insert certain data into SQL
Okay this code works perfect it pulls everything from an API the data i need. 好的,这段代码非常完美,可以从我需要的API中提取所有内容。 But i need it to just insert specific data. 但我需要它只是插入特定的数据。 How do i go in making an IF statement that just inserts data that is named provisioned in Tags and inserts it into SQL. 我该如何制作一个IF语句,该语句仅插入在Tag中设置的命名数据并将其插入SQL。 I am very new to powershell so i do not know how to make this work. 我对Powershell非常陌生,所以我不知道如何进行这项工作。 If someone can guide me in the right direction. 如果有人可以指引我正确的方向。
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
$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()
You could use the Where-Object
cmdlet to filter $Json.devices
down to just objects where Tags
= provisioned
. 您可以使用Where-Object
cmdlet来将$Json.devices
筛选$Json.devices
只包含Tags
= provisioned
对象。
$objs = $Json.devices | Where-Object {$_.tags -eq "provisioned"}
foreach($obj in $objs)
Otherwise you could use an if
statement 否则,您可以使用if
语句
if ($Tags -eq "provisioned) {
#Do logic here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.