PowerShell执行无结果SQL查询时不生成CSV文件问题咨询
PowerShell 查询无结果时生成CSV文件的解决方案
现有脚本不生成文件的核心原因是,当Invoke-Sqlcmd没有查询到数据时,不会产生任何输出,整个后续管道链不会执行,Set-Content没有收到输入自然不会创建文件。
以下是两种场景的修改方案:
方案1:查询无结果时生成带表头的CSV文件
先把查询结果存入变量,主动传入ConvertTo-Csv,即使结果为空也会输出表头行:
Import-Module Hall.psm1 $Database = 'Report' $Server = '192.168.1.2' $Query = 'SELECT * FROM [Report].[dbo].[TestView]' $LogLocation = "\\Report\LogFile.csv" $DynamicYear = (Get-Date).Year $DynamicMonth = (Get-Culture).DateTimeFormat.GetMonthName((Get-Date).Month) $FileDestination = "\\Report\MONTHLY REPORTS\$DynamicYear\$DynamicMonth\" $Outputfilename='TestView-'+(Get-Date).ToString('MM-dd-yyyy')+'.csv' $LocalCreate = 'C:\Scripts\LocalCreate\' $FolderPathExtension = "Microsoft.PowerShell.Core\FileSystem::" $CodeDestination = $FolderPathExtension+$FileDestination $filedest=$LocalCreate+$outputfilename $Logfile = $FolderPathExtension+$LogLocation # 改动点1:先将查询结果存入变量 $results = Invoke-sqlcmd -querytimeout 120 -query $Query -database $database -serverinstance $server # 改动点2:主动传入结果转换CSV,无结果时也会输出表头 $csvContent = $results | ConvertTo-Csv -NoTypeInformation | ForEach-Object { $_ -replace '"','' } # 移除空行 $csvContent = $csvContent | Where-Object { $_.Trim() -ne "" } # 写入文件,不管有没有内容都会创建 $csvContent | Set-Content -Path $filedest if(Test-Path ($filedest)) { Move-Item -Path $filedest -Destination $CodeDestination -Force $LogType = 'INFO' # 注意:原脚本的$To变量未定义,此处替换为实际的目标路径 $LogEntry = "$filedest MovedTo $CodeDestination" Write-Log -Message $LogEntry -Level $LogType -Logfile $Logfile }
注:原脚本中
SELECT all *的写法有误,all是默认值不需要显式写,已修正。原注释标注Trim header row但Select-Object -Skip 0不会跳过任何行,若需要删除表头可改为Skip 1。
方案2:查询无结果时生成完全空白的CSV文件
判断查询结果为空时直接创建空文件即可:
Import-Module Hall.psm1 $Database = 'Report' $Server = '192.168.1.2' $Query = 'SELECT * FROM [Report].[dbo].[TestView]' $LogLocation = "\\Report\LogFile.csv" $DynamicYear = (Get-Date).Year $DynamicMonth = (Get-Culture).DateTimeFormat.GetMonthName((Get-Date).Month) $FileDestination = "\\Report\MONTHLY REPORTS\$DynamicYear\$DynamicMonth\" $Outputfilename='TestView-'+(Get-Date).ToString('MM-dd-yyyy')+'.csv' $LocalCreate = 'C:\Scripts\LocalCreate\' $FolderPathExtension = "Microsoft.PowerShell.Core\FileSystem::" $CodeDestination = $FolderPathExtension+$FileDestination $filedest=$LocalCreate+$outputfilename $Logfile = $FolderPathExtension+$LogLocation # 改动点1:先将查询结果存入变量 $results = Invoke-sqlcmd -querytimeout 120 -query $Query -database $database -serverinstance $server if ($null -eq $results) { # 查询无结果时直接创建空白文件 New-Item -Path $filedest -ItemType File -Force | Out-Null } else { # 有结果时走原转换逻辑 $results | ConvertTo-Csv -NoTypeInformation | ForEach-Object { $_ -replace '"','' } | Where-Object { $_.Trim() -ne "" } | Set-Content -Path $filedest } if(Test-Path ($filedest)) { Move-Item -Path $filedest -Destination $CodeDestination -Force $LogType = 'INFO' # 注意:原脚本的$To变量未定义,此处替换为实际的目标路径 $LogEntry = "$filedest MovedTo $CodeDestination" Write-Log -Message $LogEntry -Level $LogType -Logfile $Logfile }
内容的提问来源于stack exchange,提问作者dumbsqlguy
相关产品推荐
相关产品推荐

