Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Powershell / SQL Inventory and last backup date Skips some servers?

$
0
0

Good Morning experts... I have a text file with a list of servers.. I am trying to automate a script that scans through them all and checks all databases to ensure everything is getting backed up... It works great (I copied the guts of the script from here, I think and modified it so I could "Learn").  The only problem I have is some of the servers report an error saying can not connect and bypasses it.  After the script is over, I can go back and query that single server and it responds correctly.. Has anyone else come across anything like that?  I will include the script I have been using.. if you had 100+ servers to support with MULTIPLE instances on each how would you do it?

<#####################################################################
# Get All instances and Date of last Full / Log Backup of each
# 
#
######################################################################>



#region Variables
$TextFileLocation = "C:\serverlist.txt"
$intRow = 1
$BackgroundColor = 36
$FontColor = 25
#endregion

#Region Open Excel
$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Item(1)
#endregion


#Go through text file one at a time
foreach($instance in Get-Content $TextFileLocation)
{
     $Sheet.Cells.Item($intRow,1) = "INSTANCE NAME:"
     $Sheet.Cells.Item($intRow,2) = $instance
     $Sheet.Cells.Item($intRow,1).Font.Bold = $True
     $Sheet.Cells.Item($intRow,2).Font.Bold = $True
	 for ($column =1; $column -le 2; $column++)
	 {
	 $Sheet.Cells.Item($intRow, $column).Interior.Colorindex = 44
	 $Sheet.Cells.Item($intRow, $column).Font.ColorIndex = $FontColor
	 }
 	 #Increase Row count by 1
     $intRow++
	 #Create sub-headers
      $Sheet.Cells.Item($intRow,1) = "Name"
      $Sheet.Cells.Item($intRow,2) = "LAST FULL BACKUP"
      $Sheet.Cells.Item($intRow,3) = "LAST LOG BACKUP"

	#Format the column headers
	for ($col = 1; $col -le 3; $col++)
    {
          $Sheet.Cells.Item($intRow,$col).Font.Bold = $True
          $Sheet.Cells.Item($intRow,$col).Interior.ColorIndex = $BackgroundColor
          $Sheet.Cells.Item($intRow,$col).Font.ColorIndex = $FontColor
    }
	 #Finished with Headers, now move to the data
     $intRow++
	################################################
	[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | Out-Null

	 # Create an SMO connection to the instance in servers.txt
     $s = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $instance

	$dbs = $s.Databases
	
	foreach ($db in $dbs)
	{
	 if ($db.Name -ne "tempdb")
	 {
	 	 if($db.LastBackupDate -eq "1/1/0001 12:00 AM")
		{
		 	$fullbackupdate = "Never Backed Up"
			$fgColor = "red"
		}
		else
		{
		#$fullBackupDate= "{0:g2}" -f $db.LastBackupDate 
		$fullBackupDate = $db.LastBackupDate
		}
		$Sheet.Cells.Item($intRow, 1) = $db.Name 
    	$Sheet.Cells.Item($intRow, 2) = $db.LastBackupDate #$fullBackupDate 
		if ($db.RecoveryModel.Tostring() -eq "SIMPLE")
        { 
           $logBackupDate="Simple"
		}
		else
		{ 
	       #See Date Above..-eq is same as =
           if($db.LastLogBackupDate -eq "1/1/0001 12:00 AM")  
           { 
               $logBackupDate="Never" 
           } 
           else 
           { 
               #$logBackupDate= "{0:g2}" -f $db.LastLogBackupDate 
			   $logBackupDate = $db.LastLogBackupDate			   
           } 
    	  } 
    	$Sheet.Cells.Item($intRow, 3) = $logBackupdate
	    $intRow ++ 
       } 
   } 
   $intRow ++ 


}

$Sheet.UsedRange.EntireColumn.AutoFit()
cls

Am I going about this the correct way, or is there a "Better" way to do this that I have not come across yet?

Thank you


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>