Short posts about Microsoft PowerShell as I learn, trying to focus on one aspect at a time.
Saturday, May 2, 2009
An Excellent Post on Bitwise Operators
If you find yourself working with Microsoft APIs for any length of time, you'll find yourself having to deal with bitmasks and bitwise operations. Mark Schill over at String Theory has an excellent description of what they are and how to work with them.
Friday, May 1, 2009
Advanced Functions: Using Values from the Pipeline (2.0 CTP3)
Note: The following code will only work on Powershell 2.0 CTP3 or later.
A while back I showed how you can use $input as a parameter to a function in order to use it in the pipeline. Unfortunately that technique has the side effect of making PowerShell stop and gather up all variables that are being sent down the pipeline into $Input and then passing it to the function. Ordinarily it doesn't matter much, but if you are processing a lot of data, say the results of a SQL query, you may end up using up massive amounts of memory and watch your computer grind to a halt while the memory manager swaps a billion times a second (okay, I made that number up).
The ValueFromPipeline Parameter Property
As the name suggests, this property indicates whether or not a parameter can take a value from the pipeline. Just set it to $true when declaring the variable (you'll see an example at the bottom of this post).
Doing that alone will not give you the effect you're looking for, though. It will mysteriously process the first item passed down the pipeline...and then suddenly stop. This was very frustrating when I first came across it, and left me scratching my head, until a post on the microsoft.public.windows.powershell group happened to mention the critical missing piece in an unrelated discussion.
BEGIN, END, and PROCESS Blocks
The first thing you need to know is that if you want to take a value from the pipeline and use it, then you need to add in the optional PROCESS Block. Every function essentially has a BEGIN, END, and PROCESS scriptblock in it:
- BEGIN - This scriptblock is run the first time the function is launched
- PROCESS - This scriptblock is run each time the function receives input
- END - This scriptblock is run after all input has been processed
Here's the important part: explicitly declaring these blocks is optional, but if you don't use them, then the body of your function executes in the END Block. That's why it was only executing once.
As an example of a script that uses these blocks, take a look at this little ditty I whipped up for work (I highlighted the important parts):
Labels:
BEGIN Block,
CTP3,
END Block,
Functions,
Pipes,
PROCESS Block
Tuesday, March 31, 2009
SMS Module Updated
I updated the SMS Module (old post here) for PowerShell v2. I'm using it with v2 CTP3. Now you can type "help " to get help on these functions, and check out the old post for more examples of how you can use the Get-SmsWmi and Find-SmsId functions.
Functions included are:
- Get-SmsWmi - Makes it easy to get WMI objects from the SMS Provider. (Note: call this function with no parameters to get a list of acceptable class nicknames)
- Find-SmsId - Look up SMS WMI objects by ResourceID, CollectionID, PackageID, or AdvertisementID
- Get-SmsCollectionTree - Inspired by tree.com from DOS, prints out the Collection structure of your site, with CollectionIDs and names.
- Approve-Client - Marks an SCCM client as Approved in the SCCM DB.
Don't forget to set the $default_wmi_provider_server and $default_site variables at the top of the module to match your environment.
Labels:
Collections,
Distribution Points,
Find-SmsId,
Get-SmsWmi,
PowerShell v2,
Resources,
SCCM,
SCCM SDK,
SMS,
SMS Provider,
SMS.psm1,
WMI
Wednesday, February 18, 2009
Get-ChildItem (dir) Results Color-Coded by Type
I always liked the way you can color code your directory items in Linux. I always set alias ls='ls -al --color' the minute I log into a system. It makes it easier to scan results and instantly know some extra data about the items in a directory.
I decided that I wanted to be able to do this in PowerShell. If you do a few Google searches you'll see I'm not the first, and it's not without it's quirks, but I decided not to worry about being true to the Linux form and just go with the spirit of what I wanted to get out of it. I've included the code below. There are a couple of things of note, though it's mostly waht I wanted:
- I would rather if the header at the top wasn't always the same color as the first item, but I can't think of any way around that.
- Rather than coloring results by permissions, I've colored them by type, with four major types: Directory, Executable, Compressed, and Text.
- If you use sort or select you'll lose the color, but otherwise the color change should last down the pipeline for each obect.
- This was made to be used with v2.0 CTP3 of PowerShell. If you want to use it with earlier versions you have to remove the documentation block at the beginning of the function, but you also lose the ability to use the help command to find out more about the function.
- Note that I compile the Regular Expressions at the beginning. If I just used -match to check the regular expressions for each file, I would add a lot of extra overhead, because the regular expressions would be compiled each time a file was checked. This boosts performance, and there is an analogue in each language that supports regular expressions.
Labels:
Dir,
Get-ChildItem,
Get-ChildItemColor,
Ls
Thursday, November 6, 2008
Like VIM? Like PowerShell? This link's for you!
Vim is a text editor that is popular with *nix users, but it works just fine on Windows, too.
I subscribe to Andy Schneider's Get-PowerShell blog, and today he had an entry about a new extension for Vim that does your syntax highlighting, etc, for PowerShell. I recommend checking out the Get-PowerShell blog, but if you just want the file, follow the link from Peter Provost's blog.
Tuesday, November 4, 2008
Did I Mention PrimalForms is free?
Here's that link again.
Oh, and for some reason this post shows up under the Google search , but click here if you want a PrimalForms example.
Windows Forms with PowerShell
SAPIEN came out with an awesome form builder for PowerShell called PrimalForms. I decided to give it a whirl. It really makes short work of making a native Windows GUI. For my first project I made a quick and dirty app that allows me to get disk fragmentation data for a remote server using the DefragAnalysis method of Win32_Volume. I used the PrimalForms GUI to create my form, and then I only had to fill in the button1 click handler and the form load handler, add a small function for printing to the text box, and voila!
Labels:
Defragment,
Forms,
PrimalForms,
SAPIEN,
Scripts,
Win32_Volume,
Windows Forms,
WMI
Subscribe to:
Posts (Atom)