PowerShell quickie: expanding the properties of a variable inside a string

Here’s something that gave me a bit of trouble: I was attempting to run a script to upgrade a bunch of SharePoint solutions, and I wrote it like this:

dir –Recurse –Filter *.wsp | %{ stsadm –o upgradesolution –name $_ –filename $_.FullName –allowgacdeployment –local }

Now, that actually works, but since I hadn’t tested it before, I figured I’d echo the command out first, so I tried this:

dir –Recurse –Filter *.wsp | %{ echo "stsadm –o upgradesolution –name $_ –filename $_.FullName –allowgacdeployment –local" }

Which is all well and good, except it echos out something like the following:

stsadm –o upgradesolution -name Foo.wsp -filename Foo.wsp.FullName -allowgacdeployment -local

The reason this happens is that the string expansion in PowerShell has no way of knowing that I meant to expand the property instead of printing out the literal characters. Having figured this out, the next thing I tried was:

echo "${_.FullPath}"

Which doesn’t work either. Crap. I’m not familiar enough with the PowerShell syntax to explain why that is, but after a moment of pondering I finally figured out something that does work – expressions:

dir –Recurse –Filter *.wsp | %{ echo "stsadm –o upgradesolution –name $_ –filename $($_.FullName) –allowgacdeployment –local" }

The $() syntax allows us to evaluate any arbitrary expression, which is then inserted into the string. Yay!

Get assembly files from GAC using PowerShell

A while ago I found myself in an unfortunate situation where I had to use Reflector to figure out what I was doing wrong when interacting with third-party components. Unfortunately, a large part of the third-party code was in assemblies that were installed in the GAC, and I was unable to find them anywhere else.

Here’s a quick PowerShell one-liner that helped me out:

dir C:\Windows\Assembly -Recurse -Filter "VendorName*.dll" | foreach { copy $_.FullName C:\Temp\assemblies }

That was pretty quick and easy to come up with, and it makes me very glad I decided to learn the basics of PowerShell. 🙂

Protocol-relative HTTP URLs

If you’re visiting a web page over an SSL encrypted connection, you might sometimes get an annoying “mixed content” warning. In essence, the browser is telling you that yes, the page itself was delivered over HTTPS, but other bits were not, and that may or may not mean that you’re in danger.

From the developer point of view, having to build every URL so that the protocol is taken into account can be tedious. It might equally well be the case that your framework of choice already handles this. If so, good for you.

However, there’s another escape hatch not known to most people: protocol-relative URLs. You write the url without the protocol and following colon, and the client will choose whichever protocol was used to retrieve the page, like so: //server:port/path/to/resource.html

Not sure how often this is actually necessary, but there you go. 🙂

Power to the people

My esteemed colleague Jouni Heikniemi is a speaker at this year’s TechDays Finland. His topic is “PowerShell for Developers”. We had a chance to hear his presentation beforehand this morning. I’ve been meaning to do so for a few years now, but after the presentation I finally found the motivation to try and write an actual PowerShell script for myself.

The problem

I wanted a simple script to bundle an ASP.NET web application in a directory – basically, copy all the files that are essential to running the app, and nothing more. Something I’d typically use a NAnt or MSBuild task for.

The solution


So I fired up PowerShell ISE (I assume that’s Integrated Scripting Environment) and after a few attempts, came up with this:

$targetPath = "deploy"
if (Test-Path $targetPath) { Remove-Item $targetPath -Recurse -Force }

New-Item $targetPath -type directory

Copy-Item -Path .\App_Data -Destination $targetPath -Recurse 
Copy-Item -Path .\App_GlobalResources -Destination $targetPath -Recurse -Filter "*.resx"
Copy-Item -Path .\bin -Destination $targetPath -Recurse -Filter "*.dll"
Copy-Item -Path ("*.as?x", "*.js", "*.gif", "*.jpg", "Web.config") -Destination $targetPath

Since it creates a directory that can be deployed as an application, I saved the snippet as Deploy.ps1 (in hindsight, Build.ps1 would have been a better name, but I’m too lazy to take another screenshot).

This bit of script is very specific to my needs, so don’t take it as a textbook example of good PowerShell scripting. 🙂

The snag and the resolution

As it turns out, PowerShell scripts aren’t allowed to run, at all, by default. Fortunately, the default is relatively easy to change. Fire up PowerShell or the ISE with administrative privileges, and type:

Set-ExecutionPolicy RemoteSigned

RemoteSigned means you’re free to run unsigned scripts as long as they’re locally created, but scripts downloaded from other sources need to be signed. In case you’re curious, downloaded scripts are identified based on Alternate Data Streams.

PowerShell will ask you to confirm this. In the case of ISE, by means of a really, really long-ass dialog text:


Once you confirm the change, you can restart PowerShell with limited privileges, and scripts will work.

For more info on PowerShell, visit Jouni’s series of blog posts on the topic.

