Tag: powershell

  • Powershell in a Month – Day 6 The Pipeline

    This is part of my Powershell Challenge, to learn more about PowerShell (PoSh) using the Learn Windows Powershell 3 in a Month of Lunches book by Don Jones.

    Day 6. This is where I found the idea of Powershell getting interested. I used pipes extensively back in my Unix and DOS days, and have heard that these are important in powershell.

    The pipeline (|) essentially takes the output of the left command and sends it to the right. The first examples are with exporting data, such as:

    Get-Service | Export-CSV services.csv

    This command would run the Get-Service command to get a list of services, then send that output to the Export-CSV command, which would create a file, format the data in CSV format, and send all the data from the Get-Service command into the file. Simple, easy and it makes sense. There are also Export-CliXML, -FormatData, etc. commands. On my system I even have an Export-VM  command for Hyper-V. I can start to see possibilities.

    This is different from the redirector (>), which is essentially running the pipe to Out-File, a command to create a file. There are other Out-* commands that send data other places, like printers, the screen, etc. Why does it matter that you have these commands? Well, mainly because you might want to alter some parameters, like display width, encoding, etc. Having a real command build allows for parameters, and control, and thus, customization.

    Powershell is all about customization, and being able to repeat that process. This chapter gives the basics of controlling your flow out output, including to other commands. For example, one of the things the chapter has you do is

    Get-Process -name Notepad | Stop-Process

    A fancy way to kill a process. It does warn you not to forget the -name parameter, as that would be bad. I was tempted to try it on a VM, but I know what will happen, so I didn’t bother. For commands that might cause problems, the chapter goes over the -confirm and -whatif parameters, which, respectively, ask you questions to confirm your actions, and show you the action without performing it. The lab for the chapter has you dig into other parameters, like appending to files (noclobber) using the default regional settings (useculture) and changing the delimiter for export. I actually found these lab exercises to be slightly challenging, but also handy. I got to use the help system and proved to myself a bit that I’m learning how to use PoSh.

    Overall, no great leaps in knowledge here, but I find myself gaining some confidence that I understand how PoSh works, how to manipulate the environment a bit, and I can see the possibilities of using the pipeline to string commands together and manage more than a simple stream of output data.

    The next chapter looks at more involved strings of commands, and I’m looking forward to that one.

  • Powershell in a Month– Day 4 and 5 – Providers

    This is part of my Powershell Challenge, to learn more about PowerShell (PoSh) using the Learn Windows Powershell 3 in a Month of Lunches book by Don Jones.

    Day 4, and 5. This wasn’t a long chapter, but it came right around travel and vacation, and as a result, I ended up taking about 2 hours across multiple days. Mostly because I had to re-read things and re-type them. As a result, I’m marking this as two days. Plus, this will now line up the chapters.

    Providers are the “drivers”, I guess for Powershell. They allow you to interact with various systems through the Powershell commands. Another way of putting it is that they take some item and make it look like a file system. This means you can navigate the registry (with the registry provider) like your disk with CD, DIR, etc. It’s also why you can move through SQL Server in a folder-like hierarchy.

    This was a short chapter, and I’m not sure why it took so long, but I moved slow going through the exercises to creates files and folders, learning to use the New-Item or Set-Location commands instead of the mkdir or cd ones.  I did find this interesting.

    The lab exercises were easy, and once I could focus, I got through them quickly.

    The next chapter is pipelines, which is where I think the power of PowerShell starts to come through.

  • Powershell in a Month – Day 3 – Commands

    This is part of my Powershell Challenge, to learn more about Powerhsell (PoSh) using the Learn Windows Powershell 3 in a Month of Lunches book by Don Jones.

    Day 3, chapter 4.

    This one is about commands, and getting familiar with what a cmdlet is, the naming convention, and how to call things. Again, simple stuff, but knowing that most everything is a Get-xx, or Set-xxx, or New-xxx, etc. is handy. I think the hyphen separation actually makes things easy, though the completion in the ISE and with tab is very handy.

    The big thing here is the parameter naming and cmd.exe execution. I wasn’t always clear on how parameters work, but this chapter helped. In this case, unless there’s punctuation or spaces, you don’t need quotes. That’s handy. I also like that the shell is explained in reference to cmd.exe, which is the command prompt that so many of us use.

    This was a short chapter, a quick skim, and I made sure that as the authors talked about commands, aliases, etc, I ran all the code. When I got to the end of the chapter, I was able to actually solve all the exercises without using any references. I used help to look up commands (like Get-NetFirewallRule), but otherwise, I was able to solve all the exercises in about 5 minutes.

    I felt successful and more empowered today. It’s getting a little exciting, and I can see where this will be very helpful over time.

  • Powershell in a Month – Day 2, Help

    This is part of my Powershell Challenge, to learn more about Powerhsell (PoSh) using the Learn Windows Powershell 3 in a Month of Lunches book by Don Jones.

    Today was Chapter 3, Using the Help System. Another not-so-exciting topic, but one that does matter. There are a ton of commands to learn and use in PoSh and being able to get help on them is important if you are to develop code with any level of speed. Note I said code. This is scripting, it’s interactive, but it’s code. Whether you run it once or save it and schedule executions every minute, it’s code.

    The first point in the chapter is to RTFM, or “read the friendly manual”. The authors emphasize that using Google instead of help means you’re copying code, not learning about the code, and potentially stunting your development or even wrecking your production system with commands you don’t understand. As much as I love Google, I can understand the point here.

    One of the first things the chapter does is ask you to update help. However that didn’t work for me:

    Posh_006

    The book doesn’t talk about the CLI v the ISE, which is the problem. I found a post on MSDN that noted this works from Powershell, but not the Powershell ISE.  Once I went to the CLI, it worked.

    Posh_005

    I used to do UNIX work, and was forever typing “Man xxx” to learn about xxx. MAN is a wrapper here, as is “help” for the get-help command. I played around with some get-help stuff, using wildcards and tab completion. Both of which are handy. I can definitely see that learning to use help and tab is important.

    The rest of the chapter brings out various ways in which you can use help, including getting more details, getting examples, and going to the online pages, which are more up to date than your local copy might be. All good tricks to getting going with PoSh. Lots of example commands showing how to get help, and I ran most of these.

    The chapter was boring, but at the end when I got to the lab, I was surprised how little I know. As I tried to go through the lab questions, I wasn’t sure what was working and what wasn’t. I didn’t know if I was actually running the right commands, since things like “help *array*” worked. There are answers you can download from the morelunches.com site, and once I did that, I was able to check my results, and get help on a few items I wasn’t sure about. 

    I was surprised that I learned a few things, and I can definitely see that working with help is both important, and cumbersome. For example, the scrolling isn’t smooth in the ISE and the MORE piping from the HELP wrapper doesn’t work. Definitely something to practice on as I move through the book.