|
|
-
Our 3rd generation PowerShell Host and Development Environment - PowerShell Plus has reached 1.0! We are doing away with the pretense of RC1, RTM etc when its not like we are releasing anything to a manufacturer to print a million CDs as we simply just release it to our web site.
The difference between our beta and 1.0 release is our commitment to our customers. We have disabled the auto-updating functionality because we only want to push out builds that have been widely tested to customers - however for those who like to live on the edge of innovation , we will continue to give access to our latest builds. In the future also we will update our auto-updating technology to allow you to choose to update to either trusted official releases, and latest builds.
Another difference in 1.0 is now we also have a MSI installer for those inclined. We are careful however not to ostracize those of you, like us, who value a portable application that doesn't have to have an installer and that can be run from a thumb drive if desired. Thus PowerShell Plus 1.0 has both a portable zip, and a MSI installer.
We are discontinuing selling the PowerShell Suite, but licenses for PowerShell Analyzer and the PowerShell Suite automatically work with PowerShell Plus, and PowerShell Analyzer will continue to be available to all customers as we continue to migrate important features over to PowerShell Plus 1.1
New Price
We have lowered the price to $79 - effectively giving you an Enterprise grade application for a hobbyist price. Check it out now at our Store
Additionally PS+ has a free 30 trial and beyond that it is free for non-commercial use!
Download
Videos
Check out our videos. There is our original trailer, plus a detailed video of our debugger, which is the most feature complete PowerShell debugger around - generations ahead of anything else.
Official Blog
We are moving away from PowerShellLive and centralizing everything including our forums and blogs around www.powershell.com . Our Official Blog is now at http://blog.powershell.com
Some Screen Shots
Here are a few screenshots to whet your appetite.
1) PowerShell Plus console with GUI code completion in MiniMode.
2) PowerShell Plus Full Console with Variables, Properties and Help Center visible while inserting a snippet directly into the console.
3) PowerShell Plus editor window showing our save pipeline object code completion.
4) Editor showing some debugging, the variable inspector and console preview, as well as code completion of the file system with file icons.
Enjoy
Our home page http://www.powershell.com may have some old information of it, but now that we have 1.0 out, we'll get that caught up soon.
|
-
The other day in the #powershell IRC channel on freenode, a regular wanted to know how to find out the difference in properties between two objects. He had an object with 100 properties and another with about 120 or so and wanted to know which ones were unique in the second object. Compare was the natural first place to look, but it really is good for telling the difference in CONTENT rather than SCHEMA (the object properties). Here is an example of an easy way to do this.
$a = 1 | select-object name, age , sex $b = 1 | select-object species, name , age $a.psobject.properties | % { $_.name } | ? {$($b.psobject.properties | % { $_.name } ) -notcontains $_ }$b.psobject.properties | % { $_.name } | ? {$($a.psobject.properties | % { $_.name } ) -notcontains $_ }
Here we simply create a couple of objects that have some similar and different properties. then the first example shows the properties in A that aren't in B, and the next shows the properties in B that aren't in A.
The main trick here is .psobject ... all objects have a "hidden" psobject property that contains some cool PowerShell related stuff, including the properties. There are some good blog entries around on psobject and its definitely worth a deeper look. This technique is also the basis on a script i will introduce soon, my join-object function.
-Karl
Technorati Tags: powershell
|
-
PowerShell Analyzer 1.0 is finally released. For a week or two we consider this a "soft" launch, just in case there is some major issue that eluded our testing, but we are pretty confident its stable.
Additionally, if you have already used your 45 Day trial, we have EXTENDED it another 45 days.
PowerShell Plus is now available with a 45 Day trial as well, and Additionally its available fully free for Non Commercial use as well.
So download both of these today and Enjoy
PowerShell Analyzer Screen-shots.

