I have this code that bulk copies into a SQL Server table from $dmvResult
(data table).
$dmvResult = DMV_Query 'SELECT [SESSION_ID], [SESSION_SPID]
FROM $SYSTEM.DISCOVER_SESSIONS';
$ConnectionString ='Data Source={0}; Database={1}; Trusted_Connection=True;' -f $Server,$DB
$bulkCopy = new-object Data.SqlClient.SqlBulkCopy($ConnectionString)
$bulkCopy.DestinationTableName=$TableSomething
foreach ($column in $dmvResult.Columns) {
$bulkCopy.ColumnMappings.Add($column.ColumnName, $column.ColumnName) > $null
}
$bulkCopy.WriteToServer($dmvResult)
It works flawlessly, however, it appends the data so more and more rows are created. Can I just somehow overwrite the data in the SQL Server table every time I run the script? I don't want to retain the existing data, but instead overwrite each time the script runs
Essentially, I want to do something like this:
$bulkCopy.WriteToServer($dmvResult) | overwrite