Using Powershell commands inside SQL Agent Jobs

In this post,I will show an example of using powershell commands inside SQL Agent jobs.

For Example ,I am going to add the below powershell commands in a job as one of the step and schedule it.

Get-EventLog System -Newest 5 | Out-File c:\scripts\Eventlog.txt

Get-Counter -ComputerName $env:COMPUTERNAME -ListSet 'MSSQL*' | Out-File c:\scripts\MSSQLCounters.txt

$wmiQuery = @"SELECT systemName,Name,DriveType,FileSystem,FreeSpace,Capacity,Label FROM Win32_Volume
Get-wmiObject -ComputerName "Sathya-PC" -Query $wmiQuery | Out-File c:\scripts\Drivesproperties.txt

Get-WinEvent -ListLog * | Format-Table -Autosize | Out-File c:\scripts\ListofWinEvent.txt

Like shown in below images ,we have to select the type as Powershell to schedule the execution of powershell commands.

I have piped the output to text file but in Advanced setting of job we have option to specify the job output location.

We can also send mails using powershell commands inside SQL Agent job when previous job step fails

No comments: