Hi All,I am writing this script to check if the sql agent job is running or not and if it is not running disable it.Some how I am not able to disable it and it is not evaluating the if loop. I am missing some stuff here :-Dalso, how can I make the script to take a user input and enable or disable the job.[code="plain"]$srv = New-Object Microsoft.SqlServer.Management.SMO.Server("server\instance")$jobs = $srv.jobserver.jobs#$jobs | where-object {$_.Isenabled -eq $true -and $_.name -like "*backup*"}if ($_.IsEnabled -eq $True -and $_.name -match "*backup*"){ $JobsExecuting = 0 foreach ($job in $jobs) { #if the job is running if ($job.CurrentRunStatus.ToString() -ne "Idle") { write-host "you hit inner loop" $jobExecuting = 1 write-host "The backup job is running ..." break } } if($JobsExecuting -eq 0) { #if the job is not executing write-host "no jobs are running on $($srv)" #disable the job $jobs.IsEnabled = $TRUE $jobs.Alter() }}else{write-host "$($srv.name) has running jobs, try again later" -foregroundcolor Red}[/code]
↧