How can I make bulkCopy map the same columns automatically?

Suppose I have a SQL Table that has these columns:

[server_name],[SESSION_ID],[SESSION_SPID]
I am trying to copy values stored in a data table ($dmvResult) to the SQL Table above ($Table)
$dmvResult = DMV_Query 'SELECT [SESSION_ID]
      ,[SESSION_SPID]
FROM $SYSTEM.DISCOVER_SESSIONS';

$ConnectionString =‘Data Source=$server; Database=$database; Trusted_Connection=True;’
$bulkCopy
= new-object Data.SqlClient.SqlBulkCopy($ConnectionString)
$bulkCopy
.DestinationTableName=$Table
$bulkCopy
.WriteToServer($dmvResult)


While the copying is being done successfully, there is an issue: it’s copying by position, not by column name match. In other words, the copied columns are not being mapped and copied to the same columns.

[SESSION_ID] is being copied to [server_name] and [SESSION_SPID] is being copied to [SESSION_ID]

How can I tell bulkCopy to match columns and copy?

The result copy should be [server_name] being empty because it wasn’t selected from DMV query.

I found a neat solution in this thread:

https://stackoverflow.com/a/20045505/8397835

but I dont know how to translate it to my powershell code:

var meta = definition.Context.Mapping.GetMetaType(typeof(T));
        foreach (var col in meta.DataMembers)
        {
            copy.ColumnMappings.Add(col.Member.Name, col.MappedName);
        }

https://social.technet.microsoft.com/Forums/en-US/25e54426-e4f2-40d2-907e-847f09ffd6f3/how-to-automatically-map-columns-from-datatable-to-a-sql-table-with-bulkcopy?forum=winserverpowershell

[quote quote=137630]https://social.technet.microsoft.com/Forums/en-US/25e54426-e4f2-40d2-907e-847f09ffd6f3/how-to-automatically-map-columns-from-datatable-to-a-sql-table-with-bulkcopy?forum=winserverpowershell

[/quote]
Yes, this is my thread. I didnt get response yet here so I decided to open one there too as well