To get a grasp of what PowerShell Plus can do, Check out its trailer. its about 4 minutes, and definitely worth the time in our opinion.
|
-
Finally my main blog is back in Business. I had gotten discouraged with my previous web host and abundant comment spam, and just plain being busy. But now with the help of an updated WordPress, new Host and Windows Live Writer all should be good.
All my old posts are gone, as far as being web pages, but I have archived them into a PDF here. Maybe I'll revisit some of the topics in future posts if they are worthy enough.
So what's new?
- I've been a PowerShell MVP for some time now.
- Shell Tools is sponsoring the PowerShell.org project. Actually I have a blog there, and was going to blog there rather than back at www.karlprosser.com/coder but I really wanted to use Live Writer, and the DotNetNuke Blog module is still substandard, so I'm just going to cross post here and there, and also at Shell Tools support site, so its searchable on this blog. I know its OVERKILL . We'll see how it works out.
- We've been busy with PowerShell Analyzer and PowerShell Plus. PowerShell Analyzer 1.0 is finally out, and we have a trial of the PowerShell Plus for use in a commercial environment, and a free license for non-commercial use. Check it all out www.powershell.com
- Combined PowerShell Analyzer and PowerShell Plus downloads are now far in excess of 100,000. Yay :)
- The PowerShell Debugger in PowerShell Plus is getting better by the day, and the debugger in PowerShell Analyzer should add an interesting twist to the works. More on this in future posts.
- Documentation. We admit documentation for PowerShell Plus and PowerShell Analyzer is very sparse, so one of our priorities as this year wraps up is a number of Video Tutorials, and even some hard copy documentation/ tutorials - shock , horror.
Plus I have tonnes of generic PowerShell topics inside me, bursting at the seams, just waiting to come out.
|
-
Powershell Refactored. – Introduction and Invoke Ternary.
The purpose of this column is to present powershell filters, functions and cmdlets. I have always wanted to make my own language, and in the past have experimented with and made some, mostly domain specific, however the effort to make a general purpose language as good as the ones I used in my day to day professional was too much, so they were mostly eccentric hobby projects for the sake of learning.
One thing I love about powershell is just how easily it can be extended. With carefully designed and thought out functions, cmdlets and filters you can make powershell look and feel about just like anything. In fact certain build in cmdlets that you use everyday, like foreach-object, where-object, compare-object, sort-object, measure-object and select-object, aren’t really part of the powershell language so much as they have anything to do with the built in keywords or powershell engine, but they are only part of the powershell language because they were shipped with it, However internally these cmdlets are very simple, but they are well thought out functions, and very useful, and practically are part of the language.
The goal of this column is to produce and share functions that are just as useful and generic as those built in ones, and extend powershell in areas where it is lacking. When extending a language like this, we have to be careful, that the extensions follow a consistent look, feel, and behavior. Also we want to make sure the extensions are very generic, in that they can be used in just about any context, and finally we don’t want to dilute the language. So we have to make very useful and powerful functions, that are easily understandable , and don’t balloon into too many keywords that would confuse users.
So this weeks’s “powershell refactored function” is invoke-ternary, a function I had released some time ago, and is included as part of PSCX (Powershell Community Extentions). Jeffery snover blogged about this function here. One of the glaring omissions in powershell is a ternary operator. A ternary operator is valuable in any programming language, but neccisary even more in powershell , since more often that not in powershell, commands aren’t executed over a number of lines, but through embedded expressions inside scriptblocks at various stages of the pipeline. i.e
"F","M","M","F" | foreach-Object { "its a $_" }
Expressions So often I found that I was unneccisarily writing embedded if statements inside these scriptblocks as below:
"F","M","M","F" | foreach-Object { if ($_ -eq "M") { $a = "boy"} else {$a = "girl"}; "its a $a" }
E Which made things harder to read, and didn’t give me the speed/pithyness typical of powershell, and got messy in more complex scenarios. The need for a ternary operator was just screaming out at me. So since the normal pattern for ternary operations in most C based languages is <true/false expression> ? <true value> : <false value> I wanted to use ? and :, so I chose to call the function ?: but really that is better as an alias so I called the function Invoke-ternary and made an alias called ?:
Here is that function
set-alias ?: Invoke-Ternary -Option AllScope -Description "PSCX filter alias"
filter Invoke-Ternary ([scriptblock]$decider, [scriptblock]$ifTrue, [scriptblock]$ifFalse)
{
if (&$decider) {
&$ifTrue
} else {
&$ifFalse
}
}
The scope of this column isn’t’ to get into how it works, but you can see its not many lines of code. This attests really to the awesome design of powershell. So how would we do our previous example.
"F","M","M","F" | ?: {$_ -eq "M"} {"its a boy"} {"its a girl"}
Here you can see that the decider, and the true and false sections have to be scriptblocks, and also note that it is a filter, and this works on the pipeline directly, allowing us not to have to embed this in a foreach-object. So you could do something like:
"F","M","M","F" | ?: {$_ -eq "M"} "its a boy" "its a girl"
A great this about the community is that people can pick up on things and improve it.
The above function was hashed out really quick. Nick Howell noticed that though it is powerful to allow the decider, and the true and false expressions to be scriptblocks so they can contain a lot of logic, but often you are just returning static objects.
. that is also the case in the examples before.. So it would be nice to not have to have “it’s a boy” inside a scriptblock, as in this case it is redundant, so he improved the function. So that you could do something like this:
"F","M","M","F" | ?: {$_ -eq "M"} "its a boy" "its a girl"
Unfortunately there were some issues with nicks version and it didn’t work in many situations, but it was a good idea. In a later column I will introduce a cmdlet version of this, which is more robust, designed for use in and outside the context of the pipeline, and it takes advantages of Cmdlets ability to have multiple parametersets.
That’s all for now.
Check out our join-object set of functions next week.
|
-
"192.168.1.100" ,"machine2" | foreach-object {([wmiClass]"\\$_\ROOT\CIMV2:win32_process").Create("calc")}
So what does this do?
Well first of all it takes in a list of machines, in this case 2, one specified by name, and one by IP, then pipes that into a foreach "loop" and from there we leverage WMI to reach out to another machine and create an instance of a WMI class called win32_process. With that class we can then do various tasks, call various methods, In this case Create, which starts a calc.exe on each of those computers. (Note that on remote computers the calc.exe won't be visible, just will be in your task manager, however its an easy example, and when you pass in 127.0.0.1 or localhost as the machines for testing, then it will popup on your own machine).
High Level Breakdown
- Array to Pipeline. - So lets break it down into the powershell features that are used to do this. The first is the pipeline. We have an array of things that are getting passed. These things happen to be strings of machine names that we explicitly declare. we could have these. This array gets stripped by powershell and passed along the pipeline to the next command there.
- Foreach-Object CmdLet. - This looks like a normal foreach loop, and effectively behaves like one, but is actually a Cmdlet. So for every item that comes down the pipeline. whatever that is in the {scriptblock} that follows gets run.
- [WmiClass] cast. - [typename] is how you cast in powershell. So here we are casting the following string to create a WmiClass object. This is a special cast, in comparison to simple casts of [int], [string] , in that the [WmiClass] in powershell is an adapter.
- $_ within String. - $_ is a special variable meaning the current item in the pipeline. When you put variables within a string they get expanded. In this case its the name of the machine.
- Create('Calc') method call. - Calls a method on the Wmi object that creates a process on the remote computer.
Detailed Breakdown
-
Array to Pipeline. - LEVEL 1
After Commands and Parameters, the behaviour of the pipeline is the next thing that a beginner needs to understand with powershell. Its a large topic and quite involved so I will only give a slight overview here. But basically the pipeline strips arrays and collections of objects and sends the objects through the pipeline one at a time. Think of a literal real world pipe with different markers along the way. You put an object in the pipe then roll it along, and it goes through each marker and continues further. In the same way if you run:
"hello" , "there", "sir" | A | B | C"Hello" gets piped through to A, then to B then to C then out the others ide (usually transformed to text on your computer screen), Then after that the next item in the array - "there" gets piped through A first, then B and C, and so on.
-
Foreach-Object CmdLet. LEVEL 1 Gotcha Warning Foreach-Object is actually a cmdlet, and to a traditional programmer the syntax looks quite normal so most people miss that fact that its not a language keyword here, but a cmdlet that is processing the pipeline like any internal or third party Cmdlet. Also another gotcha is that the word foreach is an alias to foreach-object and also there is an actual Powershell Language foreach feature which works different, more akin to foreach in Visual Basic, which doesn't work on the pipeline. Keep this in the back of your mind if sometime you are finding yourself dealing with strange errors and behaviours when you are using foreach. Gotcha Warning Often in scripts you will see the alias %. % is simply an alias for foreach-object. I use this all the time because foreach is one of the most common commands you write in powershell and % is pithy, but will confuse and scare away beginners as it makes the language look more like a cryptic code, than source code, but bear with it, in no time % will come naturally to you.
1,2 | foreach {"hello $_" }
In the above example the output will return two items - "hello 1" and "hello 2". Once we understand the pipeline and that % is an alias for foreach-object, its pretty obvious. A programmer with a C-based language will feel quite at home with the curcly bracket style thinking its strongly typed with the language keyword foreach. But really foreach is a Cmdlet - In Dos terms think command line program like ipconfig. {"hello $_"} is actually a parameter. Here we don't specify the name of the parameter because powershell can assume Cmdlet parameters by position but we can actually declare it explicitly like below.
1,2 | foreach-object -process{"hello $_" }
The code above now is looking less like a traditional programming language, but a commandline in a traditional shell, and powershell is indeed a shell. The above syntax really helps drive this point home. run:
get-Help foreach-Object -full
to learn all about foreach-object and its parameters. The -Process parameter runs the associated scriptblock for EVERY ITEM in the pipeline. The -Begin runs its scriptblock before foreach-object first gets its first object, and -End after the pipeline has finished giving it objects to process.
Compare our previous example to the example below which uses the LANGUAGE foreach command.
foreach($i in 1,2) { "hello $i" }
Notice here that what is in { } is not a scriptblock parameter like with foreach-object but rather a language scope strongly bound to the foreach language keyword as in a traditional programming language. Another thing to note the language imperatives such as foreach, for, do while are not inheriantly compatible with the pipeline
-
[WmiClass] cast. - LEVEL 3.
With powershell you can cast various types as other types along the same lines as in any other dotnet language. Here is a couple of examples: [int]"2" [string]2However being a dynamic language it also gives you grace and does a conversion for many of the basic types so you can do the likes of:
[datetime] "2001-01-01"
rather than having to do something like :
[datetime]::Parse("2001-01-01")
however unlike wmiclass a datetime in powershell is still the same as a dotnet datetime object, with the same properties and methods, even though it was created through a cast that in reality did a conversion from another type rather than just a plain cast. Powershell (and you yourself) can even extend ANY class, spotwelding on properties and methods of your liking, but types like [WmiClass] go a step further than that. They are called adapters. They adapt the underlying class to have different methods and properties to make it more useful and natural to use in powershell. A good example of this is the xml class in powershell. Underlying the xml class is a dotnet System.Xml.XmlDocument type. Lets have an example. [xml]"<A><B>test</B></A>"
With this example here you see the first thing it is doing is a CONVERSION. converting the string containing the XML into an XmlDocument object. If that string wasn't valid XML you would get an error. Now that we have an XmlDocument we can access most of the usual properties and methods of an XmlDocument, however as powershell has adapted this class, we can do some more useful things. Powershell has added properties based on the contents of this specific instance. I.e. the A and B nodes. so if i type this:
$myxml = [xml]"<A><B>test</B></A>" $myxml.A $myxml.A.B
The first result returns the XmlNode of B(also adapted), while the last line returns the string content of "test". In a similar way with [WmiClass] powershell will adapt and add the releveant properties and methods of the WMI object you are accessing. These properties are going to be different for a Process than for a Hard Drive or Registry Entry. So given that file://mycomputer/ROOT/CIMV2:win32_process is a valid Wmi path.
[wmiClass]"\\mycomputer\ROOT\CIMV2:win32_process"
creates a wmiclass for the specified remote computer, and adapts methods and properties that are relevant for a process to it.
-
$_ within String. - LEVEL 1
Users of PERL will be most at home with this. $_ is the default variable. The easiest way to thing about it is that it contains the object that is currently coming through the pipeline. The next thing to note in powershell is that when it processes strings it will automatically try to expand variables that are within double quoted strings, but it will not expand those within single quoted strings.
$a = "test" "hello $a" 'hello $a'
So in the first example, with the double quotes it gets expanded to "hello test", and the 2nd example doesn't get exampled so it remains the literal "hello $a". in the same manner: "\\$_\ROOT\CIMV2:win32_process"the $_ gets expanded each time this is run to whatever is coming through the pipeline. so the first time it gets expanded to: "\\192.168.1.100\ROOT\CIMV2:win32_process"
While the second time around in the foreach-object it gets expanded to: "\\machine2\ROOT\CIMV2:win32_process"
Both producing valid strings when it gets converted to a WmiClass with the cast:
[wmiClass] "\\$_\ROOT\CIMV2:win32_process"
-
Create('Calc') method call. - LEVEL 2
So now we have a valid WmiClass which is using a WMI win32_process class against a remote system. We can now use methods against it and see its properties. Here our goal is to start a process on the remote machine. try the following:
$mywmi = [wmiClass]"\\localhost\ROOT\CIMV2:win32_process" $mywmi | get-Member
Here we are piping the object we just created to get-member to see what is availible, property and method wise. Apart from the properties common to all WMI objects (they are adapted here started with a double underscore such as __SUPERCLASS) you can see the properties and methods that are adapted SPECIFICALLY to a WMI win32_process. Amoung those is the one we want - Create. from Get-member you can also see the syntax that is expected and with the following command you can test it on your own box. $mywmi.Create('notepad.exe')
Summary
This oneliner article has been a lot longer than one liner, and probably longer than most will be in the series, however I wanted to give a good overview of some key concepts of powershell for beginners. I would also like to thank Xaegr who is a regular in our #powershell chat room for suggesting this particular theme for the oneliner.
-Karl Prosser
|
-
This column is all about showing how much you can do in powershell , in a variety of areas, with one line. Powershell truely is an agile platform, where once you get the hang of things, you can code at the speed of thought. And often produce something amazing in just one line.
This column will display the current one-liner the front page, then when you click on more, you will be able to see a write up and explanation of the one-liner. Feel free to submit your own one-liners and ideas for one liners, either as comments to this blog, or in this forum.
I will be following a particular format.
- One-liner - The one liner itself.
- Description - A brief overview and description about what the one-liner does.
- High Level Breakdown - The one liner will be dissected by logical parts with a brief description of the different parts.
- Detailed Breakdown - Those same parts will be covered in depth, often with other examples and references to the powershell feature or aspect that is being employed in that part. Also the descript will be rated from LEVEL1 to LEVEL5 Where level 1 targets core concepts of powershell for beginners and LEVEL5 is aimed at powershell veterans.
Enjoy,
-The Powershell Live Team.
|
|
|
|