Tag: SQLNewBlogger

  • Using Out-GridView To Pick Parameters–#SQLNewBlogger

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

    I was editing an article on PoSh and learned something cool. I can use Grid-View to surface an object in a pipeline and then change the items passed through to the next command.

    Here’s how it works. Suppose I have a simple comment, like Get-SqlDatabase. I can run this and get a series of database. (command and output shown here).

    Get-SqlDatabase –ServerInstance .\SQL2014

    2017-01-03 12_50_20-powershell

    I get a bunch of data, but I don’t want all the items. For example, maybe I just want the “SimpleTalk” databases.

    Get-SqlDatabase -ServerInstance .\SQL2014 | Where-Object { $_.Name -like ‘SimpleTalk*’}

    2017-01-03 12_51_42-powershell

    I don’t want to edit this Where-Object clause all the time. I want something simple to easily fix this.

    Enter Out-GridView

    Instead of changing my filter, I can do this graphically. I can pipe my output to Out-Gridview, and I’ll get this.

    2017-01-03 12_54_44-Get-SqlDatabase -ServerInstance ._SQL2014 _ Out-GridView

    All of my output data appears. No big deal, right? Is this useful? It sure is. Look at the filter item above. I can type in there, and my data is filtered.

    2017-01-03 12_55_29-Get-SqlDatabase -ServerInstance ._SQL2014 _ Out-GridView

    How can I use this? Suppose my code is this:

    PS SQLSERVER:\SQL\Plato\SQL2016\Databases> Get-SqlDatabase -ServerInstance .\SQL2014 | Out-GridView -PassThru | Backup-SqlDatabase -CompressionOption Default –Script

    Here I’m getting a list of databases, passing them to Out-GridView, and then sending the results to Backup-SqlDatabase, which will return a script to back up all my databases.

    I’ll run this, and then enter a filter in the grid.

    2017-01-03 12_57_47-Get-SqlDatabase -ServerInstance ._SQL2014 _ Out-GridView -PassThru _ Backup-SqlD

    I can now highlight all these rows (if that’s what I want).

    2017-01-03 12_59_11-Get-SqlDatabase -ServerInstance ._SQL2014 _ Out-GridView -PassThru _ Backup-SqlD

    This time I have an “OK” button in the lower right corner of the grid.

    2017-01-03 12_58_23-Movies & TV

    When I click “OK”, my filtered list is returned to the pipeline and send to the backup command.

    2017-01-03 12_59_32-powershell

    This is a quick way to work with the parameters in your pipeline in an ad hoc way. Use Out-GridView to filter and select the rows you want to return to the rest of your script.

  • Do I have a Database Master Key in a database? #SQLNewBlogger

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

    How can I tell if I have a Database Master Key in a database? It’s actually easy. I query the sys.symmetric_keys DMV for data. If I get a result that has a name of ##MS_DatabaseMasterKey##, then I have a database master key.

    You can see this below. I’ve queried this DMV in my Sandbox database.

    2017-01-11 12_21_55-SQLQuery1.sql - (local)_SQL2014.Sandbox (PLATO_Steve (59))_ - Microsoft SQL Serv

    Now, what if I go to another database, say the Finances database. I see nothing.

    2017-01-11 12_22_34-SQLQuery1.sql - (local)_SQL2014.Finances (PLATO_Steve (59))_ - Microsoft SQL Ser

    Let’s add a master key here and then query. Note, I am not disclosing the real password here. Never do this, even in test systems.

    2017-01-11 12_23_14-SQLQuery1.sql - (local)_SQL2014.Finances (PLATO_Steve (59))_ - Microsoft SQL Ser

    This instance has been used with TDE, so if I go to master, I’ll get this:

    2017-01-11 12_24_20-SQLQuery1.sql - (local)_SQL2014.master (PLATO_Steve (59))_ - Microsoft SQL Serve

    You can see that I not only have a DMK, I have a Service Master Key (SMK), which protects the instance.

    When I create my DMK, the only parameter I can provide is a password, after the optional “ENCRYPTION BY PASSWORD” keywords. I don’t name it, so I can count on the naming being fairly consistent. I don’t think that the name would change from version to version, but it could.

    I’d prefer that MS not create magic numbers or names, and instead, add a column to the DMV that denotes this is a DMK.

    References

    sys.symmetric_keys – https://msdn.microsoft.com/en-us/library/ms189446.aspx

    CREATE MASTER KEY – https://msdn.microsoft.com/en-us/library/ms174382.aspx

    Connect Item to add a flag – https://connect.microsoft.com/SQLServer/feedback/details/3118588

  • Getting Started Blogging–#SQLNewBlogger

    It’s a new year, and the perfect time for you to start blogging about your SQL Server work. I think blogging is a great way to showcase your knowledge, and over time, this can become as important to your potential employers and clients as your resume or CV.

    This post won’t look at what to write, but rather, how to get started. I’ve divided this up for people that might have written some technical articles and those that haven’t. If you have, feel free to skip the next section.

    Starting From Scratch

    If you’ve never really written a blog or technical piece, I recommend do this:

    1. Create a folder on a shared drive
    2. Open Word (or another WYSIWYG) editor
    3. Begin Writing
    4. Save the post you’ve worked on as 1_SomeTopic.
    5. Repeat, incrementing the number so you get files such as 2_OtherTopic, 3_ReallyInteresting, etc.

    That’s it. Just begin writing. Don’t worry about publishing, don’t worry about anything else. Just get in the habit of putting some thoughts down on paper that talk about the work you do.

    When you get 10 posts, you are ready to move on.

    I would recommend you calculate the average time it takes for you to produce a post. This would be the rate at which I’d look to schedule my posts.

    Choosing a Platform

    The next step when you are comfortable with your writing is to choose a place to publish your work. There are lots of choices, and a good comparison of sites is listed in this article.

    I use WordPress and Blogger/Blogspot for different blogs. I chose two to have a comparison and after a few years, I think WordPress is better. You can certainly download the WordPress software and set up your own site on a VM somewhere, but I don’t think that’s a good place to start unless you want a project. If you want to write, wordpress.com is a nice start. It does enough for me, and it’s easy to let them update the software and just pick a theme.

    I also recommend OpenLiveWriter to write with. I can easily draft posts in a WYSIWYG way, keep the drafts private (and on a OneDrive folder) and then publish to my platform at will. I find this easier than trying to work online. The project looks slightly abandoned for now, but it is stable and useful on the Windows platform.

    Scheduling

    How often should you blog? I think this is a hard question, but I’d blog at the pace that works for me. If you are a new writer, you should have written 10 pieces and tracked the time to produce those. For most people, this is between 2 and 4 weeks for a piece. Some might do 1 a week, but whatever is possible in your busy life is the pace I’d stick with.

    My goal is to blog 3 times a week. Sometimes I can do more, but I don’t usually try to do more. It’s better to schedule out posts and ensure I can maintain some level of consistency than I get my posts out right away. Most of the time I have a couple of posts scheduled a few weeks out because I’m not producing news. I’m showcasing knowledge.

    My advice is to schedule less frequently than you think you should. It’s easier to add in most posts later than try to maintain some pace that causes you stress.

    This should be fun.

    Post Reviews

    No matter what topics you choose or the frequency of your posts, it is important that you do a good job in producing them. That doesn’t mean you need 3 peer reviews and a copy edit, but you should take the time to get some feedback from others on your work.

    If you aren’t an experienced writer, or you worry about the impression your writing makes, then ask a friend, spouse, co-worker, etc. to review a few posts and help you with your grammar, spelling, and the way you communicate the concepts.

    Communication is a skill, and you will get better if you work at it. However, the best way I have found to do this quickly is to get feedback from others.

    Take the Challenge

    Challenge yourself and start blogging today. Even as little as 15 minutes a week can really help you showcase your knowledge, and give you an edge for your next interview.

  • Checking Permissions for Keys–#SQLNewBlogger

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

    I got a call from someone wanted to check how permissions were stored for encryption objects. I ran a quick double check for them and decided to write this short post.

    Let’s say that you create a few encryption keys. In my case, I’ll use this code to create a symmetric key, an asymmetric key, and a certificate.

    CREATE SYMMETRIC KEY MySalaryProtector
    WITH ALGORITHM = AES_256,
        IDENTITY_VALUE = 'Salary Protection Key',
        KEY_SOURCE = N'Keep this phrase a secr#t'
    ENCRYPTION BY PASSWORD='Us#aStrongP2ssword';
    GO
    
    CREATE ASYMMETRIC KEY HRProtection
    WITH ALGORITHM = RSA_2048
    ENCRYPTION BY PASSWORD = 'Use4SomeStr0ngP@ssword%^';
    
    GO
    
    CREATE CERTIFICATE MySalaryCert
    ENCRYPTION BY PASSWORD = N'UCan!tBreakThis1'
    WITH SUBJECT = 'Sammamish Shipping Records',
        EXPIRY_DATE = '20161231';
    GO

    I do this, I have these objects.

    2016-11-29 14_21_19-SQLQuery11.sql - 192.168.1.204_SQL2016.EncryptionDemo (sa (57))_ - Microsoft SQL

    Let’s now grant rights to these objects. I’ll use this code to grant CONTROL to a user.

    GRANT CONTROL ON SYMMETRIC KEY::MySalaryProtector TO JoeDBA
    
    GRANT CONTROL ON ASYMMETRIC KEY::hrprotection TO JoeDBA
    
    GRANT CONTROL ON CERTIFICATE::MySalaryCert TO JoeDBA

    Once I do this, I should see permissions, right? Let’s check.

    2016-11-29 14_25_30-Database User - JoeDBA

    I don’t see any permissions in the dialog above. That’s not exactly what I’d want to see. After all, if I’m trying to determine why a user can’t access a certificate, I’d want to know if they had rights here.

    Instead of this, I need to use T-SQL, and check for specific classes in sys.database_permissions. Here’s the query looking for class 24 (symmetric keys), 25 (certificates) and 26 (asymmetric keys).

    2016-11-29 14_27_57-SQLQuery11.sql - 192.168.1.204_SQL2016.EncryptionDemo (sa (57))_ - Microsoft SQL

    You can see that I have permissions in here, and if I check the principal_id, I’ll find these are for my user. I could also join to database_principals and get specific information for my user.

    2016-11-29 14_30_41-SQLQuery11.sql - 192.168.1.204_SQL2016.EncryptionDemo (sa (57))_ - Microsoft SQL

    #SQLNewBlogger

    This took a bit longer as someone asked me a question and I didn’t know the answer. I had to dig and read some documentation, but I found some answers and documented things myself.

    Learned something, showed it, and hopefully will remember it from now on.