0

I'm trying to create a PowerShell script that inserts a datatable to SQL via WriteToServer... This script is called by a PowerAutomateDesktop automation. So... I cannot pass my datatable as an argument :( %dt% it s datatable variable which needs to be used inside powershell script. This is my dilemma - it is interpreted as a string or something like that

#Invoke-sqlcmd Connection string parameters
$params = @{'server'='SQLEXPRESS';'Database'='Db'}
 
Write-Output %dt%

#Variable to hold output as data-table
$dataTable = %dt% |  Out-DataTable
#Define Connection string
$connectionString = "Data Source=DSQLEXPRESS; Integrated Security=SSPI;Initial Catalog=Db"
 
#Bulk copy object instantiation
$bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $connectionString
#Define the destination table 
$bulkCopy.DestinationTableName = "dbo.__SALES"
#load the data into the target
$bulkCopy.WriteToServer($dataTable)
#Query the target table to see for output
Invoke-Sqlcmd @params -Query "SELECT  * FROM dbo.__SALES" | format-table -AutoSize

Thanks!

UPDATE

No loner need to pass an argument - I create the datatable inside the script. Thanks again!

Bogdan F
  • 5
  • 2
  • Where's `%dt%` coming from? If it's a variable, it should be `$dt` if anything – Abraham Zinala Jan 27 '22 at 00:15
  • yep - my bad. the %dt% is acting like an argument for the powershell script. It's a datatable variable inside powerautomatedesktop and needs to be used inside the ps script – Bogdan F Jan 27 '22 at 07:17

1 Answers1

0

Work-around: create the datatable inside the script

Bogdan F
  • 5
  • 2