Posting PowerShell Code with Live Writer

Lee Holmes recently wrote a post on PowerShell Syntax Highlighting. I have been using Windows Live Writer for a while now and have used a variety of ways to post code snippets, functions, and scripts.

I really like the look and feel of Lee's output. Nice and clean, easy to copy and paste, line numbers and color coding. 

So of course I went and tried it using his script. I was able to copy and paste into word without a problem and get all the cool nice highlighting, but as soon as I tried to paste in Live Writer, it just pasted in what seemed to be raw text.

I went digging around and found an option in Live Writer called "Paste Special."

The default was "Thinned HTML."

image

Using "Keep Formatting" allowed me to paste in the code just as expected.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
function Set-File {
param
(
    [parameter(Mandatory=$true,ValueFromPipeline=$true)]
    [string]
    $file
)

    $psise.CurrentOpenedRunspace.OpenedFiles.Add($file)
}

function invoke-caretline
{ 
    invoke-expression $([Regex]::Split($psISE.CurrentOpenedFile.Editor.text,"`r`n" )[$psISE.CurrentOpenedFile.Editor.caretline-1])
}


function invoke-region{
param
(
    [int] $num
)
    $ed = $psISE.CurrentOpenedFile.Editor
    $lines = [Regex]::Split($ed.text,"`r`n" )
    $foundfirst = -1
    $foundlast = -1

    for($count = 0;$count -le $lines.length-1;$count++)
        {
            if ($lines[$count].startswith("#region") -and $lines[$count].contains("@$num")) 
               { $foundfirst = $count;break}    
        }
       
    if($foundfirst -gt -1)
    {
        for ($count = $foundfirst; $count -le $lines.length-1;$count++)
        {   
            if ($lines[$count].startswith("#endregion") )
               {$foundlast = $count;break}    
        } # end For $lines
   
    if ($foundlast -gt -1)
     {
         $torun = ""
         $lines[$foundfirst..$foundlast] | % { $torun+=$_ + "`r`n"}
         invoke-expression $torun
     } # end if $foundLast
   
    } # End for $count
 
} # End Function Invoke-Region


function Insert-Text{
param
(
    [parameter(Mandatory=$true, ValueFromPipeline=$true)]
    [string]
    $text
)
    $currentFilePath = $psise.CurrentOpenedFile.FullPath
    $currentFile = $psIse.CurrentOpenedRunspace.OpenedFiles |
               where {$_.FullPath -eq $currentFilePath}
    $currentFile.editor.InsertText($text)
}

function New-FunctionTemplate{
param
(
    [parameter(Mandatory=$true, ValueFromPipeline=$true)]
    [string]
    $verb,
   
    [parameter(Mandatory=$true, ValueFromPipeline=$true)]
    [string]
    $noun   
)

$f = @"
function $Verb-$Noun {
param (
    [parameter(Mandatory=$true, ValueFromPipeline=$true)
    [string]
    $p1
)
begin {}
process {}
end {}
}
"@

    Insert-Text -text $f
}

I guess the next task is to write a plug-in for Jame's Write-CommandBlogPost

Comments (7) -

I missed Lee's post.

I wrapped James Brundages Write-CommandBlogPost in a Live Writer Add-in using C# and embedding PowerShell.

PowerShell – Programmtically post and retrieve PowerShell code on your blog

dougfinke.com/.../

Hey Doug,

I actually used your plug-in and it worked great. The only issue is you don't get syntax highlighting in the current state of Write-CommandBlogPost.

fralvoneInfef 5/23/2009 8:05:24 AM

Hello, I want to say hi everyone.
http://test.com/test.html" rel="nofollow">test

I can't wait to get an iphone 3G. But all my music and video are not in Iphone supported formats. So I think converter TuneCab will help me to convert them to iPhone. Did anybody try it?

What's up, is there anybody else here?
If there's anyone else here,  let me know.
Oh, and yes I'm a real person LOL.

Peace,

alanbeasley 9/16/2009 4:09:44 PM

Hi there, I am getting a large number of students that can't access the internet. When they log on and open IE they don't get the default web page or a log in screen. It appears to be random, I have tried setting up new user accounts but this also appeasrs to be hit and miss on whether they get access.

Any ideas?

Cheers
Alan

Hello

If you want freebet 20 EUR from betclick, visit http://betclick.blox.pl" rel="nofollow">betclick web site.

Best Regards

Comments are closed