First, I found Keith Hill’s Free PowerShell EBook that I’d previously mentioned quite valuable. Recommended.
Here’s another reference you’ll want: Bruce Payette’s Windows PowerShell Refcard.
And a few more links:
Creating Cmdlets
This is a relatively advanced topic, and is not required to use PowerShell. However, it’s something I’m interested in. I’d like to try creating DSLs (Domain-Specific Languages) in PowerShell.
Allegedly, the Windows PowerShell SDK is required. The installation instructions are unnecessarily complex. Why can’t Microsoft give us a “download just the PowerShell SDK and nothing else” link? Why do I have to install the Windows Server 2008 SDK?
Once you’ve installed the relevant portions (read the download instructions carefully), you’ll then need to locate the relevant DLL to reference. If you read way down on this article on the Windows PowerShell Blog, you’ll learn that “Reference assemnblies are installed as part of SDK installation under ‘Program Files\Reference Assemblies\Microsoft’". On my system, this is:
C:\Program Files\Reference Assemblies\Microsoft\
WindowsPowerShell\v1.0
and the assembly that your project must reference is:
System.Management.Automation.dll
Oddly, the folder timestamp showed that it was created on my PC long before I installed the SDK. I checked another PC that just had PowerShell and Visual Studio installed and found this assembly at the above location. In other words, I do not believe that you need to install the SDK.
To create a cmdlet in C# or VB.NET:
To install the new cmdlet in PowerShell:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\installutil MyCmdlet.dllGet-PSSnapin -RegisteredAdd-PSSnapin [Name]When testing, you can use the /u switch with installutil to uninstall a snap-in.
Just a thought, but wouldn’t it have been simpler to have a PlugIn folder where you could drop DLLs that PowerShell would recognize and load on startup? The current process smacks of COM to me.
You can also write your own provider. Get-PSProvider lists the current providers, which allow PowerShell to treat various hierarchies like file systems on drives.
ReSharper version 4.5 added significant functionality to the Solution-wide Analysis feature. I have found this to be a valuable tool. However, it’s wise to be extremely cautious about removing public members that ReSharper says are never used.
I would not expect ReSharper to know about projects that are not part of the current solution. If you’re creating a shared library, you’re likely to see a lot of “unused” public members.
What was less obvious to me is that data binding is not detected by ReSharper in this context. For example, say I have a public string property named FullName on a class. ReSharper displays a hint that “Property ‘FullName’ is never used". ReSharper’s Find Usages likewise finds no references. The property may still be data-bound to a control, though, since data binding generally references the property name as a string. Removing the property would cause a runtime failure.
ReSharper’s Find Usages Advanced can be used to find the binding, if Textual occurences is checked.
“Class ‘Foo’ is never instantiated” hints may also be incorrect. IoC containers (such as Unity) or other Reflection-based techniques can fool ReSharper.
Please note that I’m not suggesting turning off Solution-wide Analysis (and if you’re not using ReSharper, you’re working harder, not smarter!). Just be aware of its limitations.
Update - August 17, 2009
The UsedImplicitlyAttribute can be used where necessary to mitigate this problem.
Moq version 3.1 is out; I updated our tests from version 2.x this afternoon. The only breaking changes I encountered were that the Expect() and ExpectGet() methods were deprecated in favor of Setup() and SetupGet(), respectively. (Not technically breaking, but I had warnings-as-errors turned on.) A few quick search-and-replaces fixed this, and all the unit tests passed.
If you’re not familiar with Moq, it’s a library for creating mock objects (or stubs, or fakes - mocks in the broad sense) to assist with unit testing. Personally, I find it significantly easier and more intuitive than Rhino Mocks. Your mileage may vary, but Moq generally works the way I expect it to.
Blogs
I started reading some of the PowerShell blogs today. There are many, but I’ve subscribed to two feeds so far:
Here’s a particularly interesting (to me) post from the former, where they discuss Microsoft’s guidelines for writing new cmdlets:
Increasing visibility of cmdlet design guidelines
There’s also a post about a Free PowerShell EBook. (It was tricky to download that through the proxy server at work, but the links worked fine at home.)
I really like grep. Windows Search Containing Text seldom seems to give me the results I want. Grep works, it’s fast, and it lets me use regular expressions.
I’m used to an old version of Borland’s Turbo GREP that shipped with Delphi. It’s old, it doesn’t work well when files have long lines, and it doesn’t support Unicode (particularly UTF-16). Microsoft’s SQL Server Management Studio has a nasty habit of saving SQL text files as UTF-16, so I don’t always find the saved query I’m looking for.
I found out that Windows XP (and up) has a utility called FINDSTR that acts much like grep (type “help findstr” in a command prompt for more info). Unfortunately, it doesn’t support Unicode either. See http://stackoverflow.com/questions/408079/findstr-or-grep-that-autodetects-chararacter-encoding-utf-16.
PowerShell comes to the rescue. It appears to support Unicode/UTF-16, at least if the byte order mark is present. See http://kevin-berridge.blogspot.com/2008/06/powershell-grep.html. I think the first comment on that post is half right; the issue is that “ls” in PowerShell is an alias for Get-ChildItem and is returning a collection of FileInfo and DirectoryInfo objects. In UNIX, the output of ls is just text, so that’s all grep can operate on. PowerShell pipes objects, so it is more powerful (albeit sometimes trickier).
Here’s the feedback I submitted to Audible.com:
I just started on Audible, and have only downloaded one book. I found the experience very annoying, and am debating if I want to remain a member.
First, I had to enable JavaScript to even sign up for your site.
Second, I had to download, install and configure custom software to download books. Have you ever noticed that EVERYONE hates iTunes? That’s exactly what AudibleManager reminds me of.
Third, AudibleManager had problems with Sandboxie (http://www.sandboxie.com/). I finally had to run Firefox outside the sandbox (another security issue, along with the JavaScript) in order to get this to work.
Fourth, AudibleManager contacts your site. I had to configure my firewall to permit this.
Fifth, I use an iPod. I don’t trust iTunes, and disable all automatic syncing. I manage my library manually. I wanted to do the same with Audible. This was quite difficult. I finally figured out the procedure. Basically I had to download the file into my AudibleManager library, copy it, paste the file into Windows Explorer, open iTunes, add the file to the iTunes library (because it won’t let you add a .aa file to your iPod otherwise, unlike .mp3 files), and then drag it over to my iPod.
Contrast this with Amazon’s MP3 downloads. OK, I did install their MP3 download manager. But its interface is simple, it downloads files to a directory of my choice, and it works in a sandbox. After I purchase and download the MP3 files, I bring up iTunes and drag the files onto my iPod. Done.
Maybe there’s an easier way to do this, but it’s not at all clear from your site. I was given an array of confusing options - formats, install devices, etc. AudibleManager is not intuitive - you can’t right-click a file and say Save File As. I develop software for a living, and I want to be in control of my computer and my files.
- Bill Sorensen
(If they respond, I’ll try to post it here.)
“Learning” blog serials seem popular lately. I am a rank amateur with Windows PowerShell, but you’re welcome to stumble along with me.
References
Windows PowerShell in Action (eBook) - Bruce Payette
See also: http://www.manning.com/payette2/
Essential PowerShell (book) - Holger Schwichtenberg
Setup
PowerShell Community Extensions
I tried the demo of PowerShell Plus - I liked the IntelliSense and debugging, but the trial period was too short to convince me to shell (heh) out $145. A coworker (Justin) suggested the free PowerGUI; I’ve installed this but haven’t done much more.
If you want to run scripts (.ps1 files), the first thing you’ll need to do is set the security policy with Set-ExecutionPolicy. RemoteSigned is the easiest (if not the most secure) option for learning.
> Set-ExecutionPolicy RemoteSigned
Help
Use “help” or “man” (aliases for Get-PagedHelp) to get help on a command.
> help Get-Process
Spacebar goes to the next page; q quits. (UNIX users will find this more ;) familiar.)
Help with no parameter lists available aliases, cmdlets (basically commands), providers, and so on.
Get-Command shows you the available cmdlets.
Tab-completion is a handy feature; try typing “Get-” and then hitting [TAB] repeatedly. It works for parameters, too; try typing “Get-Process -” followed by several [TAB]s.
Objects
Not only does PowerShell give you the true piping power of UNIX shells, it does more. Rather than piping text, it pipes objects.
$_ represents the current object in the pipeline (in ForEach-Object, Where-Object, etc.).
Get-Member (alias gm) is your friend. You can pipe an object (or objects) into it to get information on the object.
> Get-Process | gm
This tells you that Get-Process returns a collection of System.Diagnostics.Process objects and lists the members.
Note that Select-Object (alias select) will screw with Get-Member, as it creates new PSCustomObject instances.
Aliases
While you may not want to use aliases extensively in scripts (ever tried to read Perl?), they are your friend at the command line.
Get-Alias (alias gal) is your gal-friend. (Couldn’t resist.)
Show all aliases:
> gal
Show the definition for the ps alias:
> gal ps
Show all the aliases of a cmdlet:
> gal | ? { $_.Definition -eq "Get-Process" }
(? is the alias for Where-Object.)
I/O
Common DOS and UNIX commands (cd, pwd, dir, ls, cat, etc.) are often aliased or otherwise made available to the shell.
Get-PSDrive (alias gdr) shows you the available drives. You can treat different providers in the same manner, which makes it possible to do things like getting a listing of all environment variables:
> dir env:
Note that you add a colon (:) on the end, as you would with a drive letter (C:).
One way to get the contents of a file is with variable notation and a provider:
> ${c:foo.txt} | ? {$_ -match "java.*version"} | sort
Alternately, you could use Get-Content (aliases gc, cat, type).
If you want to look at the contents of your GAC (Global Assembly Cache), one way is to open Windows Explorer on “C:\WINDOWS\assembly". You can also Start/Run and type that path.
I found out recently that this does not work across a network. Start/Run “\\some-workstation\c$\WINDOWS\assembly” appears to work, but will actually show you your own local GAC.
Update - August 18, 2009
Philippe Parent, coworker and friend, wrote:
…today I needed to check the gac on [a remote workstation], turns out if you issue a ‘dir’ to the command line it
works, ie:dir \\machinename\c$\windows\assembly\devexpress* /s
Thanks Phil!
The third Iowa Code Camp is going to be May 2nd, 2009 in Cedar Rapids, Iowa.
Thanks to Chris Sutton for the reminder!
I have a friend’s wedding that day; otherwise I would plan to attend. It’s well worth your time.
I recently finished reading the book Schneier on Security by Bruce Schneier.
Pros:
Cons:
Recommended.
If my statistics are correct, my readership has grown. I thought I’d highlight some older posts that newer readers may find interesting.
Comments welcome!
I just received an email with the subject line “Facade, Proxy, and Factory". I assumed it was legitimate and opened it:
many kilos ? be healthful.
real working product is come
just try
(Russian link)
Now that’s targeted spam.
Development Central is the blog of Bill Sorensen, a professional software developer. Much of this will relate to C#, .NET, and OOP in general.
Disclaimer
These postings are provided "AS IS" with no warranties and confer no rights.