Exceptional PowerShell DBA Pt 2 – The Morning Checklist

Laerte Junior takes us further into the world of an Exceptional PowerShell DBA, showing us how he uses PowerShell 2.0 to take all the headaches out of even more of his daily checklist. What could be better than having your morning checklist run itself?

I confess that when I started working with PowerShell, I envisioned functions as behaving the same way as Visual Basic or C#, i.e. everything being done within the single function. Thus it was big and full of conditions. Yet one of the great features of PowerShell is consistency; Everything in PowerShell is an object, and the output from one cmdlet can be easily “piped” into the input of another. This means your functions are generic, and over just two or three lines of code you can filter, apply summations, dictate output format, manage inserts into a SQL Server table etc…

As a result, PowerShell is an incredibly versatile scripting language which makes proactive database administration potentially pain-free. In my previous article, I demonstrated how to deal with orphaned users using PowerShell and SMO. This time around, I’ll take you through some more of my daily checklists and management tasks which have been ‘PowerShelled’ to make my life infinitely easier! Bear in mind that, as with my last article, my scripts require PowerShell 2.0, and all I’ll be giving you today are my scripts (which you can download from the top of this article) and examples on how to use them. I think they’re incredibly useful, and if you have any questions about any of them, feel free to leave a comment. So, without further ado:

Recording log information

Let’s start by talking a little more about Save-MSSQLMsg. It is one of most important functions here, because it will generate a log of all messages, be they error messages or simply information. As a result, I will always use this function in conjunction with others to ensure I know exactly what is going on in my servers. It takes 6 parameters, and only the first two (file and server names) are mandatory. The other 4 are not informed of the default values, as they are used by the script to insert information into the log files.

So, for generating an error message or simply information, we use :

900-logfile.jpg

But if we want only one file to be generated, we must constantly pass the same script name and date to the function, i.e., we need to set this in the parameters at the beginning of the function, and then we can use the function like this:

900-logfile2.jpg

Returning Backup Information

“Laerte, I need all backup information about backups in our databases, and if we had some of them without backup”

“Yes boss. Right away. How do you want that information? Email ? Snail Mail ? Smoke Signal ? Drums ?”

Not a problem; You’re an Exceptional DBA who has everything ready for your checks, and PowerShell under your command. As the heading suggests, this function returns information about your database backups. The properties return by the object are :

LineNumber

(Int)

Date

(DateTime)

ServerName

(string)

DatabaseName

(string)

LastBackupDate

(DateTime)

LastDifferentialBackupDate

(DateTime)

LastLogBackupDate

(DateTime)

Bear in mind that you can control which servers this script checks by supplying a list of targets in a flat text file – C:\TEMP\Servers.txt. If you don’t supply this file, the script will only check the default server. Take a look at the script below, and I’ll give some examples of how to use it after the jump>


So, now that you’ve seen the function, here are some examples on how to use it :

  • List all backup information on the default server:

  • List all backup information on target server[s] (supplied in the text file):

  • List all databases which haven’t been backed up for one day:

  • List backup information for all databases, and store it in a SQL Server table:

  • Generate TSQL to list all data from the “BackupsXML” table, where the ServerName condition = “Jupiter”:

Now that we’ve got this script working, let’s generate a CSV file and email it to our beloved leader:

900-backupCSV.gif

Returns Linked Servers Information and Connectivity

“Laerteeeee (yes..he loves my name) , Some of our remote procedures are returning errors. What is happening?”

“I will check the linked servers again, my lord”

I know..I know.. PowerShell is my middle name.

This function returns an object with information about all your linked servers and your connectivity. As with the others functions, you can pass a file to the script with all the servers you want to test, or else leave it empty if you want the current server. Likewise, this script will also generate a log file if some server is off or another problem occurs.The properties returned are :

LineNumber

(int)

Date

(datetime)

ServerName

(string)

LinkedServerName

(string)

DataSource

(string)

DateLastModified

(datetime)

CollationCompatible

(boolean)

DataAccess

(boolean)

