Tag: powershell

  • Powershell in a month – Day 1

    I got started on Learn Windows Powershell 3 in a Month of Lunches at lunch one day. Working at home makes this easy as I have a computer and a kitchen near each other. In this case, a salad, a bowl of chili, and the laptop next to the book to get started. The picture below was post-chili.

    20131106_114540

    This is the first post in my Powershell challenge. I don’t expect to post every day as I travel, have vacation, etc, but I will post once for each of the days I work on this. At the end, I hope to have a couple PoSh presentations I’ll build as well as more incorporation of PoSh into my talks.

    Day 1

    I cheated slightly today. I read the first and second chapters here. The first was really a “how to use this book” and the second actually got me started. The first introduces what Powershell is, why it’s good, etc. It’s a good intro, but I’ve been through most of this before, I know something about the environment, and I’ve used it a bit for specific tasks. If you are new to PoSh, read this.

    I didn’t get much out of it, so I went into Chapter 2, despite the warning from the book not to do more than one a day. Instead, practice. I made an exception this first day.

    The second chapter gets you familiar with how to set up your environment. As it mentioned, PoSh gives you two ways to work with it on most modern OSes. The command line or the Integrated Scripting Environment (ISE). I tend to use the ISE, but you can choose. In this case, I popped both open. One note here, I’m on Win 8 and if I hit the Windows key and type “power”, I see this:

    Posh_001

     

    There’s no ISE available. This is different on different machines for me. However if I finish “Powershell” and add a space “i”, I get the ISE. Not a bid deal, but an interesting item for me.

    Posh_002

    The second chapter really goes into getting the environment set up first. Choose large fonts, which is a must for older folks like me. I definitely plan to make this a part of any presentations I do. The chapter also emphasizes that typing matters and accuracy is important. PoSh is case sensitive and different separators, like the various grouping items, matter. These:

    ([{<

    are all different and you can’t mix and match them. Make sure you know the difference. It’s especially try for the single quote (‘) and the backtick (`). Learn the difference.

    No scrolling was another important point since you can lose things on the screen. For me, I set the font to 18, left the default colors, and then in the ISE, removed the command window and script editor. That left me full screen with the interactive shell.

    Posh_003

     

    From there, I played a bit with the way PoSh works. Typing part of commands, and then using Tab or Shift+Tab to cycle through commands. Using these keys to get folders, or parameters, or choices, was handy.

    Nothing really executed today, but I did play with various commands to see what happened. The one thing I did run was

    Get-Service SQLAGENT

    I typed Get-s, hit tab to cycle through choices, and then entered “SQ” and tab’ed to get SQLAgent. That gave me the status of the service on my laptop.

    Posh_004

     

    That was the basic  learning for today. It’s simple, and a little boring, but it is stuff to know that will make future days easier and build some familiarity with the environments.

    Tomorrow, chapter 3.

     

  • The Powershell Challenge

    indexI’ve been wanting to do more with Powershell (PoSh). I’ve seen Allen White present on it for years, and I’ve dabbled in various ways, using it to handle small tasks. However I haven’t really committed to learning a bunch of PoSh.

    Last month I was with Grant Fritchey on the 2013 SQL in the City tour and in one of his sessions, he uses some Powershell to audit his SQL Server instance. I thought that was pretty cool, and it made me want to dig in further. At the end of his talk, he mentioned a book, and I decided to grab it.

    It’s Learn Windows Powershell 3 in a Month of Lunches from Don Jones. The name alone made me want to grab it, and it has a catchy title.

    My plan is to take time every day or two and read a chapter and work through exercises in the book. I really want to get more comfort in PoSh because I do believe in it and I think it’s a fantastic way to get things done. I don’t work at scale, but I suspect with a little practice I’ll be using PoSh more often for simple things and saving those scripts.

    I’ll blog along the way and see how things are going. Once I get through, I expect that I’ll be digging into more with other articles and books that deal specifically with SQL Server.

    The days in the Challenge:

  • Scripting

    One of the directions in which Red Gate Software is to build more tools and utilities that all of you can use for working in cloud environments. We aren’t ignoring the desktop tools like SQL Compare and Prompt (v6, yeah!) that you’ve come to appreciate and depend on, but we are investing in expanding these tools to work with a remote, services style architecture since there are plenty of companies looking at AWSAzure, or other providers for a portion of their applications.

    I’ve been learning a bit more about the way cloud services work and one of the things that becomes important in an IaaS environment is scripting. The environment you get is a base install and if you need more software or processes to run when a new VM starts up, you need to script the various installations or configurations.

    There are some great scripting features for SQL Server, like Finebuild for installs and Powershell for most everything else. Scripting is a great way to accomplish tasks, but like most skills, it needs to be used in order to be efficient and practical for most tasks. I’ve struggled with this, and I’m sure many others feel the same way.

    However I am starting to think that Powershell might be the best investment in my future skills. The quicker I can write it and build scripts to accomplish tasks, the more I’ll be able to do over time as I build a library of commandlets and scripts. That’s if I remember to document them well so I can easily search for what I need.

  • T-SQL Tuesday #39 – PowerShell

    tsqltuesdayThis month the invitation is from Wayne Sheffield, asking if you have a PowerShell story for everyone. That’s fitting since Wayne is currently writing a month of PowerShell blogs, and they are worth reading.

    The second Tuesday of each month is T-SQL Tuesday, with a new theme each month. If you follow the community on Twitter, you should get an invite every month, and I also keep a history page on my blog that I try to update.

    PowerShell

    I believe in PowerShell. It’s one of the few directives that Microsoft has taken in their products that I think is both a great idea, and one that will last for a long time. As far as I’ve heard, all their future products in the server space need to support Powershell and include hooks to incorporate it into the administration of those products.

    That being said, I don’t use PowerShell (PoSH) a lot. Scripting tasks is good for repetitive actions. However since I work at home and manage a couple instances, with disparate requirements, the time to write a PoSH script seems to outweigh the time it takes just performing a few tasks.

    I’m always looking for places that I can use PowerShell, and where it makes sense for me and I found one last year.

    Binary Data in SQL Server

    I wrote a presentation last year that looked at binary (BLOB) data in SQL Server, and worked with the FileStream/Filetable features. In building a demo, I wanted to show that binary documents in SQL Server work well with Filestream and that you can query the binary fields just like any other. However to render something like an image, you really need to stream the data into an application, or a file.

    I chose a file, and built a short PoSH script to do just that. Here’s the script:

    .csharpcode, .csharpcode pre
    {
    font-size: small;
    color: black;
    font-family: consolas, “Courier New”, courier, monospace;
    background-color: #ffffff;
    /*white-space: pre;*/
    }
    .csharpcode pre { margin: 0em; }
    .csharpcode .rem { color: #008000; }
    .csharpcode .kwrd { color: #0000ff; }
    .csharpcode .str { color: #006080; }
    .csharpcode .op { color: #0000c0; }
    .csharpcode .preproc { color: #cc6633; }
    .csharpcode .asp { background-color: #ffff00; }
    .csharpcode .html { color: #800000; }
    .csharpcode .attr { color: #ff0000; }
    .csharpcode .alt
    {
    background-color: #f4f4f4;
    width: 100%;
    margin: 0em;
    }
    .csharpcode .lnum { color: #606060; }

    $server = "SevenFalls"
    $database = "AdventureWorks2008"
    $query = "SELECT TOP 10 Document, FileName +'.' +  FileExtension FROM Production.Document WHERE Document IS NOT NULL"
    $dirPath = "C:\Users\Steve\Documents\Presentations\UnstructuredData\Docs\"
     
    $connection=new-object System.Data.SqlClient.SQLConnection
    $connection.ConnectionString="Server={0};Database={1};Integrated Security=True" -f $server,$database
    $command=new-object system.Data.SqlClient.SqlCommand($query,$connection)
    $command.CommandTimeout=120
    $connection.Open()
    $reader = $command.ExecuteReader()
    while ($reader.Read())
    {
        $sqlBytes = $reader.GetSqlBytes(0)
        $filepath = "$dirPath{0}" -f $reader.GetValue(1)
        $buffer = new-object byte[] -ArgumentList $reader.GetBytes(0,0,$null,0,$sqlBytes.Length)
        $reader.GetBytes(0,0,$buffer,0,$buffer.Length)
        $fs = new-object System.IO.FileStream($filePath,[System.IO.FileMode]'Create',[System.IO.FileAccess]'Write')
        $fs.Write($buffer, 0, $buffer.Length)
        $fs.Close()
    }
    $reader.Close()
    $connection.Close()

     

    The script works by essentially setting variables to a server, database, and a query. In this case I use the AdventureWorks database from 2008 that includes FileStream data. I then open a connection to the instance, run the command and look through the results, streaming out the data in the “Document” field to a file which has the name from the FileName+FileExtension fields.

    If you run this on your system, assuming fix the file paths, you’ll get 9 new documents in the file system, each one a document from the AdventureWorks sample database.

    That’s the best example of where I’ve used PowerShell in my daily work, but I know if I had to manage multiple instances, I’d be using this tool all the time.