This is cache of http://pluralsight.com/blogs/keith/archive/2007/12/02/49336.aspx. Cache is the snapshot of article that we took when we index feed.
To see original page click here.
We are not affiliated with the authors of this article and not responsible for its content.
Execute in PowerShell
2007-12-02 11:27:00 by Keith Brown in Security Briefs
 

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!

 
 
 
 
 
 
TOP SEARCH
Expand / MinimizeClose Widget
  •  
RECENT SEARCH
Expand / Minimize
  •  
RELATED VIDEO
Expand / Minimize
SecurityRatty FAQ
Sergey Zarubin, 31yo
CISSP, CCSP
Moscow, Russia