RPC

(boolean)

RpcOut

(boolean)

UseRemoteCollation

(boolean)

CollationName

(string)

ConnectionTimeOut

(int)

QueryTimeOut

(int)

Distributor

(boolean)

Publisher

(boolean)

Subscriber

(boolean)

LazySchemaValidation

(boolean)

EnablePromotionofDistributedTransactionsForRPC

(boolean)

ProviderName

(string)

ProductName

(string)

Connectivity

(boolean)

… And just as before, some examples on how to use it:

  • List All the LinkedServers information for the default Server:

  • >List All the LinkedServers information for All target servers:

  • List All the LinkedServers information for All target servers, and filter the data down to just the name and connectivity test data:

  • List All the LinkedServers information for All target servers, and filter the data down to just the name and connectivity test data for results where Connectivity = false):

  • List All the LinkedServers information for All target servers, filter the data down to just the name and connectivity test results where Connectivity = false), and store the data in a ‘LinkerServers’ SQL Server Table:

  • Generate TSQL to list all the data from the ‘LinkerServers’ table where the ServerName condition = “Jupiter”

Return Logins information

“Laerte, my Padawan, we need to know all the logins created in our servers during last week”

“Yes my Master. I´m an Exceptional DBA and The PowerShell is with me.”

This function returns an object containing:

LineNumber

(int)

Date

(datetime)

ServerName

(string)

LoginName

(string)

CreateDate

(Datetime)

DateLastModified

(Datetime)

LoginType

(String)

AsymmetricKeys

(String)

Certificate

(String)

DefaultDatabase

(String)

DenyWindowsLogin

(Boolean)

HasAccess

(Boolean)

IsDisable

(Boolean)

IsLocked

(Boolean)

IsPassowordExpired

(Boolean)

IsSystemObject

(Boolean)

Language

(String)

LanguageAlias

(String)

MustChangePassword

(Boolean)

PasswordExpirationEnabled

(Boolean)

PasswordPolicyEnforced

(Boolean)

… And the script itself looks like this:

As before you can easily apply all the filters you want , this function works with logs, and the usage patterns are basically the same:

  • List All Logins information on the default server:

  • List All Logins information on All target servers:

  • List All Logins information on All target servers, filter the data down to just the name and CreateDate:

  • List All Logins information on All target servers filter the data down to just the name and where the CreateDate is less than 7 days ago, and store the results in a SQL Server Table:

  • Generate TSQL to list the data where the ServerName condition = “Jupiter”:

Returning Jobs information

“Laerte, I see that every morning when you get to work, you have an excel list in your inbox listing the problematic backups on all the servers in the environment. Out of curiosity, how do you do that? I couldn’t find Database Mail configured, checking procedures or any other mechanism in place to audit jobs in servers.”

From my machine, with a few lines of code, I access all the servers’ lists of the jobs that failed, in XLSX format, and send that information to my email. I don’t have any procedures or mechanism on the servers;. the function is uniform, repetitive and centralized; it returns the following information:

LineNumber

(int)

Date

(DateTime)

ServerName

(String)

JobName

(String)

Lastrunoutcome

(String)

Isenabled

(Boolean)

lastrundate

(DateTime)

And now, once we’ve collected all that iinformation, we can create a XLSX and send to our email:

If you want to see more about PowerShell exporting data to Excel, be sure to read this excellent content on TechNet:

Now it only remains for you to schedule this script for a job in SQL Server, or even Windows Task Scheduler. Keep in mind that these functions are just examples, and do not return all the properties of the SMO objects used. If you want a complete list of properties, I suggest using MSDN and studying the classes in question.

So, in this second part of my Exceptional PowerShell DBA posts you can see some of my daily checklist, and, more importantly, how PowerShell can help reduce the overhead of managing that checklist Please feel free to download my sample scripts from the top of this article and experiment with them. In the third and final part of these posts, we will see some more set ups we can do, like changing collation columns and rebuilding indexes. As always: don’t forget, you are an Exceptional DBA who loves PowerShell!