functions/private/Invoke-Sql.ps1

function Invoke-Sql {    
    [CmdletBinding()]
    param (
        $connectionString,
        $sql,
        $parameters = @{}
    )
    $connection = New-Object System.Data.SqlClient.SQLConnection($connectionString)
    $command = New-Object System.Data.SqlClient.SqlCommand($sql, $connection)
    
    try {
        foreach ($p in $parameters.Keys) {        
            $command.Parameters.AddWithValue("@$p", $parameters[$p])
        }

        $connection.Open()    
        $command.ExecuteNonQuery()
        $connection.Close()        
    }
    catch [System.Data.SqlClient.SqlException] {
        Write-Error "An error ocurred while executing the command. Please ensure the connection string is correct and the identity database has been setup. Connection String: $($connectionString). Error $($_.Exception.Message)"  -ErrorAction Stop
    }    
}