Out-Blog!

[Edited again 25th November – Jachym gave me some suggestions and insights on the use of parameters, and I slightly changed/fixed the original code I had posted yesterday. There are still some more things that could be improved, of course, but I'll leave them to the future, next time I'll have time fot it (who knows when that will be?)]

This one is a post regarding my first test writing a cmdlet for PowerShell. After a few days since having change my blog's title to "$daniele.rant | Out-Blog" (where Out-Blog was a fantasy cmdlet name, and the title just meant to mimick PowerShell syntax in a funny way), I stumbled across this wonderful blog post: http://blog.boschin.it/archive/2006/09/21/4375.aspx that describes how to use the assemblies of "Windows Live Writer". Then I saw the light: I could actually implement an "Out-Blog" cmdlet. I am not sure what this could be useful for… but I thought it was funny to experiment with. I followed the HOW TO information on this other blog post to guide me through the coding: http://www.proudlyserving.com/archives/2005/10/lets_all_write_1.html

The result is the code that follows. you see is pretty much Boschin's code wrapped into a cmdlet class. Nothing fancy. Just a test. I thought someone might find it interesting. It is provided "AS IS", mainly for educational purpose (MINE, only mine…. I'm the one whose education is being improved, not you :-))

 

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Management.Automation;

using WindowsLive.Writer.BlogClient.Clients;

using WindowsLive.Writer.BlogClient;

using WindowsLive.Writer.CoreServices;

using WindowsLive.Writer.CoreServices.Settings;

using WindowsLive.Writer.Extensibility.BlogClient;

using Microsoft.Win32;

 

 

namespace LiveWriterCmdlet

{

[Cmdlet("out", "blog", SupportsShouldProcess=true)]

 

public sealed class OutBlogCmdlet : Cmdlet

{

[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]

[ValidateNotNullOrEmpty]

public string Title

{

get { return _title; }

set { _title = value; }

}

private string _title;

 

[Parameter(Position=1,Mandatory=true,ValueFromPipeline=true,ValueFromPipelineByPropertyName=true)]

[ValidateNotNullOrEmpty]

public string Text

{

get { return _text; }

set { _text = value; }

}

private string _text;

 

[Parameter(Position = 2, Mandatory = true, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]

[ValidateNotNullOrEmpty]

public string BlogApiEndPoint

{

get { return _blogapiendpoint; }

set { _blogapiendpoint = value; }

}

private string _blogapiendpoint;

 

[Parameter(Position = 3, Mandatory = true, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]

[ValidateNotNullOrEmpty]

public string UserName

{

get { return _username; }

set { _username = value; }

}

private string _username;

 

[Parameter(Position = 4, Mandatory = true, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]

[ValidateNotNullOrEmpty]

public string Password

{

get { return _password; }

set { _password = value; }

}

private string _password;

 

 

[Parameter(Position = 6, Mandatory = false, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]

[ValidateNotNullOrEmpty]

public string ProxyAddress

{

get { return _proxyaddress; }

set { _proxyaddress = value; }

}

private string _proxyaddress;

 

[Parameter(Position = 7, Mandatory = false, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]

[ValidateNotNullOrEmpty]

public int ProxyPort

{

get { return _proxyport; }

set { _proxyport = value; }

}

private int _proxyport;

 

[Parameter(Position = 8, Mandatory = false, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]

public string ProxyUserName

{

get { return _proxyusername; }

set { _proxyusername = value; }

}

private string _proxyusername;

 

[Parameter(Position = 9, Mandatory = false, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]

public string ProxyPassword

{

get { return _proxypassword; }

set { _proxypassword = value; }

}

private string _proxypassword;

 

[Parameter(Position = 10, Mandatory = false, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]

public SwitchParameter Published

{

get { return _published; }

set { _published = value; }

}

private bool _published;

 

 

 

 

protected override void BeginProcessing()

{

base.BeginProcessing();

 

 

ApplicationEnvironment.Initialize();

if ((ProxyAddress != null) | (ProxyAddress != ""))

{

WebProxySettings.ProxyEnabled = true;

WebProxySettings.Hostname = ProxyAddress;

WebProxySettings.Port = ProxyPort;

WebProxySettings.Username = ProxyUserName;

WebProxySettings.Password = ProxyPassword;

 

}

else

{

WebProxySettings.ProxyEnabled = false;

}

 

}

 

 

 

 

protected override void ProcessRecord()

{

if (ShouldProcess(Text))

{

ISettingsPersister persister = new RegistrySettingsPersister(Registry.CurrentUser, @"Software\Windows Live Writer");

IBlogCredentials credentials = new BlogCredentials(new SettingsPersisterHelper(persister));

IBlogCredentialsAccessor credentialsAccessor = new BlogCredentialsAccessor("dummy-value", credentials);

 

credentials.Username = UserName;

credentials.Password = Password;

 

MovableTypeClient client = new MovableTypeClient(new Uri(BlogApiEndPoint), credentialsAccessor, PostFormatOptions.Unknown);

 

 

BlogPost MyPost = new BlogPost();

MyPost.Title = Title;

MyPost.Contents = Text;

client.NewPost("dummy-value", MyPost, Published);

 

WriteVerbose("Posted Successfully.");

 

}

 

}

 

}

}

2 Responses to “Out-Blog!”

  1. Andrea Boschin Says:

    good proof, happy to be useful. :-D

  2. Daniele Muscetta Says:

    "Good proof" – "Bella Prova" :-)
    Grande!!!


%d bloggers like this: