Get-AgentJobStep
DBSystems: Gets SQL Server Agent job step information
#Requires -Version 5.0
#Requires -Modules SQLServer
[CmdLetBinding()]
Param(
[Parameter(Mandatory = $true)]
[string]$ServerInstance,
[pscredential]$ServerCredential,
[string]$JobName,
[string]$StepName,
[int]$ConnectionTimeout = 30,
[ValidateSet('*','Name','ID','State','Command','LastRunDate','LastRunDuration','LastRunDurationAsTimeSpan','OnFailAction','OnSuccessAction')]
[string[]]$Properties = @('Name','ID','State','Command','LastRunDate','LastRunDuration','LastRunDurationAsTimeSpan','OnFailAction','OnSuccessAction')
)
function Get-SqlServerInstanceInternal {
[CmdLetBinding()]
Param(
[Parameter(Mandatory = $true)]
[string]$ServerInstance,
[pscredential]$ServerCredential,
[int]$ConnectionTimeout = 30
)
try {
[hashtable]$cmdArgs = @{
'ErrorAction' = 'Stop'
'Confirm' = $false
'ServerInstance' = $ServerInstance
'ConnectionTimeout' = $ConnectionTimeout
}
if ($null -ne $ServerCredential) {
$cmdArgs.Add('Credential', $ServerCredential)
}
return Get-SqlInstance @cmdArgs
} catch {
throw
}
}
Import-Module SQLServer
try {
if ($Properties -contains '*') {
$Properties = @('*')
}
$instance = Get-SqlServerInstanceInternal -ServerInstance $ServerInstance -ServerCredential $ServerCredential -ConnectionTimeout $ConnectionTimeout
[hashtable]$cmdJob = @{'ErrorAction' = 'Stop'}
[hashtable]$cmdStep = @{'ErrorAction' = 'Stop'}
if (-not [string]::IsNullOrWhiteSpace($JobName)) {
$cmdJob.Add('Name', $JobName)
}
if (-not [string]::IsNullOrWhiteSpace($StepName)) {
$cmdStep.Add('Name', $StepName)
}
$agent = Get-SqlAgent -InputObject $instance -ErrorAction Stop
$jobs = $agent | Get-SqlAgentJob @cmdJob
$result = $jobs | Get-SqlAgentJobStep @cmdStep | Select-Object $Properties
Write-Output $result
} catch {
throw
}Specifies the name of the target computer including the instance name, e.g. MyServer\Instance
Specifies a PSCredential object for the connection to the SQL Server. ServerCredential is ONLY used for SQL Logins. When you are using Windows Authentication you don't specify -Credential. It is picked up from your current login.
Specifies the name of the target Job object
Specifies the name of the JobStep object to retrieve
Specifies the time period to retry the command on the target server
List of properties to expand, comma separated e.g. Name,Command. Use * for all properties