Author: way0utwest

  • PowerShell–Don’t Use Write-Host

    I’ve written a few scripts and programs lately, mostly just for fun. In those scripts, I’ve used Write-Host to return output. To me, it’s been like “Print” in various languages where I can get output of a program. Often I’ll use a method/function to get info and then use print to output that to the caller.

    However a few people noted that in my last script, Write-host wasn’t necessary. When I asked why, both Mike Fal and Drew Furgiule responded. I got these two items:

    I learned something new today. I had assumed that I’d need a way to get the output to the screen and manage output with my own logic. I’ve had this before

    $debug = 1

    if $debug  = 1 { write-host $somevariable}

    However a quick check shows this isn’t really what I want. Instead, I’d use Write-Debug or Write-Verbose. A quick test shows I can do this:

    2016-03-02 12_10_16-Windows PowerShell ISE

    That’s much better than passing in a debug parameter or changing a value as I run scripts.

    Looks like I have some refactoring to do.

  • Open is Not Necessarily More Secure

    Researchers found a bug in glibc. This library has been around for a long time, used in lots, and lots of software. I’m sure there are some programmers unaware that they coded in software that itself used glibc. However, despite all the people that must have looked at this library over the years, no one noticed the bug and disclosed it.

    The maintainers missed the bug, though they took quite some time to patch code once they were aware. That’s disconcerting, especially because many of the routers we use are potentially vulnerable. What’s worse is that many of the consumer based router firmware likely won’t be upgraded as companies would prefer you to purchase a new router rather than actually patch their existing products.

    I wouldn’t be surprised if someone found the bug and didn’t notify anyone publicly. Perhaps they decided to use this bug to attack software with some criminal purpose in mind. What might be scary is that I am sure that criminal organizations, or even various governments, might actively look for issues such as these and take advantage of them.

    I’m glad the issue was found and it can be patched. Certainly having code open for viewing means that researchers and organizations can examine the code, looking for issues. However the fact that software is open doesn’t mean it is more secure. Security depends on careful examination of all code, which may or may not be the case for plenty of FOSS software. I’m not implying FOSS is better or worse than closed source, vendor written software. Just be aware that you can’t assume it’s more secure.

    Steve Jones

    The Voice of the DBA Podcast

    Listen to the MP3 Audio ( 2.8MB) podcast or subscribe to the feed at iTunes and LibSyn.

  • Quick Folder Size with PowerShell

    This is a fairly simple idea, and one I’m sure many people have done in the past. Personally, I have tended to just hover a mouse over a folder when I want a size. That seems to work fairly well, but not only is it slow, it’s not programmatic.

    2016-02-25 10_24_01-Settings

    I saw Jose Barreto write a quick OneDrive size script in PowerShell (PoSh), and thought it was interesting. It didn’t work for me as I’ve moved the OneDrive folder to my D: drive, so I had to alter this to get information. However in doing so, I decided to play with a function.

    Here’s the code I used, altering Jose’s slightly.

    UPDATE: Thanks to Mike Fal, one of my go-to PoSh experts, I’ve removed the aliases for Dir and %.

    function Get-FolderInfo($folder) {
    $OneDrives = $folder
    Get-ChildItem $OneDrives | ForEach-Object {
    $Files=0
    $Bytes=0
    $OneDrive = $_
    Get-ChildItem $OneDrive -Recurse -File -Force | ForEach-Object {
    $Files++
    $Bytes += $_.Length
    }
    $Folders = (Get-ChildItem $OneDrive -Recurse -Directory -Force).Count
    $GB = [System.Math]::Round($Bytes/1GB,2)
    Write-Host “Folder ‘$OneDrive’ has $Folders folders, $Files files, $Bytes bytes ($GB GB)”
    }
    }

    Note that I’ve passed in the folder name and then kept Jose’s code. This worked fine for me, as you can see below.

    2016-02-25 10_24_11-Windows PowerShell ISE

    The next step for me was to make this programmatic and useful. All that text isn’t helpful. What I really want is just a size. I guess I need a folder name as well, so I built a function to return that information. I merely changed the last line to:

      Write-Host “$GB”

    I also moved this to the end of the function, rather than for each subfolder. With this change, I can now call this for a folder and get the size in GB returned.

    2016-02-25 10_31_22-Windows PowerShell ISE

    And this checks out from Windows

    2016-02-25 10_31_16-Settings

    I know there are better ways to write this function, but this was more of a programming exercise. This was really a 10 minute chance to practice and experiment a bit with PoSh and work on skills.

    BTW, I used dot sourcing to load this as a function I could callIn love

    2016-02-25 10_33_42-Jump List for VMware Workstation Pro

  • Learning from Microsoft

    I know that many data professionals out there are skeptical of the cloud. Certainly there are problems with reliability, security, and more that prevent many of you from migrating to some online database. However that doesn’t mean that everything being done in platforms like the Azure SQL Database is a bad idea.

    One of the interesting things that has occurred at Microsoft is a completely new engineering effort for SQL Server. It seems there is one SQL Server development team that works on the Azure SQL Database. This is (I guess) the main branch in version control, and what we see with SQL Server 2016 is a subset of the code being built for the on-premises, boxed product.

    Certainly there are some features that exist in the box that aren’t in Azure, but relatively few of them. Those features, and many new ones, have to be included in the version control system, but they can’t be activated. Microsoft must be making extensive use of feature flags, allowing their developers to build and test features in the Azure cloud, but not make them available to customers, or potentially even other parts of Microsoft.

    If you really think about the effort to build a platform like Azure SQL Database and allow it to be a development environment as well as a production environment, that’s an amazing engineering effort. From the outside, I’m amazed at how far the SQL Server team has come in the last few years. I’ve seen a number of features released in the cloud first, and then appearing in the SQL Server 2016 CTPs.

    I think there are some amazing lessons here for software development. Certainly I think many of us could learn from how feature flags and multiple versions of software can co-exist in systems. That would certainly help many of us deploy new software in a way that minimizes interruptions to customers and clients. Or manage those multiple clients with different requirements from the same codebase.

    I hope that Microsoft will continue to evolve, as well as share their knowledge with the world. There are other companies that seem to be accomplishing amazing things at scale, Netflix, Amazon, Google, and more. Some of them share their techniques, but I truly hope that the methods they all use for building software become spread throughout the industry and help all of us build better, more secure applications.

    Steve Jones

    The Voice of the DBA Podcast

    Listen to the MP3 Audio ( 3.3MB) podcast or subscribe to the feed at iTunes and LibSyn.