Connect to SQL server using powershell

Hello, I'm just trying to connect to the SQL server using powersehll and check the job status of all the jobs in that server.

The server I'm trying to connect to is a local host and instance is MSSQLSERVER(Localhost\MSSQLSERVER)

Here's the code that I'm using

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$sql = New-Object ('Microsoft.SqlServer.Management.Smo.Server') ".\MSSQLSERVER"
$sql| Get-Member -name *job*
$sql.JobServer

As far as I know, this should give me a list of all the job exists in that instance. But the script simply  executes but would not shown an output on the screen, except for this 

$sql| Get-Member -name *job*

which simply lists all the members of the object 'sql.' Further, I've verified that the server agent is running and also a job is running currently.

Any inputs here ?

Thank

August 27th, 2015 1:08am

Hi Niranjan,

Please see below sample.
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
#use '.' when connection to a default instance
$sql = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "."
$sql| Get-Member -name *job*
#use EnumJobs to enumerate a list of information about current jobs
$sql.JobServer.EnumJobs()

For more APIs, you can refer to JobServer.

If you have any question, feel free to let me know.
Free Windows Admin Tool Kit Click here and download it now
August 28th, 2015 3:10am

Than you very much, Eric.. That was a whole lot of info !!!
August 28th, 2015 12:24pm

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

Other recent topics Other recent topics