Disclaimer:Hey Guys, this post contains affiliate link to help our reader to buy best product\service for them. It helps us because we receive compensation for our time and expenses.

InterServer Web Hosting and VPS

Scenario: PS is most powerful and very helpful tool for windows and SQL server administrator and automation. So its today need to learn at least basic of some PS cmdlets.

In this series of tutorial, we will only walk through some of the basis and need to know cmdlets which will helps us in future in order to manage our SQL server.

For more details, you can google and look for good site.

Note:- Please click here to get 8th part of PowerShell –SQL Automate tutorial.

To get more information about PowerShell cmdlets, you can refer this link.


In this tutorial, we will try to develop a PowerShell code to display SQL services status and save the result either in .TXT(non-formatted) or CSV (Formatted) output.

Here is the code for the same, you can even do more interactive required stuffs.

$ServerList = ("Machine-PC")
#Write-host Server Name Is $ServerList
try
{
If (Test-Connection -ComputerName $ServerList -Quiet -ErrorAction Stop)

 
{
#write-host 'Test Connection Successful!' 
$CSVPath=New-Item -path D:\Established_SQL_Servers_status.csv -Force
$SQLServicesStatusList=get-service | Where-Object {($_.Name -match 'SQL')} |Select-Object $ServerList,Name,status | export-csv $CSVPath -NoTypeInformation
#$SQLServicesStatusList | export-csv $CSVPath
#Set-Content D:\Established_SQL_Servers_status.csv $SQLServicesStatusList -Force

}
}
catch 
{

#Write-Host 'Catch loop is working'
Add-Content D:\Unavailable-Servers.txt $ServerList
}

Download this working PS script from below location https://app.box.com/s/bo4ceu4ca9hbedu2ylb6nkkjlbl293md

Output:-


Even if a gem be placed on the foot and a mirror on the head, Still, the gem will not lose its value

Chanakya

 

One thought on “PowerShell –SQL Automate Tutorial-9

Comments are closed.