2017-05-25 81 views
0

我有下面的power-shell代碼,試圖將一個excel文件導入DB1。將文件導入到SQL Server 2012時發生錯誤

$dir = "\\server\folder\" 
     $latest = Get-ChildItem -Path $dir | Where-Object {$_.name -like "*Notes*"} | Sort-Object LastWriteTime -Descending | Select-Object -First 1 
     Write-Output "The latest file is: $latest" 
     Write-SqlTableData -ServerInstance "instance1" -DatabaseName "DB1" -SchemaName dbo -TableName Table1 -InputData $latest -Force 

上午陷入錯誤,當我運行此代碼,見下圖:

Write-SqlTableData : A mapping between .Net type 'System.IO.DirectoryInfo' and SQL type for column 'Directory' was not found. Consider removing the column with that type and repeat the operation 

我用Google搜索這個錯誤,似乎沒有得到任何幫助。任何人都可以幫助我嗎?

回答

0

錯誤很明顯;你需要

  • Get-ChildItem輸出到文件(即.xsl.xslx,在所有的可能性),並
  • 供應-InputData作爲$latest.Name,而不是$latest對象System.IO.FileSystemInfo類型(所謂的:這是只有我的猜測,沒有安裝SQL相關的cmdlet)。
$dir = "\\server\folder\" 
$latest = Get-ChildItem -Path $dir | 
    Where-Object {$_.name -like "*Notes*.xsl*"} | 
    Sort-Object LastWriteTime -Descending | Select-Object -First 1 
Write-Output "The latest file is: $latest" 
Write-SqlTableData -ServerInstance "instance1" -DatabaseName "DB1" -SchemaName dbo -TableName Table1 -InputData $latest.Name -Force 

,或者代替Where-Object,使用-Filter parameterGet-ChildItem cmdlet的作爲

$latest = Get-ChildItem -Path $dir -Filter "*Notes*.xsl*" | 
    Sort-Object LastWriteTime -Descending | Select-Object -First 1 

-Filter

以提供程序的格式或語言過濾器。此參數的值爲 限定Path參數。過濾器的語法(包括使用通配符)取決於提供者。過濾器的效率比其他參數高 ,因爲提供者在檢索對象時應用 而不是Windows PowerShell在從 提供程序中檢索對象後過濾對象。

請注意:-InputData參數可能是單引號$latest.FullName

Write-SqlTableData -ServerInstance "instance1" … -InputData"'$($latest.FullName)'"-Force

相關問題