SharePoint web part lifecycle

For various reasons, I’ve found myself doing Web Part development for SharePoint 2007. While I suspect I’ll be blogging more thoroughly about it later (possibly even via SharePoint Blues, who knows) , I thought I’d contribute my first useful SharePoint link: The lifecycle of SharePoint Web Parts.

My WebForms experience is rather limited, but on the surface, the list looks a lot like a WebForms control’s life cycle. The reason I needed this list specifically is that I wanted to know the point at which I could safely assume that other web parts would be connected and their data would be available.

Chapter 5: But I Can’t Because…

(This is part 5 in a series of posts where I document my progress through reading Becoming a Technical Leader: An Organic Problem-Solving Approach by Gerald Weinberg, answering the questions at the end of each chapter)

1. Do you know someone whose leadership style you particularly admire?

“Try to create a short career biography of this person, characterized in MOI terms. If possible, interview the person to check your perceptions.”

I can’t really name anyone for this exercise. This has been the primary reason I’ve been sitting on this blog post – the rest of it has been ready for quite a while now. I’ll just have to move on, unfortunately.

2. When was the last time you made a major change in your career?

“Which do you remember best, the factual details, or the feelings?”

I think the one and only major change so far has been leaving my first job after nearly seven years. I was primarily anxious, and afraid that I wouldn’t be good enough for the “real world”. Definitely feelings.

3. How did you react the last time someone you know well made a major career change?

“What else does that tell you about yourself?”

I was happy for that person, and a little envious. I felt like they were going somewhere, while I wasn’t. It’s mostly an issue of low self-esteem.

4. What change in your career have you made that you still don’t fully accept?

“don’t fully admit to others? to yourself? Can you now let go of it? Why?”

I’m not sure what this question is asking. A change that I wasn’t actually fully aware of, and hence don’t accept it, or a change that was forced on me? Either way, I don’t think I have any good examples.

5. Can you remember the first major decision point in your career?

“Can you still recall your feelings? Were you afraid to take the step? Was your fear justified? Was it worth it anyway?”

See question 2. Yes, I was afraid. Yes, I think the fear was justified, because I didn’t think we had ever assessed our actual competence level realistically. It was worth it, though. The next time I moved to a new job, I wasn’t anxious, and I think I made a point of looking at the right things when making the decision. I also got valuable experience from that job.

6. What are your choices for the next major decision point?

“What happens to your body as you contemplate each possible alternative? What messages pop into your mind?”

More or less direct responsibility. More or less of time in the spotlight. Less seems comfortable, but a bit boring. More makes me nervous and excited. The messages are along the lines of “do you want to make a difference or not?”

7. Have you ever been appointed Leader of anything?

“Did people begin to assume that you, as Leader, would now handle situations that they could perfectly well handle themselves? How did you deal with these situations? Were you so taken with your new status that you tried to handle them yourself, rather than delegating them where they belonged? How did your actions affect the group’s later reactions to you as Leader?”

Worse. I was so taken with my status, that I tried to handle things even when nobody asked me to! It lead to tension, bad decisions and me realizing that I wasn’t anything close to a real leader.

8. Next time you are appointed Leader of some group, keep a list of situations …

“… in which people assume that you as Leader will come up with the crucial ideas, but in which you exercise leadership by changing the environment so as to encourage them to do it. Keep going until you have at least ten items.”

Um, OK.

9. Have you ever felt when appointed Leader that you were essential to the group?

“What did the group do when you were not around? What did they do when you eventually left? Could there have been some sort of self-fulfilling prophecy in operation?”

See question 7.

10. For some group you work with, consider ho much of your influence in that group derives from your appointed position.

“How much comes from your idea power? How much from enhancing the idea power of others? Are you satisfied with this mix?”

Influence from appointed position is something I’d like to mostly do without. Most of my influence tends to come from my own actions and ideas. So far, I haven’t been very good at enabling others, except when paired with someone who is a good match with me.

11. Tomorrow morning change your traditional breakfast in some way.

“Notice if your day changes.”

I seemed to be a bit more focused in the morning, having a routine-breaking thing I needed to do.

12. For the next two weeks, change your breakfast each day according to some new idea each day

“(such as fewer or more calories, faster of slower preparation, more or less wholesome, more interaction with other people, less interaction with others, more appealing to look at, thirty minutes later, thirty minutes earlier, in a different place, using different utensils, hot instead of cold, cold instead of hot, more liquid, less liquid). Note what, if anything, is happening to you, and summarize the effect at the end of the two weeks.”

After a couple of days I settled into a mode, where I expected that my morning routine, while being routine, would change every day. I tried to avoid planning too much in advance, so I had to think on my feet to respond to the changes. I’d go as far as to say I was a bit more prepared for routine things to change in other areas of my day.

13. Tomorrow change the way you interact with some person you see frequently.

“Try to do it in such a way as to improve your interaction. Note the results.”

