Category: SQL Server

PowerShell One-Liner to Disable Active Directory Accounts and Log the Results to a SQL Server Database

The new PowerShell cmdlets that are part of the SQLServer PowerShell module that's distributed as part of SSMS (SQL Server Management Studio) 2016 make it super easy to write the output of PowerShell commands to a SQL Server database. The ActiveDirectory PowerShell module that's part of the RSAT (Remote Server Administration Tools) is also required by the code shown in this blog article. This PowerShell one-liner retrieves a list of Active Directory users who have not logged in within the past Read more [...]

Write a GUI on Top of Existing PowerShell Functions with SAPIEN PowerShell Studio 2016

This blog article will demonstrate how to write a GUI on top of your existing PowerShell functions using SAPIEN PowerShell Studio 2016. I've previously written a couple of functions for managing SQL Server agent jobs. These two functions, Get-MrSqlAgentJobStatus and Start-MrSqlAgentJob can be found in my SQL repository on GitHub. Launch PowerShell Studio. Select file, the arrow next to new, and new form: For this particular GUI, I'll select the "Dialog Style Template" since I want a fixed Read more [...]

PowerShell Function to Check the Status of a SQL Agent Job using the .NET Framework

My previous blog article demonstrated how to start a SQL agent job using the .NET Framework from PowerShell to eliminate the dependency of needing the SQL Server PowerShell module or snap-in on the machine where the command is being run from. There's not much use of blindly starting a SQL agent job without being able to check the status of it so I decided to write another function to accomplish that task. If the machine where the code is being run from has the SQL Server PowerShell module that Read more [...]

Start a SQL Agent Job with the .NET Framework from PowerShell

As of this writing, the most recent version of the SQLServer PowerShell module (which installs as part of SQL Server Management Studio) includes cmdlets for retrieving information about SQL agent jobs, but no cmdlets for starting them. I recently ran into a situation where I needed to start a SQL agent job from PowerShell. The solution needed to be a tool that others could use who may or may not have the SQLServer module, SQLPS module or older SQL Server snap-in Read more [...]

Video: Automate Operational Readiness and Validation Testing of SQL Server with PowerShell and Pester

I recently presented a session on "Automate Operational Readiness and Validation Testing of SQL Server with PowerShell and Pester" for the PowerShell Virtual Chapter of SQL PASS. The video from that presentation is now available: https://www.youtube.com/watch?v=Ds1YO_Dpx0I Both the code and slide deck from the presentation can be found in my presentations repository on GitHub. µ Read more [...]

Store and Retrieve PowerShell Hash Tables in a SQL Server Database with Write-SqlTableData and Read-SqlTableData

In my blog article from last week, I demonstrated using several older open source PowerShell functions to store the environmental portion of the code from operational validation tests in a SQL Server database and then later retrieve it and re-hydrate it back into a PowerShell hash table. Earlier this week, a new release of the SQLServer PowerShell module was released as part of SSMS (SQL Server Management Studio): It includes three new cmdlets, two of which can be used to store and retrieve Read more [...]

Video: Building Unconventional SQL Server Tools in PowerShell

Last week, on Wednesday (April 6th, 2016), I presented a session at the PowerShell and DevOps Global Summit 2016 on "Building Unconventional SQL Server Tools in PowerShell with Functions and Script Modules". The video from that presentation is now available: https://youtu.be/2rEzMWdTFDk Here's the abstract or synopsis for this presentation: "Have you ever had records from a SQL Server database table come up missing? Maybe someone or some process deleted them, but who really knows what happened Read more [...]

Converting a SQL Server Log Sequence Number with PowerShell

As demonstrated in one of my previous blog articles "Determine who deleted SQL Server database records by querying the transaction log with PowerShell", someone or something has deleted records from a SQL Server database. You've used my Find-MrSqlDatabaseChange function to determine when the delete operation occurred based on information contained in either transaction log backups or the active transaction log: You're ready to perform point in time recovery Read more [...]

Code and Slides from my sessions at SQL Saturday 439

Today I presented two wildly successful sessions at SQL Saturday #439 in Columbus, GA. The code and slides from my "How do I Get-Started with PowerShell?" session can be downloaded here and the code and slides from my "PS C:\> Get-Started -With PowerShell Desired State Configuration" session can be downloaded here. The custom functions that are referenced in the code from the DSC session can be downloaded as part of my MrDSC module from GitHub. µ Read more [...]

Presentation Materials from my sessions at SQL Saturday 423 in Baton Rouge

This past Saturday, I presented two sessions at SQL Saturday #423 in Baton Rouge. The code and slides that I used during my "PS C:\> Get-Started -With 'PowerShell for SQL Server'" presentation can be downloaded from here and the code and slides from my "PS C:\> Get-Started -With 'PowerShell Desired State Configuration'" presentation can be downloaded from here. The code from both sessions reference PowerShell functions that are part of script modules that can be downloaded from my Read more [...]

Determine who deleted SQL Server database records by querying the transaction log with PowerShell

Have you ever had a scenario where records from one or more SQL server database tables mysteriously came up missing and no one owned up to deleting them? Maybe it was an honest mistake or maybe a scheduled job deleted them. How do you figure out what happened without spending thousands of dollars on a third party product? You need to determine what happened so this doesn't occur again, but the immediate crisis is to get those records back from a restored copy of the database. How do you determine Read more [...]

Query SQL Server from PowerShell without the SQL module or snapin

There are several different ways to query a SQL Server from PowerShell but most often you'll find that they're dependent on the SQL PowerShell module or snapin. To eliminate that dependency, you can query a SQL Server from PowerShell using the .NET framework. There are several different options to accomplish this including using a DataReader or a DataSet and there are plenty of tutorials on those topics already on the Internet so there's no reason to duplicate that information here. Most of the Read more [...]

Function to Import the SQLPS PowerShell Module or Snap-in

The SQL Server 2014 basic management tools have been installed on the Windows 8.1 workstation that's being used in this blog article. When attempting to import the SQLPS (SQL Server PowerShell) module on your workstation, you'll be unable to import it and you'll receive the following error message if the PowerShell script execution policy is set to the default of restricted: Import-Module : File C:\Program Files (x86)\Microsoft SQL Server\120\Tools\PowerShell\Modules\SQLPS\Sqlps.ps1 Read more [...]