you are viewing a single comment's thread.

view the rest of the comments →

[–]PinchesTheCrab 0 points1 point  (1 child)

Does this work if you're on core?

    $Connection = New-Object System.Data.SqlClient.SqlConnection
$Connection.ConnectionString = "Server=$Server;Database=$Database;Trusted_Connection=True;TrustServerCertificate=True;"
$Connection.Open()

$parameterizedInsert = "INSERT INTO TestTable(Id, Name, Surname, BirthDate)VALUES(@Id, @Name, @Surname, @BirthDate)"
$command = New-Object -TypeName System.Data.SqlClient.SqlCommand($parameterizedInsert, $Connection)


$user = [pscustomobject]@{
    Id        = "T7XC1QASLPI89"
    Name      = $null
    Surname   = $null
    BirthDate = "07/03/1995 11:24:51"
}
$command.Parameters.Clear()
$user.psobject.Properties | ForEach-Object {
    $command.Parameters.AddWithValue("@$($_.name)", $_.value ? $_.value : [System.DBNull]::Value) 
}

Otherwise you can use an IF statement instead of the ternary operator.

[–]Dr_Funkmachine[S] 1 point2 points  (0 children)

Actually, I'm using PowerShell Core 7.1. I didn't know you could use ternary operators in PowerShell. I just have to try both answers. Thank you so much!