A tiny bit of conscious effort daily can have a huge positive impact on interaction, at least when both parties are naturally inclined to get along anyway. 🙂


Previously: Chapter 4: How Leaders Develop  Next up: Chapter 6: The Three Great Obstacles to Innovation

Duct tape, pine cones, a Swiss Army knife and a virtual machine or two

Today, two things that amaze me to no end:

First, a set of tools for functional testing, that allow me to write .NET code that talks to a Java server which starts a web browser, and makes it load JavaScript which then calls back to the server, and somehow this setup works and allows me to control multiple web browsers in a coordinated manner!

Second, something I thought about today only after I left work was still bugging me when I got home. So I fired up my Windows XP Mode, used it to open a VPN connection (via a web browser!) to work. Through the VPN I then used Remote Desktop to connect to my work desktop. On that desktop, I fired up my Windows XP Mode, and used it to open a VPN connection (standalone client, this time) to a network hosting a production server. Through the VPN I then used Remote Desktop to connect to the server, wherein I fired up SQL Server Management Studio to run a query against the production db.

what would mcgyver do

The most amazing thing?

While in Management Studio, I selected the query I had written, and hit Ctrl+C.

Then, I moved focus back to the computer I was physically on, opened Notepad, and hit Ctrl+V.

And it worked.

Don’t let anyone tell you programmers can’t get their shit together. If copy-paste can work reliably through that many layers, it can’t all be doom and gloom. 😉

PS. Longer-than-usual wait on Chapter 5. I’m not procrastinating, I’ve answered most of the questions already. Some of them, however, were exercises spread out to the next two weeks, and I can’t really write down the results until I have them. 

So I switched jobs again

This is not a lolcat.Like I wrote in a previous post, I’ve now began my first week as a developer working for Sininen Meteoriitti. Today was day two. Time to put down a few impressions.

First, the most important thought that bubbled up in my sleep-deprived consciousness today (while watching The Big Lebowski and recovering from a rather heavy pizza – there goes the diet!) was that I’m now part of a larger community of developers that give a crap. Obviously not a very well integrated part, this being day two and all, but a part nonetheless. This has some rather profound implications, chief among them that I no longer have an excuse to not get shit done.

In previous positions, I’ve had ideas on how to build up the collective competence of my team, to do geeky, fun stuff while learning in the process. So far, my follow-through rate has been abysmal, and I’ve always had at least one obstacle that I could point to and say “see, that’s why I can’t make this work.” Possibly I’ve even been right a couple of times. This time, though, I’m told from the get-go that if I’ve got ideas, I’ve got support too. It’s all up to me.

Today I got several interesting nudges from Jouni, wicked bastard that he is. 🙂 One of them resulted in an idea: learn F# and hold a brown-bag lunch presentation where I introduce it to others. I’m not committing to a date, but it would combine two goals of mine: practicing the art of presentation and learning a new language. So yeah, I think I’ll put that on my to-do list. Starting with the “learn” part.

I also got my first assignments, and in the process realized how poorly I really know ASP.NET, especially the Visual Studio tooling. The stuff I do know was probably current sometime back in 2004. Crap. Far from being a technical leader – if this is the yardstick, I’m a crappy follower.

Speaking of leadership: I don’t think I mentioned why I’m blogging my answers to questions presented in a book. I suck at sticking to doing something, be it physical exercise, a diet or answering difficult personal questions in the name of learning. The point of these posts is to force me to think about each question, and write the answer down. I hope the content is interesting enough to warrant the search engine hits I seem to be getting…

In Chapter 4 I set myself a goal of practicing my guitar playing for 15 minutes every day. It’s a sad testimony of my piss-poor commitment that I managed to miss two days from the last seven. I also recalibrated my expectations: instead of trying to play and sing at the same time, I’m concentrating on playing riffs right, with a metronome. It seems to be working, too. Who knew? 😛 I’m combining daily practice with what I got from reading The Talent Code: I’m trying to split the problems into chunks, then practice the chunks in a way that allows me to fail in a controlled way, and then hone that failure away. Once that’s done, I can do the same thing with the chunks, and so on. (I’m paraphrasing heavily here.)

On the study front, I failed again to sign up for the study techniques course. This time I remembered it, and even had a reminder to sign up. Unfortunately, while my other spring courses opened up for registration yesterday, registration for that one ended last Friday. They’re doing this on purpose, I frigging know it. I did manage to sign up for two grueling math courses that should keep me occupied for the spring. In fact, when I plotted them on my weekly calendar, the idea that I might have tackled more seems absurd. I also decided to not re-take the exam for Theory of Computation. I’m not going to learn anything significant in the exam, and improving the grade would only serve my ego.

Also, finally got me an iPhone, which explains the gratuitous cat photo. This concludes my status update. I promise I’ll follow up with Chapter 5 soon.

1 2 3 4 5 6 14