Lessons Learned

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

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

    One-Liner: Getting My Documents and Other Special Folders

    • 0 Comments
    I have roaming profiles, which is great until I need to reference stuff in my Documents folder. Sure, I can hardcode \\ProfilerServer10\Mydocs2\timdunn\My Documents into my script, but I want to make my scripts more reusable by my team members. The closest...
  • Lessons Learned

    WMI and HP Product IDs

    • 0 Comments
    An HP Product ID is related to the hardware SKU The plain-English way of getting the SKU is Get-WMIObject –ComputerName $computerName Win32_ComputerSystem However, our inventory system uses the HP product ID property stuffed in ILO.  As someone who...
  • Lessons Learned

    Enums, Enums, Enums, a One-Liner

    • 0 Comments
    Enums are short for Enumerations, which is a fancy way of saying, “map this [string] to that [int].”  They’re handy when you want to limit choices, such as ‘Continue’, ‘SilentlyContinue’, etc. In this case, I’m looking at the System.Net.NetworkInformation...
  • Lessons Learned

    WMI-Less Network Adapters

    • 0 Comments
    We can get a list of network adapters (calling them NICs is a bit outdated: the C in NIC stands for ‘card’, like it’s something you plug into an ISA or PCI slot.  These days, it’s almost always built in) from Get-WMI Win32_NetworkAdapter and Win32_NetworkAdapterConfiguration...
  • Lessons Learned

    Getting Event Logs with wevtutil.exe

    • 0 Comments
    A long time ago, I wrote a painful way to export event logs (to CSV) on Server 2003, which lacks wevtutil.exe. Well, here’s the flip side of the coin. Also, I wrote a simple wrapper to get the topmost event log names. It’s really just Get...
  • Lessons Learned

    One-Liner: Resetting Desktop Toolbar

    • 0 Comments
    For some reason, HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Streams\Desktop is set on some of these machines, which means my previous snippet to set it in http://blogs.msdn.com/b/timid/archive/2014/02/19/meatball-surgery-the...
  • Lessons Learned

    Those Who Don’t Know Their History…

    • 1 Comments
    My function:\prompt changes every so often. For my latest, see http://blogs.msdn.com/b/timid/archive/2014/02/07/prompts.aspx ... but one thing I have often used is the snippet ($([int]((Get-History | Select-Object -Last 1).ID) + 1)) This gives...
  • Lessons Learned

    I Should Have Done This Years Ago (New-Shortcut, Revisited)

    • 0 Comments
    In spite of having this blog, sometimes I rewrite a function because I forgot I wrote it already.  Case in point, New-Shortcut.  It was one of my earlier entries in this blog: http://blogs.msdn.com/b/timid/archive/2009/09/06/powershell-for-non...
  • Lessons Learned

    Meatball Surgery, the Taskbar, Toolbars, and You

    • 2 Comments
    It takes me a while, but when I get used to something, I hate to change.  Windows 7 introduced the new Taskbar, and I hate it.  I hate having to click or hover over a ‘stack’ PSH windows to select one. I prefer the old XP (rest in peace) way...
  • Lessons Learned

    The Clipboard

    • 0 Comments
    Talk about a sea change. I started off wondering how to read text out of the clipboard. Thanks to this link below, that’s trivial, and it even showed me a better way than | clip.exe to stuff it into the clipboard. My exact words: “Well, that...
  • Lessons Learned

    I Never MetaCharacter (I Didn’t Like)

    • 1 Comments
    Lame title jokes aside, a metacharacter is a character that describes other characters. Put together, they form Regular Expressions. Regular expressions are used for string matching, which is a less threatening way of saying parsing . Basically, with...
  • Lessons Learned

    Finding Disk Hogs (i.e. Large Folders)

    • 0 Comments
    “Disk full? It was only at 60% last week! Where did all the space go?” If you haven’t said this, then you probably won’t appreciate this post. It seems such a relatively simple task – give me a list of the biggest folders...
  • Lessons Learned

    Sanity-Checking RDG Files

    • 1 Comments
    Remote Desktop Connection Manager is a must-have tool. However, it too easily falls out of date. Depending on your environment, you might be spinning up new machines regularly, especially if you’re in a virtualized environment. Here’s a quick...
  • Lessons Learned

    Modules vs. Scripts: It’s Not Just the Letter ‘M’

    • 0 Comments
    Here’s an interesting gotcha if you are spawning a new window for asynchronous execution via the "cmd.exe /c start" hack. This works as expected in a script, but doesn’t work in a module. $start = get-date cmd /c start powershell -command "start...
Page 2 of 9 (201 items) 12345»