Lessons Learned

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

Browse by Tags

Tagged Content List
  • Blog Post: Decrypting SecureStrings (Once More!)

    [PSCredentials] have some interesting methods. Chief among them, at least for today, is GetNetworkCredential() . Well, what’s so special about this? The returned object has the .Password property which is the plaintext of the password. I’ll say it again: $PsCredential.GetNetworkCredential().Password...
  • Blog Post: VM Host

    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 Machine\Guest\Parameters" /v hostname #>...
  • Blog Post: One-Liner: GetDateTimeFormats

    DateTime objects are amazing. You can format them in so many different ways, but they also come with a boatload formats pre-defined. Oh, and it uses the current culture. So much cleaner. But which is which? $i = 0; ( Get-Date ).GetDateTimeFormats() | Select-Object @{name = 'index' ; expression = ...
  • Blog Post: One-Liner: Setting Default Web Proxy for Windows Server 2003 and 2008

    Our labs run a mix of Windows Server 2003 and 2008. Setting a proxy in Windows Server 2003 is as follows: CMD> proxycfg.exe proxy.fqdn *.first.excluded.domain;*.second.excluded.domain;*.and.so.forth Windows Server 2008 doesn’t have proxycfg.exe, so the command is: CMD> netsh.exe winhttp...
  • Blog Post: PowerShell One-Liner: Getting This Computer's Asset Tag Number

    Here's a gem from an internal email: CMD> wmic SystemEnclosure get SMBIOSAssetTag 5551212 If the system has asset tag data added to the BIOS, this is a way to collect it.
  • Blog Post: One-Liner: Getting This Computer's Serial Number

    The computer's serial number can be very useful. Among other things, many manufactuers' support sites keep record of their machine's serial numbers to serve the right drivers for downloads. CMD> wmic bios get serialnumber SerialNumber ABCDEF12345 Wmic is the command line executable for performing...
  • Blog Post: PowerShell One-Liner: Removing a System Variable

    We've learned we can create system variables on a remote machine with the setx.exe command: PSH> setx -m remote.machine My_System_Variable VariableData However, setx doesn't give us a way to remove the variable. PSH> (new-object -com wscript.shell).environment("system").remove("My_System_Variable...
  • Blog Post: PowerShell One-Liner: What's the Function Name?

    Rather than have to put the function's name in the error mesage (and then forgetting to change it when I copy/paste/rename/modify it), here's a quick function that I can embed in the error message to spit out the function's name: function Get-FunctionName { ( Get-Variable MyInvocation -Scope 1).Value...
  • Blog Post: PowerShell One-Liner: Strings to Arrays and Back Again

    I'm (just) a old-school Perl hacker and miss their join() and split() functions. Turns out, while PowerShell doesn't have them, .NET does, to a point. PS> $home.split("\") C: Users timid And the other way around. PS> [String]::Join("|", $(gci)) Contacts|Desktop|Documents|Downloads|Favorites...
  • Blog Post: PowerShell One-Liner: Stripping Comments and Blanks

    I know, I know, XML is the new standard. There are still times where flat files are easier. Time is short, and I'm lazy. (I'm also impatient and working on hurbis. Geek-points if you can tell me where that's from.) Anyhow, here's a quick-and-dirty way to strip out comments, whitespace and blank lines...
  • Blog Post: PowerShell One-Liner: Decrypt-SecureString

    Let's say you have a $secureString. If you don't have one handy, create one: PSH> $secureString = ConvertTo-SecureString -Force -AsPlainText "hello" Actually, we're breaking the system - SecureStrings aren't supposed to be created this way - they're supposed to be created like this: PSH>...
  • Blog Post: PowerShell One-Liner: Is this a debug build or not?

    In a dev environment, there are debug builds and there are retail builds. And there are debug builds that get mistaken for retail builds... Here's a quick way to see if the binary in question is a debug build or not: PSH> (Get-Command kernel32.dll).FileVersionInfo.IsDebug False Good. I'm...
  • Blog Post: PowerShell One-Liner: sort | uniq -c

    Another track, this time for me to collect useful scripting snippets. Here's one: In Unix, if I have a non-unique list of strings, it's sometimes interesting to see the frequency in which the particular strings appear. For example, I have a list of errors from a long, and need the top 10. In Unix, it...
Page 1 of 1 (13 items)