'PowerShell - How to replace a column value while loading an Excel file into SQL server
I am trying to load an Excel sheet into SQL server using Import-Excel module in Powershell. There are columns which have some values - #N/A. While loading I need to replace #N/A to something else. I am trying to use below statement to do this but it is giving me the error.
write-host "Processing TD Equipment File" -ForegroundColor DarkBlue -BackgroundColor Cyan
$TD_File = "$Data_Dir\Backlog_Hardware*.xlsx"
$TD_FileIn = (Get-ChildItem "$TD_File" | Sort-Object LastWriteTime -Descending | Select-Object -first 1)
$TD_File_Date = $TD_FileIn.BaseName.Replace("Backlog_Hardware_","")
$TD_Data = Import-Excel -Path $TD_FileIn -WorksheetName Backlog
$TD_Count = $TD_Data.Count
Write-Host "TD Equipment File imported - $TD_Count records found ... " -NoNewline
Write-Host "Converting to Data Table"
$TD_DataTable = ($TD_Data | Where-object { ($_.'E/S' -replace "``#N/A","NA")} -and ($_.'Physical Stock' -replace "``#N/A","0")|
Select-Object -Property Distributor,'File Date',Customer,'Customer Name','Physical Stock') | Out-DataTable
Invoke-Sqlcmd -Database XXX -ServerInstance XX\XX -Query "Truncate Table tbl1"
Write-SqlTableData -DatabaseName XXX -ServerInstance XX\XX -TableName tbl1 -InputData $TD_DataTable -Timeout 900
I am getting below error:
Out-DataTable : Could not add property 'Physical Stock' with value '#N/A' and type 'OfficeOpenXml.ExcelErrorValue' At line:60 char:812
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
