2017-05-25 92 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

相关问题