Using PowerShell to SSH into your openelec (XBMC) mediacenter

A while ago I finally gave up on Windows Media Center because of too many issues, mainly video driver in combination with HDCP sillyness. I’m over it now, having found XBMC as a more than worthy alternative. XBMC exists for many platforms, including Windows, OS X, Linux and some more. As I already had a mediacenter PC dedicated to this task (ASRock Vision 3D, great little machine, and looks OK in a HiFi rack) I decided to go for a full install instead of booting from a USB stick. And I also decided to ditch Windows as the underlying operating system, since there was actually no real point in still having Windows running on it if all it does is show movies, series and play music. There’s a nice, small and very efficient community build of XBMC called openELEC. You can install it to a variety of platforms, including common x86 based PCs, Raspberry Pi and Apple TV (the latter has to be “flexibilized” first). My install took around 15 minutes, and I was watching my first movie after another 15 minutes of initial configuration…

So back to SSH. Why would I want to SSH into my mediacenter? Easy – if I add new media, it has to be added to the media library. I could of course do this onscreen, but sometimes I rip a (legally owned, of course!) DVD on my PC and copy it to the NAS where my media files are stored, which means I’m not in front of my TV. Or the TV could be in use by someone else. The answer is easy – media scanning can be triggered by issuing the command “xbmc-send –a “xbmc.updatelibrary(video)” from the shell. From Windows that’s easy enough, just fire up putty or any other ssh client, log in, send the command, log off and be done:

And it’s not like I have a problem doing things this way, but I had always wondered … is it possible to do this from PowerShell? The answer to any question like “Is it possible to do X in PowerShell?” is usually “Yes, of course!”. The real question is “How difficult is it to do X in PowerShell”. As it turns out, not so difficult at all.

First, you need SSH functionality in PowerShell. No way around that, and no, this is not included in PowerShell. I’m using PowerShell on Windows 8, which means the latest version of PowerShell (v3, in fact). There is however a ready to use SSH Module for PowerShell, which can be found here – the download link is near the bottom of the page.

Once the module is loaded, all you do is basically the same – open the SSH session:

… and send the command:

To verify that something happened you can “cat” or “tail” the xbmc.log file. Since you don’t actually enter an interactive SSH session, you can’t use “tail –f“. Use it without the “-f” parameter instead, it will show you the last couple of lines which usually is enough. Repeat if needed:

 

People who know me also know that I don’t like very much typing the same stuff over and over again. This is actually one of the reasons I started coding, way back REXX on IBM Hosts and OS/2, then gradually made my way through VBScript, VB6, VB.Net, C# and these days use mainly PowerShell and sometimes C#. So what I did was create a script that has some functions in it:

function Connect-XBMC
{
    New-SshSession -ComputerName mediacenter -Username root -Password XXXXXXXX
}
 
function Get-XBMCLog
{
    Invoke-SshCommand -ComputerName mediacenter -Command "tail /storage/.xbmc/temp/xbmc.log"
}
 
function Clean-XBMCVideoLibrary
{
	Invoke-SshCommand -ComputerName mediacenter -Command 'xbmc-send --a "xbmc.cleanlibrary(video)"'
}
 
function Clean-XBMCAudioLibrary
{
	Invoke-SshCommand -ComputerName mediacenter -Command 'xbmc-send --a "xbmc.cleanlibrary(audio)"'
}
 
function Update-XBMCVideoLibrary
{
	Invoke-SshCommand -ComputerName mediacenter -Command 'xbmc-send --a "xbmc.updatelibrary(video)"'
}
 
function Update-XBMCAudioLibrary
{
	Invoke-SshCommand -ComputerName mediacenter -Command 'xbmc-send --a "xbmc.updatelibrary(audio)"'
}

I then “dot-source” the script in PowerShell, which gives me instant access to all the functions in the script. Then it’s a simple matter to just call Connect-XBMC and Update-XBMCVideoLibrary:

A normal video scan takes a little over half a minute, so after that time I call “Get-XBMCLog”:

 

So essentially while I can still use SSH, this is easier and faster. It also lets me script and automate things in an environment I know – Windows and PowerShell. The target isn’t a Windows machine, doesn’t have PowerShell on it. So what? No reason not to script it using PowerShell!

One thought on “Using PowerShell to SSH into your openelec (XBMC) mediacenter

  1. Pingback: XBMC/OpenELEC: Getting started | winblog

Leave a Reply

Your email address will not be published. Required fields are marked *