I am new to PowerShell and I am trying to import data from a CSV log file into a SQL database.
I use ForEach to format the table and inject the values into a query using Invoke-Sqlcmd, which works okay except the date and time format needed to be changed.
I started by reformatting the date and time to match the smalldatetime format.
I have looked through various sites trying to figure out how to format the text back into its original form. Aside from reprocessing it back into a new CSV file and repeating the process, I can't think of a way to accomplish this offhand ( And I don't even know if that would work as intended or not. )
$CSVImport = Import-CSV $FileBrowser.FileName -Header $FSHeader | Select $_ | ? Time -NotLike 'Time'
$CSVRowCount = $CSVImport.Count
"Inserting $CSVRowCount rows from CSV into SQL Table"
ForEach ($CSVLine in $CSVImport) {
$CSVLine
$CSVLine = $CSVLine -Replace '(\d{1,2})\/(\d{1,2})\/(\d{4})', '$3/$1/$2'
$CSVLine = $CSVLine -Replace '(\d{1,2})\/(\d{1,2})\/(\d{4})', '$3/$1/$2'
The output from my $CSVLine variable shows:
Date ColumnB ColumnC ColumnD
-------------- ------- ------- ------
2/7/2017 13:28 Second Third Last
Afterwards, the format changes to.
@{Date=2017/2/7 13:28; ColumnB=Second; ColumnC=Third; ColumnD=Last}
I am unsure how to utilize the output for the query or reformat it back to the table that existed before.
Any recommendations in how to proceed with this?
Thank you in advance.