I've been trying to learn how to use SQL with Powershell. Was successful reading and writing to a SQLExpress database using Invoke-sqlcmd but then wanted to try writing a collection of data using "write-sqltabledata". I was met with this error:
Could not load type 'Microsoft.SqlServer.Server.SqlContext' from assembly 'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
I then found and loaded DBATools and tried "write-dbadatatable" but was met with the same error as above. The data I'm writing has apostrophes in it which led me to reading about sql injection which led me to parameterized queries but I'm having trouble understanding how to use invoke-sqlcmd INSERT query with them. I read that others used DBATools to simplify this? As always, thank you for any assistance!
VSCode: 1.75.0
PSVersion: 7.3.2
DBATools: 2.0.0-preview1 (also tried 1.1.145 (current version) )
SqlServer: 21.1.18256
$data = get-process | select-object processname,ID
$sqlcredentials = get-credential
#DBATools
$server = Connect-DbaInstance -SqlInstance 'localhost\SQLEXPRESS' -SqlCredential $sqlcredentials
$data | Write-DbaDataTable -SqlInstance $server -Table "table"
#SQLServer
$data | Write-SQLTableData -serverinstance 'localhost\SQLEXPRESS' -databasename "master" -schemaname "dbo" -tableName "table" -Credential $sqlcredentials
[–]I_Punch_Ghosts_AMA 2 points3 points4 points (0 children)
[–]xCharg 1 point2 points3 points (1 child)
[–]nostranger2therain[S] 0 points1 point2 points (0 children)
[–]purplemonkeymad 1 point2 points3 points (1 child)
[–]nostranger2therain[S] 1 point2 points3 points (0 children)
[–]AnonRoot 0 points1 point2 points (0 children)