This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Get-DbaSmoObject.ps1
96 lines (75 loc) · 4.17 KB
/
Get-DbaSmoObject.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function Get-DbaJobCategory {
<#
.SYNOPSIS
Gets SQL Agent Job Category information for each instance(s) of SQL Server.
.DESCRIPTION
The Get-DbaJobCategory returns connected SMO object for SQL Agent Job Category information for each instance(s) of SQL Server.
.PARAMETER SqlInstance
SQL Server name or SMO object representing the SQL Server to connect to. This can be a collection and receive pipeline input to allow the function to be executed against multiple SQL Server instances.
.PARAMETER SqlCredential
Login to the target instance using alternate Windows or SQL Login Authentication. Accepts credential objects (Get-Credential).
.PARAMETER JobCategory
The job category(ies) to process. This list is auto populated from the server. If unspecified, all job categories will be processed.
.PARAMETER ExcludeJobCategory
The job category(ies) to exclude. This list is auto populated from the server.
.PARAMETER EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
.NOTES
Tags: Migration, Backup
Author: FirstName LastName (@twitterhandle and/or website)
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
- License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Get-DbaJobCategory
.EXAMPLE
PS C:\> Get-DbaJobCategory -SqlInstance localhost |
>> foreach { Write-output "Job Category Name: $($_.Name)"
>> }
Returns all SQL Agent Job Categories on the local default SQL Server instance
.EXAMPLE
PS C:\> Get-DbaJobCategory -SqlInstance localhost, sql2016
Returns all SQL Agent Job Categories for the local and sql2016 SQL Server instances
.EXAMPLE
PS C:\> $servers = Get-Content C:\servers.txt
PS C:\> $servers | Get-DbaJobCategory
Returns all SQL Agent Job Categories for the local and sql2016 SQL Server instances
#>
[CmdletBinding()]
param (
[parameter(Position = 0, Mandatory, ValueFromPipeline)]
[DbaInstance[]]$SqlInstance,
[PSCredential]$SqlCredential,
[string[]]$JobCategory,
[string[]]$ExcludeJobCategory,
[switch]$EnableException
)
process {
foreach ($instance in $SqlInstance) {
Write-Message -Level Verbose -Message "Attempting to connect to $instance"
try {
$server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential
}
catch {
Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
}
$categories = $server.JobServer.JobCategory
if ($JobCategory) {
$categories = $categories | Where-Object Name -in $JobCategory
}
if ($ExcludeJobCategory) {
$categories = $categories | Where-Object Name -notin $ExcludeJobCategory
}
foreach ($object in $categories) {
Write-Message -Level Verbose -Message "Processing $object"
Add-Member -Force -InputObject $object -MemberType NoteProperty ComputerName -value $server.ComputerName
Add-Member -Force -InputObject $object -MemberType NoteProperty InstanceName -value $server.ServiceName
Add-Member -Force -InputObject $object -MemberType NoteProperty SqlInstance -value $server.DomainInstanceName
# Select all of the columns you'd like to show
Select-DefaultView -InputObject $object -Property ComputerName, InstanceName, SqlInstance, ID, Name, Whatever, Whatever2
}
}
}
}