将文件系统::\\to file path从SQL导出一个空白CSV

将文件系统::\\to file path从SQL导出一个空白CSV,sql,powershell,csv,filesystems,export,Sql,Powershell,Csv,Filesystems,Export,首先,当我在Powershell中运行该脚本时,它的工作状态与我一样。它带着所有的数据到达了目的地 $adjustedDate=(获取日期).AddDays(-1) $dateString=获取日期-日期$adjustedDate-格式“%m” $dateString+=“” $dateString+=获取日期-日期$adjustedDate-格式“%d” $dateString+=“” $dateString+=获取日期-日期$adjustedDate-格式“%y” $query=“\\Dri

首先,当我在Powershell中运行该脚本时,它的工作状态与我一样。它带着所有的数据到达了目的地

$adjustedDate=(获取日期).AddDays(-1)
$dateString=获取日期-日期$adjustedDate-格式“%m”
$dateString+=“”
$dateString+=获取日期-日期$adjustedDate-格式“%d”
$dateString+=“”
$dateString+=获取日期-日期$adjustedDate-格式“%y”
$query=“\\Drive\Folder\Folder\Folder\Insurance Status.sql”
$instanceName=“SQL-SERVER”
$csvFilePath=“\\Drive\Folder\Folder\Folder\Insurance Status”
$csvFilePath+=$dateString
$csvFilePath+=“.csv”
$results=Invoke Sqlcmd-InputFile$query-Querytimeout 0-ServerInstance$instanceName
$results |导出Csv$csvFilePath-notype信息
但是,当我通过任务调度器运行它时,任务将成功完成,但导出文件从未到达映射驱动器文件夹中的目标

为了让任务调度器将CSV导出到映射驱动器,我在路径名中添加了“FileSystem:”

$adjustedDate=(获取日期).AddDays(-1)
$dateString=获取日期-日期$adjustedDate-格式“%m”
$dateString+=“”
$dateString+=获取日期-日期$adjustedDate-格式“%d”
$dateString+=“”
$dateString+=获取日期-日期$adjustedDate-格式“%y”
$query=“FileSystem::\\Drive\Folder\Folder\Folder\Insurance Status.sql”
$instanceName=“SQL-SERVER”
$csvFilePath=“文件系统::\\Drive\Folder\Folder\Folder\Insurance Status”
$csvFilePath+=$dateString
$csvFilePath+=“.csv”
$results=Invoke Sqlcmd-InputFile$query-Querytimeout 0-ServerInstance$instanceName
$results |导出Csv$csvFilePath-notype信息

现在,当我通过调度程序运行它时,CSV将其放入映射的驱动器文件夹,但CSV为空。我缺少什么?

在任务计划程序中运行脚本的上下文可能会更改/影响SQL Server模块(sqlps/sqlserver)的使用。您不指定正在使用的模块,但两个模块中都存在
Invoke Sqlcmd

在某些情况下,导入sqlps模块会将您的路径更改为SQLSERVER提供程序(
SQLSERVER:\
)。当这种情况发生时,它会改变您访问本地文件系统的方式,而不再知道提供程序。这就是为什么它需要添加
文件系统::
,以便它知道在导出数据时要使用哪个提供程序

较新版本的
sqlserver
模块更改了提供程序的使用方式,因此不再将上下文/当前目录更改为sqlserver:\provider。您可以考虑使用该模块并将其显式导入到脚本中。

< P>我认为这是问题的根源。

就您可以做什么而言,有多种选择:

  • 加载SQL模块时,提示更改为SQLSERVER:\drive;因此,您可以在脚本开始时加载它,然后更改上下文。例如下面。这可能就是为什么您在调度程序中看到不同的行为;在交互式PS会话中,您已经加载了模块,因此不会在每次运行脚本时重新加载模块。在任务调度器中,它每次运行时都会获得一个新会话,因此每次都必须加载模块
导入模块SQLPS
设置位置c:\\或任何位置;如果您在脚本中,可能需要$PSScriptRoot;虽然如果您在脚本文件之外运行代码,这没有任何价值,但如果您在cmdline中粘贴代码,则可能会造成混淆
  • 或者,您可以使用pushd/popd环绕对模块cmdlet的调用,以便在调用后重置当前路径
推送位置c:\\不在乎在哪里,只要它存在
调用Sqlcmd-InputFile$query-Querytimeout 0-ServerInstance$instanceName
Pop Location(弹出定位)#将您移回调用Push Location之前的位置
  • 或者你可以给我们一个不同的方法。就我个人而言,我非常讨厌使用此模块所带来的不可预知的副作用,以至于我编写了自己的代码:

函数调用CCSqlCommand{
[CmdletBinding(DefaultParameterSetName='ByQueryByConnection')]
param(
[参数(必需=$true,参数setName='ByQueryByConnection')]
[参数(必需=$true,参数setName='ByPathByConnection')]
[System.Data.SqlClient.SqlConnection]$Connection
,
[参数(必需=$true,参数setName='ByQueryByConnectionString')]
[参数(必需=$true,参数setName='ByPathByConnectionString')]
[字符串]$ConnectionString
,
[参数(必需=$true,参数setName='ByQueryByProperties')]
[参数(必需=$true,参数setName='ByPathByProperties')]
[string]$DbInstance
,
[参数(必需=$false,参数setName='ByQueryByProperties')]
[参数(必需=$false,参数setName='ByPathByProperties')]
[string]$DbCatalog='master'
,
[参数(必需=$true,参数setName='ByQueryByConnection')]
[参数(必需=$true,参数setName='ByQueryByConnectionString')]
[参数(必需=$true,参数setName='ByQueryByProperties')]
[字符串]$Query
,
[参数(必需=$true,参数setName='ByPathByConnection')]
[参数(必需=$true,参数setName='ByPathByConnectionString')]
[参数(必需=$true,参数setName='ByPathByProperties')]
[字符串]$Path
,
[参数(必需=$false)]
[哈希表]$Params=@{}
,
[参数(必需=$false)]
[int]$CommandTimeoutSeconds=30
,
[参数(必需=$false)]
[int]$ConnectionTimeoutSeconds=15
,
[参数(必需=$false,参数setName='ByQueryByProperties'
$adjustedDate = (Get-Date).AddDays(-1)
$dateString = Get-Date -Date $adjustedDate -UFormat "%m"
$dateString += " "
$dateString += Get-Date -Date $adjustedDate -UFormat "%d"
$dateString += " "
$dateString += Get-Date -Date $adjustedDate -UFormat "%y"

$query = "\\Drive\Folder\Folder\Folder\Insurance Status.sql"
$instanceName = "SQL-SERVER"

$csvFilePath = "FileSystem::\\Drive\Folder\Folder\Folder\Insurance Status "
$csvFilePath += $dateString
$csvFilePath += ".csv"

$results = Invoke-Sqlcmd -InputFile $query -Querytimeout 0 -ServerInstance $instanceName
$results | Export-Csv $csvFilePath -NoTypeInformation