Unable to Import Date Column from Excel through PowerShell

Viewed 196

I am trying to pull an excel sheet through below PowerShell script which I need to write to a SQL Server later. The 'Report Date' column is unable to import any data. Is there a problem with sytax somewhere? I have attached how my excel sheet looks like.

Excel Sheet

$My_File_Date = $My_FileIN.BaseName.Replace("filename_","").Replace("_","")
$My_Formatted_Date = [datetime]::ParseExact($My_File_Date,"yyyyMMdd",$null).ToString("dd/MM/yyyy")
$My_Fields_File = "$Headers_DIR\My_Fields.txt"
$My_Fields = @{}
$My_Fields = (Get-content "$My_Fields_File" -Delim ",").Replace(",","")
Convert-xlsToxlsx $My_FileIN $My_Converted_File
$My_Data = Import-XLSX -Path $My_Converted_File -Sheet 1 -WarningAction SilentlyContinue
$My_Count = $My_Data.Count
$My_Data | Add-Member -MemberType NoteProperty -Name Distributor -Value "Name Co."
$My_Data | Add-Member -MemberType NoteProperty -Name "File Date" -Value $My_Formatted_Date
Write-Host "File imported - $My_Count records found ... " -NoNewline
Write-Host "Converting to Data Table"
$My_DataTable = 

 ($My_Data |
    Select-Object -Property Distributor,'File Date',@{l='Report Date';e={If ($_.'Report Date' -as [datetime]){$_.'[datetime]::ParseExact("Report Date","dd.MM.yyyy",$null).ToString("dd/MM/yyyy")'} else {[DBNULL]::Value}}},Country
  )





Distributor File Date  Report Date Country
----------- ---------  ----------- -------
Name Co.    09/06/2021             US     
Name Co.    09/06/2021             UK     
Name Co.    09/06/2021             GE
0 Answers
Related