-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNew-DbaJobs.ps1
55 lines (44 loc) · 1.71 KB
/
New-DbaJobs.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Function New-DbaJobs
{
[CmdletBinding()]
Param (
[parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[object]$sqlServer,
[int]$jobsToCreate
)
#Connect to SQL.
Import-Module sqlserver;
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null;
$server = New-Object Microsoft.SqlServer.Management.Smo.Server $sqlServer;
Try{$server.ConnectionContext.Connect()} Catch{Throw "Can't connect to SQL Server $sqlServer."}
$jobs = 1;
While ($jobs -le $jobsToCreate)
{
$steps = (Get-Random -Minimum 1 -Maximum 10); #Random number of steps (1-10).
$s = 1;
#Create job.
$jb = New-Object Microsoft.SqlServer.Management.Smo.Agent.Job($server.JobServer, "DBA - Test Job #$jobs")
$jb.OwnerLoginName = "sa"
$jb.Create()
While($s -le $steps)
{
$wait = (Get-Random -Minimum 1 -Maximum 10);
#Create step(s).
$js = New-Object ('Microsoft.SqlServer.Management.Smo.Agent.JobStep') ($jb, "Step $s")
$js.Command = "WAITFOR DELAY '00:0$wait';" #Random wait time seconds (1-10).
$js.OnSuccessAction = 'QuitWithSuccess'
$js.OnFailAction = 'QuitWithFailure'
$js.Create()
$s++;
} #End While s in steps loop.
#Set start step.
$jsid = 1
$jb.ApplyToTargetServer($server.Name)
$jb.StartStepID = $jsid
$jb.Alter()
$jobs++;
} #End While jobs in jobsToCreate loop.
#Disconnect from SQL.
$server.ConnectionContext.Disconnect();
} #End New-DbaJobs function.
#New-DbaJobs -sqlServer Server1 -jobsToCreate 2