打开SQL连接时,我的工作似乎挂起。重现此问题的示例如下:

$myJob = start-job -ScriptBlock {
     $connection_string = [string]::Format("Data Source={0};Initial Catalog={1};User ID={2}; Password={3};Connection Timeout=5", "my_sql_server", "my_database", "my_user", "my_password");
     $connection = New-Object System.Data.SqlClient.SqlConnection -ArgumentList $connection_string;
     Write-Output "Opening Connection...";
     $connection.Open();
     Write-Output "Connection Opened!";
}

当我查看 $ myJob 时,它始终处于的状态。运行,输出为“Opening Connection ...” 。如果我在作业块外运行相同的代码,则运行良好。

如何正确在作业中打开SqlConnection?

编辑:

以下是$ myJob的属性:
PS C:\Documents and Settings\agprax>>> $myJob

Id              Name            State      HasMoreData     Location             Command
--              ----            -----      -----------     --------             -------
3               Administrator   Running    True            localhost             ...

这是作业的输出:
PS C:\Documents and Settings\agprax>>> Receive-Job $myJob
Opening Connection...

最佳答案

您从哪个操作系统运行此作业?值得注意的是,Connect上提交了一个错误,该错误似乎与您所遇到的问题有关。

https://connect.microsoft.com/PowerShell/feedback/details/499499/adding-a-snapin-add-pssnapin-from-within-a-job-start-job-causes-job-to-hang-indefinitely

10-08 11:58