为什么 .NET Folder.CopyHere 方法在 SQL Server 代理 PowerShell 作业步骤中运行时不允许对 .ZIP 文件进行对话框抑制?

pet*_*411 4 powershell compression .net copy sql-server-2008

我在 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
- 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
Run Code Online (Sandbox Code Playgroud)

为什么 vOption 参数对 SQL 作业不起作用?

微软在线社区支持的 Walter Wang 说

...请注意,每个 shell 文件夹都由一个 shell 命名空间扩展(简称 NSE)支持。每个 NSE 将选择拥有自己的机制来复制/移动/删除数据项(普通文件系统路径的文件/文件夹)。

您引用的有关 CopyHere 方法的文档仅适用于普通文件系统路径。这就是为什么您使用选项 4 来禁用进度对话框不适用于 zip 文件夹的原因。

另一方面,上次我与 shell 团队检查时,目前 zip 文件 NSE 的功能仅用于用户交互。

换句话说,官方不支持以编程方式访问 zip 文件 NSE。”

smo*_*oak 5

你需要传递flag

(16)

Respond with "Yes to All" for any dialog box that is displayed.
Run Code Online (Sandbox Code Playgroud)

像这样:

$destination.Copyhere($zip_file.items(), 16) 
Run Code Online (Sandbox Code Playgroud)

您可能希望将其与此标志结合使用:

(4)

Do not display a progress dialog box.
Run Code Online (Sandbox Code Playgroud)

所以你会这样做:

$destination.Copyhere($zip_file.items(), 20)
Run Code Online (Sandbox Code Playgroud)