lundi 11 septembre 2017

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. 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. I am very new to powershell so i do not know how to make this work. If someone can guide me in the right direction.

For loop giving variables to the objects

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

Inserting into MYSQL database

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

Aucun commentaire:

Enregistrer un commentaire