Set Objects of a SMO variable

  • Hello All,

    What I am trying to do here is gather the SQL Server Jobs running on a SQL server and the disable some particular jobs out of them.This is what I use :

    **************************************************************************

    [System.Reflection.Assembly]::LoadWithPartialName(’Microsoft.SqlServer.SMO’) | Out-Null

    $smo = New-Object (’Microsoft.SqlServer.Management.Smo.Server’) "Server\Instance"

    $jobstodisable= get-content C:\jobstodisablefile.txt

    for($i=0; $i -lt $jobstodisable.count; $i++)

    {

    $job= $smo.Jobserver.jobs | where-object {$_.name -eq $jobstodisable[$i]}

    $job.isenabled = "False"

    }

    *************************************************************************

    Even after assigning the value False to the Job it still shows as enabled, I am sure there is something wrong in my approach but I am not able to figure it out. Does any one have any ideas on :

    1. If I am wrong on my syntax anywhere?

    2. If there is some way that I can directly change the value of the "Isenabled" Object to False/True after gathering it. Something like :

    $job | set-object {isenabled -eq "False"}

    Just to clear, the file C:\jobstodisablefile.txt contains the name of the Jobs that are to be disabled.

  • I believe you are very close here you just need to save the changes (so to speak).

    I think this will get it for you.

    [System.Reflection.Assembly]::LoadWithPartialName(’Microsoft.SqlServer.SMO’) | Out-Null

    $smo = New-Object (’Microsoft.SqlServer.Management.Smo.Server’) "Server\Instance"

    $jobstodisable= get-content C:\jobstodisablefile.txt

    for($i=0; $i -lt $jobstodisable.count; $i++)

    {

    $job= $smo.Jobserver.jobs | where-object {$_.name -eq $jobstodisable[$i]}

    $job.isenabled = 0

    $job.Alter()

    }

  • I really feel foolish that I never noticed that I was not committing the changes made to the object.

    Your point worked for me. thanks Mike.

    SQLSErverCentral is really helpfull.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply