我在SQL Server代理作业步骤中使用PowerShell来自动提取.ZIP文件内容到目录。
基于这个问题的最高票数答案: 如何在Powershell中压缩/解压缩文件?
我正在使用这个代码:
$dir_source = "\\myserver\myshare\" $dir_destination = "\\myserver2\myshare2\" $file_source = Get-ChildItem -Path (Join-Path -Path $dir_source -ChildPath "test.zip") $shell_app = New-Object -com shell.application $zip_file = $shell_app.namespace($file_source.FullName) $destination = $shell_app.namespace($dir_destination) $destination.Copyhere($zip_file.items(),20)
CopyHere方法的vOptions可选参数是20,它指定“不显示进度对话框”。 (4)和“对所有显示的对话框回答”是全部“。 (16)。
此参数在PowerShell脚本编辑器(我正在使用PowerGUI脚本编辑器)中按预期工作。 我可以运行脚本,并再次运行(覆盖scheme),脚本完成,没有错误,没有对话框。 但是,在SQL Server代理PowerShell作业步骤中执行相同的代码会导致当文件已经存在于目标数据库中时挂起的作业。
重现步骤:
- Instantiate code in a SQL Server 2008 SQL Server Agent Job step - Start the SQL job - Result: SQL job completes, unzipped file "test.csv" appears in the $dir_destination folder - Start the SQL job - Result: Job executes indefinitely. - Stop the SQL job - Delete the "test.csv" from the $dir_destination folder - Start the SQL job - Result: SQL job completes, unzipped file "test.csv" appears in the $dir_destination folder
为什么vOption参数不适用于SQL作业?
微软在线社区支持的Walter Wang说 :
…请注意每个shell文件夹由一个shell命名空间扩展(简称NSE)支持。 每个NSE将select有自己的机制来复制/移动/删除数据项(正常文件系统path的文件/文件夹)。
您引用的有关CopyHere方法的文档仅适用于正常的文件系统path。 这就是为什么使用选项4禁用进度对话框不适用于zip文件夹。
另一方面,最后一次我与壳牌团队核对,目前NSE的压缩文件的function只是用于用户交互。
换句话说,以编程方式访问压缩文件NSE不被官方支持。“
我想知道这是否与SQL代理中的Powershell作业步骤的内部有关。 例如,您不能在SQL代理的PowerShell作业步骤中执行写主机,如本博客所述: http : //blogs.msdn.com/b/mwories/archive/2009/09/30/the-use-of-写主机和-SQL服务器代理PowerShell的作业steps.aspx
一个build议 – 尝试设置CmdExec作业步骤,并使用-file参数调用C:\ windows \ System32 \ WindowsPowerShell \ v1.0 \ powershell.exe,而不是使用powershell作业步骤。 这样它将使用cmdexec而不是sqlps。
你需要通过国旗
(16) Respond with "Yes to All" for any dialog box that is displayed.
像这样:
$destination.Copyhere($zip_file.items(), 16)
你可能想要把它和这个标志结合起来:
(4) Do not display a progress dialog box.
所以你会这样做:
$destination.Copyhere($zip_file.items(), 20)