我在 SQL Server 代理作业步骤中使用 PowerShell 自动将 .ZIP 文件内容提取到目录。
基于这个问题的最高投票答案:How to zip/unzip files in 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)
Run Code Online (Sandbox Code Playgroud)
CopyHere 方法的 vOptions 可选参数是 20,它指定“不显示进度对话框”。(4) 和“对显示的任何对话框用“全部是”来响应。” (16).
此参数在 PowerShell 脚本编辑器中按预期工作(我使用的是 PowerGUI 脚本编辑器)。我可以运行脚本,然后再次运行它(覆盖场景),脚本完成时没有错误,也没有对话框。但是,当目标数据库中已存在文件时,在 SQL Server 代理 PowerShell 作业步骤中执行相同的代码会导致作业挂起。
重现步骤:
- Instantiate code in a SQL Server 2008 SQL Server Agent Job step
- Start the SQL job
- …Run Code Online (Sandbox Code Playgroud)