Created
July 13, 2017 16:19
-
-
Save RichieBzzzt/2d564b691b25c74dde9b820b6bd840b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cls | |
Set-Location "/home/richie/sqltoolservice/" | |
$Assem = ( | |
"Microsoft.SqlServer.Management.Sdk.Sfc", | |
"Microsoft.SqlServer.Smo", | |
"Microsoft.SqlServer.ConnectionInfo" | |
); | |
Add-Type -AssemblyName $Assem | |
Function Connect-SqlConnection { | |
[CmdletBinding()] | |
param | |
( | |
$instance, | |
$uname, | |
$pword | |
) | |
$sqlConnection = new-object Microsoft.SqlServer.Management.Common.SqlConnectionInfo($instance, $uname, $pword) | |
try { | |
Write-Host "Connecting..." | |
$SqlSvr = New-Object Microsoft.SqlServer.Management.Smo.Server($SqlConnection) | |
Write-Host "Connected!" | |
return $SqlSvr | |
} | |
catch { | |
throw $_.Exception.InnerException.ToString() | |
} | |
} | |
$thisInstance = "localhost" | |
$thisUname = "sa" | |
$thisPword = "" | |
Write-Host "About to run..." | |
$thisSqlConn = Connect-SqlConnection -Instance $ThisInstance -Uname $thisUName -Pword $thisPword | |
$svrJobs = $thisSqlConn.JobServer.Jobs | Select-Object -ExpandProperty Name | |
$svrJobs | |
$jobName = "my_first_job" | |
$job = new-object ('Microsoft.SqlServer.Management.Smo.Agent.Job') ($thisSqlConn.JobServer, $jobName) | |
if ($svrJobs -notcontains $jobName) { | |
try { | |
$job.Create() | |
$job.Refresh() | |
Write-Host "SQL Agent Job $jobName created successfully." | |
} | |
catch { | |
throw $_.Exception.InnerException.ToString() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment