Powershelll Script running in Task Scheduler copying Output to one drive

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

Tonyd thank you for the tip but when I try to copy and paste that link I get

PowerShell 7.4.13
PS C:\Users\Amber> Install-Module -Name OneDrive -RequiredVersion 2.1.1

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
‘PSGallery’?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is “N”): y
Install-Package: The module ‘OneDrive’ cannot be installed or updated because the authenticode signature of the file ‘OneDrive.psd1’ is not valid.

From a google search:

Common solutions

  • Update PowerShellGet: An outdated PowerShellGet module is a common cause of this issue.
    • Run Update-Module PowerShellGet to install the latest version.
  • Use -SkipPublisherCheck: This will ignore the signature check, allowing the module to install.
    • Use the command Install-Module -Name OneDrive -SkipPublisherCheck.
    • You may also need to add other parameters like -Force and -AllowClobber depending on your situation, e.g., Install-Module -Name OneDrive -Force -AllowClobber -SkipPublisherCheck.

thanks again but

PS C:\Users\Amber> Update-Module PowerShellGet
Update-Module: Module ‘PowerShellGet’ was not installed by using Install-Module, so it cannot be updated.

I am too new at this to continue… I do not know what the aboice mean, and what all the switches do in your Options so I am a little lost. While my SQL script is the PowerShell function on this server, it is a production machine and I not comfortable …

I guess I am going to have to forget it. I did not think it would be so complicated :frowning:

thanks

Totally agree. If you are not comfortable, dont proceed on a production system. Personally, when I install a module from the gallery, I download and extract locally.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.