【问题标题】:Why does export-csv not run properly in Powershell on SQL Server?为什么 export-csv 在 SQL Server 上的 Powershell 中无法正常运行?
【发布时间】:2020-04-01 18:06:09
【问题描述】:

我不是 Powershell 专家;但是,我试图简单地运行 Invoke-Sqlcmd 并将结果保存为 .csv 文件。我正在循环多个 *.sql 文件作为查询。当我从 Windows10 桌面运行以下 Powershell 时,一切运行正常,但是当我从 SQL Server 运行它时,我在 export-csv 命令上收到错误。

这是我的 Powershell:

#Prompt for date
$DatePrompt = Read-Host -Prompt 'Enter the database date. (YYYY-MM-DD)'
#Convert to date
$DatabaseDate = [DateTime]::Parse($DatePrompt)
#Create FileDate string
$FileDate = $DatabaseDate.ToString("yyyyMMdd")
#Create SQLDate string
$SQLDate = $DatabaseDate.ToString("yyyy-MM-dd")
#--------------------------------------------------------------------------------------------------------------------------------
#Provide SQLServerName
$SQLServer ="SQLSERVER\MISDATA"
#Provide Database Name
$DatabaseName ="StagingDB"
#Set MonthStart and MonthEnd (YYYY-MM-DD)
$Variables = "FileDate = $SQLDate"
#Scripts Folder Path
$FolderPath ="\\SQLSERVER\MISDATA\Queries"
#Output Folder Path
$OutputFolderPath ="\\SQLSERVER\MISDATA\Results"
#--------------------------------------------------------------------------------------------------------------------------------
#Loop through the .sql files and run them
foreach ($filename in get-childitem -path $FolderPath -filter "*.sql")
{   #Run SQL Query
    Invoke-Sqlcmd -ServerInstance $SQLServer -Database $DatabaseName -InputFile $filename.fullname -Variable $Variables -Querytimeout 0|
    #Export to CSV
    Export-Csv -NoTypeInformation -Path ($OutputFolderPath + "\" + $FileDate + "-" + $filename.basename + ".csv") -Encoding UTF8
    #Write to ConsoleHost
    Write-Host $filename.name "completed"
}

这是错误:

Export-Csv : Cannot open file because the current provider (Microsoft.SqlServer.Management.PSProvider\SqlServer) cannot open a file.
At C:\MISDATA\Queries\Run Scripts.ps1:26 char:5
+     Export-Csv -NoTypeInformation -Path ($OutputFolderPath + "\" + $FileDate + " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], PSInvalidOperationException
    + FullyQualifiedErrorId : ReadWriteFileNotFileSystemProvider,Microsoft.PowerShell.Commands.ExportCsvCommand
sqlquery1.sql completed
Export-Csv : Cannot open file because the current provider (Microsoft.SqlServer.Management.PSProvider\SqlServer) cannot open a file.
At C:\MISDATA\Queries\Run Scripts.ps1:26 char:5
+     Export-Csv -NoTypeInformation -Path ($OutputFolderPath + "\" + $FileDate + " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], PSInvalidOperationException
    + FullyQualifiedErrorId : ReadWriteFileNotFileSystemProvider,Microsoft.PowerShell.Commands.ExportCsvCommand
sqlquery2.sql completed
Export-Csv : Cannot open file because the current provider (Microsoft.SqlServer.Management.PSProvider\SqlServer) cannot open a file.
At C:\MISDATA\Queries\Run Scripts.ps1:26 char:5
+     Export-Csv -NoTypeInformation -Path ($OutputFolderPath + "\" + $FileDate + " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], PSInvalidOperationException
    + FullyQualifiedErrorId : ReadWriteFileNotFileSystemProvider,Microsoft.PowerShell.Commands.ExportCsvCommand
sqlquery3.sql completed

【问题讨论】:

  • 也许您的命令正在将您当前的工作位置更改为非文件系统提供程序。如果你在 Export-Csv 之前 cd c: 会发生什么?
  • 您可能需要将Get-ChildItem ... 部分括在foreach($filename in ... 内的括号中

标签: powershell csv


【解决方案1】:

您需要指定 powershell 规则的 UNC 格式。 $OutputFolderPath ="Filesystem::\\SQLSERVER\MISDATA\Results" 或者 $OutputFolderPath ="Microsoft.PowerShell.Core\FileSystem::\\SQLSERVER\MISDATA\Results"

【讨论】:

  • 我知道你很兴奋,但请尽量控制你的语言。 Stack Overflow 更像是维基百科而不是 Reddit。
猜你喜欢
  • 2019-11-02
  • 2020-12-06
  • 2021-12-12
  • 1970-01-01
  • 2016-02-12
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
  • 1970-01-01
相关资源
最近更新 更多