Lessons Learned

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

Posts
  • 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...
  • Lessons Learned

    PowerShell –EncodedCommand and Round-Trips

    • 0 Comments
    PowerShell.exe accepts the –EncodedCommand parameter, which is a way to ‘wrap’ DOS-unfriendly command strings in such a way as to be safely passed into PSH for execution.  It’s a great feature.  However, it has a huge documentation hole. ...
  • Lessons Learned

    Scripting Best Practices, or Write-Host Considered Harmful

    • 1 Comments
    Disclaimer: this is going to sound like I actually know what I’m doing.  Let’s be honest – I work in Microsoft, home to the people who created PSH, .NET, and the native internals that people invoke with Add-Type and other arcane arts.  There...
  • Lessons Learned

    Hotkeys and RDC Man

    • 0 Comments
    There are days I seem to live on RDC Man.  One day, I might try setting my Shell= to RDCman.exe.  On a VM, of course. At any rate, there are times I need to switch back to the host machine.   While I would like to just see a ‘minimize...
  • Lessons Learned

    Do-Something | Set-Variable Foo

    • 0 Comments
    Especially for stuff that generates a lot of output, I’ve learned to pipe commands to Set-Variable. It’s no different than starting the command with “$foo =”, such as $foo = Do-Something, However, I like ‘building’...
  • Lessons Learned

    Function Skeleton for Invoke-Command –AsJob

    • 1 Comments
    PSRemoting jobs are quite amazing. While they are a little complicated to grasp at first, they’re quite powerful. However, with great power usually comes great responsibility. We know we can spawn a gazillion jobs, but how about managing them? How...
  • Lessons Learned

    Parsing Dates by Hand

    • 0 Comments
    I’m always impressed by the amount of variety in input formats that Get-Date can accept and still parse it into a date. However, there is one time that it vexes me: US vs. EU date strings.  Specifically, the US uses dd/MM/[yy]yy, while the EU uses...
  • Lessons Learned

    CredLocker – Storing PSCredentials to File

    • 0 Comments
    Summary CredLocker is tool to securely store PSCredentials to a file such that the file can be copied to multiple hosts and the PSCredentials can be retrieved from that file via processes running on those hosts. The function + the data store file...
  • Lessons Learned

    Invoke-WebRequest, a First Attempt

    • 0 Comments
    “To learn PowerShell, you first have to have a problem to solve.” Well, this is utterly non-work-related, but I’m sure I can find more respectable uses than defeating the idleout of a web chat. The situation is pretty simple: I’m on a fandom site where...
  • Lessons Learned

    Capturing Write-Noun Cmdlet Output

    • 0 Comments
    I’m sure there’s a cleaner way to capture the output from Write-Host, Write-Verbose, etc.  However, I’m just going to “go caveman” in the words of a coworker and just hack around with the Verb-Transcript cmdlets. Without further ado: function Get...
Page 2 of 9 (215 items) 12345»