SQL Server Services and cluster info along with last reboot time of Server


The below powershell script provide the information of when the server was last rebooted and the status of SQL Server Services and Cluster resources if the sql server is clustered environment.

clear
$servernames="C:\ServerList\ServerList.txt"
Get-Content $servernames|
ForEach-Object {

$ComputerName=$_
$S=Get-WmiObject `
-Class Win32_SystemServices `
-ComputerName $ComputerName

IF ($S|Select PartComponent | Where-Object {$_ -like "*ClusSvc*"})
{

Write-OutPut "$ComputerName is clustered"
Invoke-command `
-ComputerName $ComputerName `
-ScriptBlock{
Get-WmiObject Win32_operatingsystem|
Select-Object CSName,@{Label='LastBootTime';EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
}
Invoke-Command `
-ComputerName $ComputerName `
-ScriptBlock{
Get-Service -Name "*sql*"
}|Format-Table -AutoSize

Invoke-Command `
-ComputerName $ComputerName `
-ScriptBlock{
Get-ClusterResource
}|Select-Object Name,State,ResourceType,PSComputerName|Format-Table -AutoSize
}
else #If a server is not clustered then all the services should be up and running.
{

Write-OutPut "$ComputerName is not clustered"
#This provides last uptime

Invoke-command `
-ComputerName $ComputerName `
-ScriptBlock{
Get-WmiObject Win32_operatingsystem|
Select-Object CSName,@{Label='LastBootTime';EXPRESSION={$_.ConverttoDateTime($_.lastbootuptime)}}
}
Invoke-Command `
-ComputerName $ComputerName `
-ScriptBlock{
Get-Service -Name "*sql*"

}|Format-Table -AutoSize
}
}
#|Out-File "C:\ServerList\Results.txt"

Comments

Popular posts from this blog

System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

Pre login Handshake or Connection Timeout Period

Transparent Data Encryption(TDE) with Master Key and Certificate in SQL Server