Lessons Learned

My drop-site for interesting snippets and tips. If it's useful to you as well, great!

Posts
  • Lessons Learned

    Creating Local Users

    • 0 Comments
    I’m helping some of our test teams more and more.  In one case, we need to create local users in bulk. I’m surprised PSH doesn’t have something for this.  function New-LocalUser { <# .syopsis Creates a local user account .parameter...
  • Lessons Learned

    Querying WMI with a Timeout

    • 0 Comments
    This is thanks to my coworker Keith Munson, who is at least as passionate and adept at PSH as I am:   function Get-WmiObjectWithTimeout {     <#     Credit to Keith Munson for this.     #>  ...
  • Lessons Learned

    Storing SecureStrings Machine-Independently

    • 0 Comments
    As part of a brown bag, I extracted out the logic CredLocker uses to store credentials.  Here’s the guts of the code. The short form is unchanged from the Credlocker post, but I’ve cleaned it up. - It prompts the user for a password if $Host.CredentialStoreCredential...
  • Lessons Learned

    Comparing Performance Counters

    • 0 Comments
    A common TSG step is to look at some perf counter, wait n minutes, then look at it again. When we’re talking about pools, it gets a little more involved.  PerfMon.exe isn’t the most easy to automate (you can save the counterset to an .msc, then copy...
  • Lessons Learned

    One-Liner: Symlinks and Windows

    • 0 Comments
    Windows 7 and above support symbolic links, but the OS needs to be updated to recognize them: fsutil behavior set SymlinkEvaluation R2R:1
  • Lessons Learned

    One-Liner: Fun with Multi-line Blocktext and –Split/.Split()

    • 0 Comments
    The title is an oxymoron: I’m going to show a one-liner on how to deal with multi-line blocktext.  To start with, I’m using ‘blocktext’ to mean: $blocktext = @" This is an example using @" .. "@ "@; which is known...
  • Lessons Learned

    Getting SRV Records with PowerShell

    • 0 Comments
    PowerShell leverages .NET, and .NET exposes the System.Net.Dns class, so all should be happy, no?  Evidently not if it comes to SRV records.  Someone asked me how to do it, and I said, “That’s easy.  I’ll get back to you in a few.” A few...
  • Lessons Learned

    One-Liner: Valid PSH Verbs

    • 0 Comments
    I try to use proper PowerShell verbs, but I’m not always sure what they are (or what they do, which is another matter).  Here’s a quick one-liner to dump those verbs out as a [string[]]$array: Get-Command -CommandType cmdlet | Select-Object -ExpandProperty...
  • Lessons Learned

    MSSQL Server Error Logs

    • 0 Comments
    MSSQL Server error logs exist in the same folder that contains the installed server bits.  They’re text files.  Easy-as-pie to read, right? No. MSSQL is often installed on a dedicated drive.  You can’t assume it will be in C:\Program Files...
  • Lessons Learned

    One-Liner: Padding a Number with Leading Zeroes

    • 0 Comments
    I’m putting this here only because I’m tired of searching for it.  How to pad an integer with leading zeroes is trivial.  1..16 | % { "{0:D2}" -f $_ } And padding a float with trailing zeroes is more of the same: $i = 2; (1..10) |...
  • Lessons Learned

    Yet Another Password Generator

    • 0 Comments
    This must be freshman programming again.  I’m redoing CredLocker and want the way to parametrically generate passwords with minimum character set counts. This is also a good example of New-Object System.Random and [char[]]”Casting a string to char...
  • Lessons Learned

    Reinventing the Wheel: Get-WmiChildNamespace and Get-WmiChildPropertyData

    • 0 Comments
    I love and hate WMI.  I love how it offers the ability to access so many parts of the OS that PowerShell has not (yet) exposed via cmdlet.  I hate how the taxonomy is so deep, how you need to know what you’re looking for to find it. Also, I...
  • Lessons Learned

    Copy-History – Copy a Previous Command to the Clipboard

    • 0 Comments
    It’s great that I can see my history with Get-History, but what if I want to edit it?  I can hit up-arrow to find it, but that doesn’t work if I copy-and-paste functions into my PowerShell window as I write/test the function?  In that case,...
  • Lessons Learned

    Suspend-Computer (a.k.a. Sleep)

    • 0 Comments
    Shutdown.exe hibernates a computer, but there's no easy way to put a computer to sleep. One possible reason is that shutdown.exe’s /s, /l, and /e flags are already defined, and /p isn’t the most intuitive mnemonic for’ ‘sleep’...
  • Lessons Learned

    Windows Experience Index on 8.1

    • 0 Comments
    Firstly, thanks to http://www.intowindows.com/get-windows-experience-index-wei-score-in-windows-8-1/ for the underlying magic: WinSAT.exe. Under Windows 8.1, the Windows Experience Index is no longer exposed under Computer | System Properties.  That...
  • Lessons Learned

    HP Power Supplies

    • 0 Comments
    HP servers have redundant power supplies.  SCOM will tell me which one is broken if it’s down, but in this case, I lost power to one side of the rack, and want to see if replacing the distribution unit fixed the issue.  Here’s a quick-and-dirty...
  • Lessons Learned

    #StupidProfileTrick - FunctionList

    • 0 Comments
    As I previously posted, my $Profile is stupidly complicated, and it adds a ton of functions.  Which ones did I add? (Reason this is of interest to me is because I mistakenly named a function with the same name as a cmdlet.  Yeah, totally stupid...
  • Lessons Learned

    Something I Should Have Done a While Ago: $continue

    • 1 Comments
    $ErrorActionPreference is your friend.  However, ‘SilentlyContine’ isn’t, especially when it’s misspelled as is this case.  If we could Tab-complete it, that’d be great. Hey…. I’ve added this to my $PROFILE (actually my CommonFunctionLibrary...
  • Lessons Learned

    8.3, the Undead Path

    • 0 Comments
    8.3 is a common reference to the DOS name limit of 8 characters for the filename and 3 characters for the file extension.  E.g.: command.com. NTFS did away with that, but it still haunts us in the form of legacy .BAT files that can’t handle file...
  • Lessons Learned

    Write-Error, $Error, and Users

    • 0 Comments
    Write-Error is useful, but the output is intimidating for new users.  You want them to see a nice message, but they get a wall of red text including a stack dump. Here’s a way to output it as a warning, but populate $Error as well. function Out-Error...
  • Lessons Learned

    VM Host

    • 0 Comments
    Here’s the one-liner: reg query "HKLM\Software\Microsoft\Virtual Machine\Guest\Parameters" /v hostname Here’s the wrapper function Get-VmHostName { <# A fancy way to run remotely reg query "HKLM\Software\Microsoft\Virtual...
  • Lessons Learned

    Setting Cross-Forest Passwords

    • 0 Comments
    In my test labs, some of the more specialized testing labs (perf, specifically) have one-way trust with the larger test domain.  This means I can set (or, in the case of some coworkers) reset credentials when they don’t log into the smaller domain...
  • Lessons Learned

    Selecting an XML Element by #text value

    • 0 Comments
    This is actually just a wrapper for the [text()=’value’] XPATH attribute.  As I said in a previous post, this blog is my own knowledge base.  When I need this, and I’m sure I will, I’ll remember writing this entry, search my blog, and there...
  • Lessons Learned

    Why Don’t I Use [Parameter(Mandatory=$true)]$ParameterName?

    • 0 Comments
    PowerShell’s Domain Specific Language for describing command-line parameter attributes (see about_Functions_Advanced) offers many features.  It’s powerful, versatile, and allows scripts to implement some of the worst features of true cmdlets. What...
  • Lessons Learned

    PowerShell for N00bs: Libraries, Paths, and You

    • 0 Comments
    One of the scripts a coworker inherited has the following workflow: 1. Set-Location $PathContainingScript 2. & $Script Why?  Because the original author was a believer in libraries.  Or, specifically, TheMotherOfAllLibraries.ps1.  Divide...
Page 1 of 8 (199 items) 12345»