The debug database switch snippet

Not long into this whole project it became clear that while I was making changes to my PowerShell inventory scripts and updating the database, there were also people who were needing to look at the data while I was making changes.  This meant that I needed a dev playground that mirrored the current SQL database.  So I made a duplicate of Inventory named Inventory_Dev and insert this snippet into any code that works with the database:

#$Debug = $true
$SQLServer = "CACRPDFS01"
If ($Debug) {$SQLDatabase = "Inventory_Dev"}
Else {$SQLDatabase = "Inventory"}
Invoke-Sqlcmd -Query "DELETE FROM [$SQLDatabase].[dbo].[TableName]" -ServerInstance $SQLServer -Database $SQLDatabase

After that I only need to comment out the $Debug = $true line when I’m done making changes and then all the code through-out the script switches databases back to prod.

This entry was posted in Programming and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s