As I learn how to be a blogger (including the abandoning of Windows Live Writer), I keep finding lots of interesting new things. For instance, in my last post, I needed to escape the square bracket “[“. In my research, I found of list of HTML escape codes, including the one that I needed: http://www.degraeve.com/reference/specialcharacters.php
WordPress understands PowerShell
March 16, 2011While constructing my last post, I wanted to include a PowerShell function. I attempted to use Windows Live Writer because I thought it would be easier to include code. Windows Live Writer had three source code formatting modules:
1: function Test-CodeFormatter
2: {
3: [CmdletBinding()] param()
4:
5: 1..10 | foreach-object { "{0} {1}" -f "Hello", "World" }
6: }
1: function Test-CodeFormatter
2: {
3: [CmdletBinding()] param()
4:
5: 1..10 | foreach-object { "{0} {1}" -f "Hello", "World" }
6: }
- Source Code Formatter (http://plugins.live.com/writer/detail/source-code-formatter)
1: function Test-CodeFormatter2: {3: [CmdletBinding()] param()4:5: 1..10 | foreach-object { "{0} {1}" -f "Hello", "World" }6: }All three of them looked pretty terrible in WordPress. After a little more Bing searching, I discovered: http://vkreynin.wordpress.com/2009/02/24/syntax-highlighting-in-wordpress-using-windows-live-writer/. This allowed me to paste my code in directly as html:
<pre>1 function Test-CodeFormatter { [CmdletBinding()] param() 1..10 | foreach-object { "{0} {1}" -f "Hello", "World" } } [/sourcecode]</pre>And it finally looked pretty.
function Test-CodeFormatter { [CmdletBinding()] param() 1..10 | foreach-object { "{0} {1}" -f "Hello", "World" } }
Loading a .Net 4.0 Snap-in in PowerShell V2
March 14, 2011I recently started developing a PowerShell snap-in, and without putting much thought into it, I created a .Net 4.0 project. After getting the initial code ready, I attempted to load the snap-in and got the following error:
This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.
After a bit of searching, I found that this was not a PowerShell issue, but a .Net one, and the solution was to add to the PowerShell.exe.config file. The following was needed:
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0.30319"/>
<supportedRuntime version="v2.0.50727"/>
</startup>
</configuration>(http://tfl09.blogspot.com/2010/08/using-newer-versions-of-net-with.html)
For PowerShell to work using remoting, the same solution must also be applied to wsmprovhost.exe (http://tfl09.blogspot.com/2010/08/using-later-versions-of-net-framework.html).
In ultra-lazy fashion, I spent half an hour writing a function to place one of these configuration files for me, which is probably 10 times as long as it would take for me to do it all the times I will need to. However, writing scripts is fun. ![]()
function Write-DotNet4Config
{
<#
.SYNOPSIS
Function to write .Net 4 configuration files for me so I don't have to remember
.EXAMPLE
Write-DotNet4Config "$env:SystemRoot\system32\WindowsPowerShell\v1.0\powershell.exe"
Writes a configuration for the given executable if none currently exists.
#>
[CmdletBinding()]
param($executable, [switch]$whatif)
if (-not (test-path $executable))
{
Write-Error "Cannot find executable $executable"
if (-not $whatif.isPresent) { return }
}
if(test-path "$executable.config")
{
Write-Error "Path already exists";
if (-not $whatif.isPresent) { return }
}
$versions = @("v4.0.30319", "v2.0.50727" )
$config = @"
<?xml version="1.0"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
$($versions | %{'<supportedRuntime version="{0}"/>' -f $_})
</startup>
</configuration>
"@
Write-Verbose "XML: $config"
Write-Verbose "Output file: $executable.config"
if($whatif.isPresent)
{
Write-Host "What if: Writes to file $executable.config"
Write-Host "What if: Writes xml: $config"
}
else
{
([xml]$config).Save("$executable.config")
}
}
Who am I? White, James White
March 14, 2011 Who
My name is James White, professional programmer. I rank among those programming geeks whose love affair started at a very young age–a maze game in BASICA. My background is in philosophy and psychology, although I did a bit of computer science study. Upon my discovery that one of these topics was more fun and paid well I became a professional programmer. Currently I work for Microsoft on Lync Server. My day-to-day work is focused on C# and PowerShell. To keep things spicy, I occasionally perform web programming, SQL, and C++, with a goodly portion of my time spent on MakeFiles and unit tests.
What
As someone who loves to read, I have many thoughts about many subjects. Lately those subjects are mostly programming theory, project management, and what thinkgeek toys are most fun to play with during compilation. Language theory and how languages change the way you code is a favorite subject of mine right now. I plan many posts on the uses and abuses of different languages.
Posted by grimkey