powershell start-job hangs when running sql query

I am trying to run a simple sqlquery but as background jobs(via start-job) as i expect multiple such calls..Below is the snippet of my code



 Import-Module "C:\functions\common\sqlps\sqlps.psm1"
    Import-Module "C:\functions\common\sql\sql.psm1"
    $query = "select count(*) from Table1"
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.CommandText = $query
    $SqlCmd.CommandType = [System.Data.CommandType]'Text'
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
    $SqlConnection.ConnectionString = "Server=" + "dbserver\db_live" + ";Database=db"  + ";Persist Security Info=False;User ID=dbuser" + ";Password=dbpwd" + ";Integrated Security=False"
    SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter 
    ([System.Data.SqlClient.SqlCommand]$SqlCmd).Connection = [System.Data.SqlClient.SqlConnection] $SqlConnection
    $SqlAdapter.SelectCommand = $SqlCmd
    $myDataSet = New-Object System.Data.DataSet
    try {
        Write-Host Calling SqlAdapter   
        [void]$SqlAdapter.Fill($myDataSet) # | out-null
    } catch [Exception] {
        write-host "error:" $_
    }
     Write-Host Called SqlAdapter Fill
    $SqlConnection.close()
    return $myDataSet.Tables[0].Rows[0].Column1

It exactly hangs at $SqlAdapter.Fill and even when replaced it with invoke-sqlcmd and passing input file it still hangs ..Note that when i run it normally it works $jobs = start-job -FilePath .\Dev_Temp.ps1 just hangs at .Fill running directly .\Dev_Temp.ps1 returns the count as 3 Not sure what i am doing wrong ..Appreciate any pointers as i am struck here..
September 27th, 2013 9:38am

Hi,

Thanks for your posting.

Firstly, I want to confirm whether there had any errors when you run the scripts above.

Please try to Replace the SqlAdapter in  SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter with $SqlAdapter.

I hope this helps.

Free Windows Admin Tool Kit Click here and download it now
September 30th, 2013 9:34am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics