Hi
First, Let me say I am not very good at Powershell I am truly a hacker at best. I can read and make things work but the finer nuances allude me… and this one really alludes me
I have a PowerShell script that you see below that that calls SQL and outputs the results to a .CSV file and it is scheduled via the task manager running nightly but I have the need to put the output of the script onto OneDrive so it can be shared with a third party.
I cannot figure out how to do it… It seems that in order do this I need to do a lot of things that quite frankly I do not understand so I was hoping someone would take pity on me and help me or tell e where I might be able to get some help
Define the directory that will be used for output
$directory = “C:\exports”
Get the current date in the desired format (e.g., yyyy-MM-dd)
$date = Get-Date -Format “yyyyMMdd”
Define the file name with the date included
$fileName = “MPN-ProductID_$date.csv”
Combine the directory and file name
$outputFile = Join-Path -Path $directory -ChildPath $fileName
SQL credentials
$SqlUser = “PARead”
$SqlPassword = “XXXXXXX”
SQL Query
$Query = @"
SELECT
Items.iID AS product_id,
LTRIM(RTRIM(s.ProductNumber)) AS mpn
FROM Items
LEFT JOIN POS..Skus s ON Items.strSku = s.Sku
WHERE ISNULL(LTRIM(RTRIM(s.ProductNumber)), ‘’) <> ‘’;
"@
Build connection string (SQL auth)
$connectionString = “Server=$SqlServer;Database=$Database;User Id=$SqlUser;Password=$SqlPassword;”
Open connection and run query
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$command = $connection.CreateCommand()
$command.CommandText = $Query
$adapter = New-Object System.Data.SqlClient.SqlDataAdapter $command
$table = New-Object System.Data.DataTable
$adapter.Fill($table) | Out-Null
Export to CSV with headers
$table | Export-Csv -Path $OutputFile -NoTypeInformation -Encoding UTF8
Write-Host “Export completed” $OutputFile