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: Enabling and Disabling Users

      All good things must come to an end.  Sometimes, users become ex-users.  Here’s a quick-and-dirty way to check. One notable line here is: [bool] ( ( $_ . UserAccountControl . ToString ( )   -as   [int] )   -band   2 ) ; With AD, the second bit from the right is the...
  • Blog Post: Why Use Semi-colons

    I get asked from time to time why I still put semi-colons at the end of each statement. Part of it is force of habit. Part of it is trying to make sure my script source is useful no matter how the web server, post edtior, and user's brower decide to display it. Sometimes, some combination of the above...
  • Blog Post: Abbreviated Key Names in Synthetic Properties

    The title is almost longer than the post. From Chris Dent's http://www.indented.co.uk/index.php/2010/01/12/powershell-iis-and-log-settings/ , I see that he's generating synthetic properties with Select-Object -Property @{n = 'name'; e = { "this row's name" } } Notice anything? PSH allows the...
  • Blog Post: PowerShell for Non-N00bs: LiveMeeting XML API and You

    I work with Microsoft LiveMeeting regularly, and have found PowerShell's ability to interact with its public XML API interface to be of great value. There's even a blog devoted to this topic (last update August 11, 2008 - it's a stable interface, so there's little change.) Here's a scrubbed screen...
  • Blog Post: Powershell for non-N00bs: Creating XML the .NET Way

    I'm dogmatic - if there is a 'right' way to do something, I want to do it (or at least learn how to do it) the 'right' way. So often, I've seen [ xml ] $xml = "<xml />"; That's one way to do it, but not the RIGHT way, dammit. I want to use the constructor: $xmlDocument = New...
  • Blog Post: PowerShell for Non-N00bs: Creating Shortcuts

    $shell = new-object -com wscript.shell if ( $name )#create shortcut in same directory { $shortcut = $shell . createshortcut ( "c:\" + $name + ".lnk" ) } Else #create shortcut in specified directory { $shortcut = $shell . createshortcut ( $source + ".lnk" ) } $shortcut . targetpath...
  • Blog Post: PowerShell for Non-N00bs: Formatting Time With RegEx

    So, last episode we learned how to format the output of a LastBootUpTime property from the WIN32_OperatingSystem WMI query to something human readable using WMI. What if that didn't exist? PSH> (Get-WmiObject -Query 'SELECT LastBootUpTime FROM Win32_OperatingSystem').LastBootUpTime 20090712112652...
  • Blog Post: PowerShell for Non-N00bs: System Uptime Via WMI

    Getting the last time a system was rebooting is pretty easy from WMI. From this half-decade old post , we get the two key bits: the LastBootUptTimem property of the Win32_OperatingSystem WMI class. In PowerShell, it looks like this: PSH> (Get-WmiObject -Query 'SELECT LastBootUpTime FROM Win32_OperatingSystem...
  • Blog Post: PowerShell for Non-N00bs: Adding a User to a Group (with Script)

    Here's the one-liner: ([ ADSI ] "WinNT://$computerName/$localGroupName,group" ).Add( "WinNT://$domainName/$userName" ); But a script may be more useful for execution.
  • 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 for Non-N00bs: Certificates Installed on a Remote Host

    Okay, we know how to examine when a certificate file expires. How about whne a certificate that's installed on a computer? PSH> $computer = ‘remote.host' PSH> $ro = [System.Security.Cryptography.X509Certificates.OpenFlags]"ReadOnly" PSH> $cu =[System.Security.Cryptography.X509Certificates...
  • Blog Post: PowerShell for Non-N00bs: Certificate Files

    Let's say we have a certfiicate file. ("We have a certificate file." Yuck-yuck, haw-haw.) When does it expire? PSH> $certObject = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $pathToCertFile; PSH> $certObject.NotAfter Wednesday, January 21, 2011 12:49:18 PM ...
  • Blog Post: PowerShell for Non-Noobs: Tracking 302's

    There are times I need to see what hops a URL takes along the way from what I type to what it shows in the address bar. From a GUI, I just fire up Fiddler or HTTPWatch . If I am trying to automate it, that's a different story. PS> $url ='http://microsoft.com'; PS> $req = [system.net.webrequest...
  • Blog Post: PowerShell for Non-N00bs: [PSObject]::AddNodeProperty - the Missing Link

    We all know PSH can output objects. When the cmdlets or input give it objects to work with, outputting objects is easy. However, when the program is dealing in strings and objects are required, things get long. PS> $psobject = New-Object PSObject PS> Add-Member -MemberType NoteProperty -InputObject...
  • Blog Post: PowerShell for Non-N00bs: [PSObject]::AddNodeProperty - the Missing Link

    We all know PSH can output objects. When the cmdlets or input give it objects to work with, outputting objects is easy. However, when the program is dealing in strings and objects are required, things get long. PS> $psobject = New-Object PSObject PS> Add-Member -MemberType NoteProperty -InputObject...
  • 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: Hardware SKU

    Someone asked me what kind of server I was running in the lab. Uh, I don't have lab access, and even if I do, how the heck would I find it? PS> Get-WmiObject Win32_ComputerSystem | fl Domain : testlab Manufacturer : HP Model : ProLiant DL380 G4 Name : testlab1234 PrimaryOwnerName : TotalPhysicalMemory...
  • 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 for Non-N00bs: Symlinks

    Here's how to create a shortcut in PowerShell. Actually, it's how to create a shortcut with COM in PowerShell function New-ShortCut { param ( [ string ] $target = $null , [ string ] $name = $target , [ string ] $parent = ( Get-Location ).ToString(), Normal 0 ...
  • 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...
  • Blog Post: PowerShell for Non-N00bs: My Script Outline

    This is more for me to know where to go looking for this. I don't expect anyone else to laud it and worship it. If you find it useful, great. If you find holes in it, please let me know. At the end of this post will be a walkthrough. #region #######################################################################...
  • Blog Post: PowerShell for Non-N00bs: How Much is That Character in the Window? (Or, How to Change Your Password Without Really Trying)

    I'm creating a slightly different track here. PowerShell for Non-N00bs is for me to track some of the tidbits I've found along the way. This will be a two-fer article. By this I mean I'm going to address two tangentially related subjects: how to find an 'odd' character that is visible, but not matching...
Page 1 of 1 (25 items)