Cmdlets vs Functions

A discussion came up recently in an internal mailing list about the difference between Cmdlets and Functions. What was most interesting was how many hard and fast distinctions and rules came out of the discussion, even though those distinctions don’t really exist. The key point is that there really isn’t a hard and fast philosophical distinction between cmdlets and functions. Most differences largely exist because of setup, installation, and supported features.

Secret SQL Escape Characters

I learned of an evil SQL escape sequence today, in the context of a data migration script. The script moves data from one database to another, but the schema changes between databases, so you can’t use BCP. As such, the script needs to ensure that it does not modify any of the database content. The script works well, and it creates insert statements based on the values and the content of the old data.

Invoking Generic Methods on Non-Generic Classes in PowerShell

A question recently came in asking, “How do you invoke a generic method on a non-generic class in PowerShell?” [Edit: this "just works" in PowerShell V3] PS C:\Users\Lee> Add-Type -Path C:\temp\generic.cs PS C:\Users\Lee> $r = New-Object NonGenericClass PS C:\Users\Lee> $r.GenericMethod("Hello", "Hello") Hello Multi Generic World: True In an earlier post (http://www.leeholmes.com/blog/CreatingGenericTypesInPowerShell.aspx), we talked about how to create generic types in PowerShell, but classes can contain generic methods even when the classes themselves don’t represent generic types.

Obfuscated PowerShell

For some reason, a surprisingly common (albeit half-joking) remark about PowerShell is that it hasn’t “arrived” until you can write obfuscated one-liners like you do in C or Perl. They may have missed Adam’s obfuscated script (http://www.proudlyserving.com/archives/2005/11/obfuscated_mona.html,) or this email quoting / wrapping one-liner: http://www.leeholmes.com/blog/2006/10/25/email-quoting-and-wrapping-in-59-bytes/. While I normally try to clarify and educate, here’s another script to make your eyes burn: $ofs=""; '"$(0'+ '..(0'+ 'xa*['+ 'Math'+ ']::R'+ 'ound'+ '([Ma'+ 'th]:'+ ':Pi/'+ '2,1)'+ ')|%{'+ '[cha'+ 'r][i'+ 'nt]"'+ '"$($'+ '("""'+ '"0$('+ '1838'+ '1589'+ '*726'+ '371*'+ '60)$'+ '(877'+ '7365'+ '981*'+ '263*'+ '360)'+ '$(22'+ '2330'+ '793*'+ '1442'+ '99)$'+ '(310'+ '9*37'+ ') ""'+ '"")[' + '($_*' + '3).

Why Another 3rd Party Book?

Gerd recently posed a good question on the PowerShell blog: _I’m really surprised to read about another 3rd party book from a member of the PS team. To me PS is the most intriguing MS innovation for years and Bruce’s book is really excellent and answered almost all my questions, but maybe I’m not the only one wondering why PS team members write that busy for the bookstore shelf, as long as the official MS documentation on PS is less than adequate.

PowerShell: The Definitive Guide Rough Cut now Available

With the book getting close to completion, O’Reilly has now posted a “Rough Cut” version of the book here: http://www.oreilly.com/catalog/9780596528492/. The Rough Cuts program gives you early access to the majority of the book’s content before publication, with the option for an online and print bundle for a discounted price.

Interested in being a Technical Reviewer?

[Edit: Thanks for all of the interest – the review period is now closed.] We’re getting close to “content complete” of Windows PowerShell: The Definitive Guide. The next step is technical review, where we look for both high-level and low-level feedback on the content and structure. Also, this is a book focused on administrators. While PowerShell uber-hackers are always appreciated, inexperience with PowerShell is extremely valuable, as well. If you’re interested in being a Technical Reviewer of the book, please let me know and I’ll forward your information to O’Reilly.

Discovering Registry Settings - PowerShell and Process Monitor

To give a glimpse into the writing process behind my upcoming “Windows PowerShell - The Definitive Guide” (O’Reilly,) I’ll occasionally post entries “as the author sees it.” This entry illustrates how to discover registry settings for programs that do not otherwise support scripted automation. Discover Registry Settings for Programs Problem You want to automate the configuration of a program, but that program does not document its registry configuration settings. Solution Use Sysinternals' Process Monitor to observe registry access by that program.

Agony

You Can Write C / Assembly / Perl in any Language

One of the most common tasks when administering a system is working with its files and directories. This is true when you administer the computer at the command line, and is true when you write scripts to administer it automatically. Fortunately, PowerShell makes scripting files and directories as easy as working at the command line — a point that many seasoned programmers and scripters often miss. A perfect example of this comes when you wrestle with limited disk space, and need to find the files taking up the most space.