As part of a disaster recovery script, early on I wanted to ensure that all of the vdirs on a server were using ASP.NET 2.0. That meant that I wanted to run
aspnet_regiis.exe -r
but I didn't want to make any assumptions about what drive or directory Windows was installed in. What I wanted was something like this:
%WINDIR%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -r
In PowerShell, an easy way to get the value of an environment variable is to use the $env namespace like so:
$env:windir
but when I put this all together and tried to run it, PowerShell didn't parse it the way I expected:
$env:windir\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -r
Unexpected token '\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe' in expression or statement.
Putting quotes around it doesn't help. Now PowerShell assumes you want a string.
"$env:windir\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe" -r
You must provide a value expression on the right-hand side of the '-' operator.
What you need to do is make it clear to PowerShell that you want to execute the next string as a command. The way to do this is to use the call operator & (ampersand). Here's what I ended up with:
& $env:windir\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -r
That's more like it. This is the sort of little roadblock that tends to frustrate people trying to use PowerShell for the first time. I hope this helps!
Posted
Dec 02 2007, 11:27 AM
by
keith-brown