Today With PowerShell – Wednesday, 9th November 2016

In an effort to contribute to the PowerShell community I have signed up at PowerShell.org . There’s a good general forum for PowerShell Q&A. Head over there if you’ve got questions. You never know, I might have an answer for you!

 

Posted in PowerShell | Leave a comment

Today With PowerShell – Saturday 22nd October 2016

Whilst creating a script I wanted to be able to trace what was happening as the script ran. In my VBScript days I would have set up a flag to determine if I needed verbose output and then tested for it. Even with cut and paste this can be laborious. In PowerShell we have Write-Verbose, but how do we turn it on? The -Verbose parameter is one of the PowerShell ‘Common Parameters’. To enable these we need a [CmdletBinding()] line as the first executable line in our script.

Have a look at PowerShell – Using Common Parameters for a great overview.

Posted in PowerShell | Leave a comment

Today With PowerShell – Saturday 1st October 2016

No code in this post, just a pointer to a great free resource for learning PowerShell. The Microsoft Virtual The Microsoft Virtual Academy. There are a couple of couple of courses I can recommend; one for general PowerShell learning and one for those using PowerShell in Enterprises.

Getting Started with PowerShell 3.0 Jump Start is a good starting point, even though version 3 is no longer the most recent version.

Using PowerShell for Active Directory is aimed at helping those familiar with PowerShell to use their skills automating AD tasks.

There are a number of other courses available for PowerShell on the site, as well as a whole host of other Microsoft technologies.

Posted in PowerShell | Leave a comment

Review – War Horse

Disclaimer – I haven’t read the book or seen the stage show. This review is purely based on the recently broadcast film version.

I wasn’t expecting to like War Horse. It was showing on TV and was kind of on by default whilst we decided what else to watch. I think horses are beautiful animals, but I’m not a fan. I had a vague idea of the plot, knew it was based on a novel by Michael Morpurgo.

So what do you get when you combine Steven Spielberg as director with a story by a children’s author, a cast containing faces recognised around the world (Benedict Cumberbatch, David Thwelis, Tom Hiddleston to mention a few) and music by John Williams? You get an excellent film – and that’s what motivated me to post this review.

War Horse is not always an easy film to watch – it is rated 12A in the UK. Those of us with real experience of barbed wire gasped at one point. It does also have a few lighter moments. Younger viewers will have many questions about World War I – some adults too, as the film shows but doesn’t tell. There are both human and animal characters; the animals aren’t characters in a cutesy Babe kind of way but they’re characters all the same. The film doesn’t shy away from the senselessness and waste of war; it even hints at issues such as PTSD, but it isn’t gratuitous or gory. At its heart the film explores the relationships between Joey the horse and the various characters he encounters (English, German, French and Equine) against the backdrop of his early life in Devon and then the war in Europe; and if the end isn’t happy for everyone it’s worth watching nonetheless.

Some might think that the plot War Horse is too fantastic, after all only a few tens of thousands of the millions of horses and donkeys that took part in World War I survived. If so have a look at the story of Warrior, a horse born and bred here on the Isle of Wight. Warrior’s story is not that of Joey from War Horse but he did experience some the kinds of action that War Horse depicts. Warrior was quite a celebrity in his time and a book was written about him in 1934. You can find a statue of Warrior and his owner Jack Seely in the gardens of Carisbrooke Castle.

Obligatory Amazon Product Links:
War Horse on DVD
War Horse Novel (paperback, hardback, kindle)
Warrior: The Amazing Story of a Real War Horse paperback
Warrior The Real War Horse – Presented by Brough Scott DVD

Posted in Film, Review | Leave a comment

Today With PowerShell – 3rd September 2016

Today I used PowerShell with the TagLibSharp library to fix, then break, then fix again the ID3 tags on some of my music files.

We buy a lot of compilation CDs here and tend to end up with correctly tagged but unhelpfully named files. Using TagLibSharp I can construct the filenames I need.


$taglib = "C:\bin\taglib-sharp.dll"
[System.Reflection.Assembly]::LoadFile($taglib)
$sharedMusic = "\\server\Shared Documents\Shared Music\"
$mediaArtist = "Various Artists"
$mediaAlbum = "Now Thats What I Call PowerShell"
$albumPath = "$($sharedMusic)$($mediaArtist)\$($mediaAlbum)"
$mediaFiles = Get-ChildItem -LiteralPath $albumPath -Filter '*.mp3'

ForEach ($file in $mediaFiles) {

    $mediaFile = [TagLib.File]::Create($file.FullName)
    $titleTag = $mediaFile.Tag.Title
    $tag = $titleTag -split " / "
    $fileArtist = $tag[0]
    $fileTitle = $tag[1]
    $mediaFile.Tag.Artists = $fileArtist
    $mediaFile.Tag.AlbumArtists = $mediaArtist
    $mediaFile.Tag.Title = $fileTitle
    $mediaFile.Save()
    $newFileName = "$($albumPath)\$(($mediaFile.Tag.Track).ToString("00"))-$($fileTitle).mp3"
    Rename-Item -Path $file.FullName -NewName $newFileName

} 

Hopefully this is all fairly clear but I’ll explain anyway.

In line 01 I establish a variable for the path to the TagLibSharp library file on my local PC. I load the library in line 02. Lines 03 to 06 set up some variables that build a path to the particular folder containing the files I want to fix. Line 07 grabs me a collection of all the MP3s in the folder, which I then iterate through. Line 11 gives me an object for the current file which surfaces the tags for the file. In my particular instance I want to fix the Title tag and rename the file based on it’s new title, as well as fixing the track artist. Lines 10 to 16 grab the tags and fix them. I commit the changes in line 19. In line 20 I create a new file name and rename the file in line 21. Simple!

TagLib: http://taglib.github.io/

TagLibSharp: https://github.com/mono/taglib-sharp/

Posted in PowerShell | Leave a comment

PowerShell – the .Net Factor

PowerShell is a command shell and a scripting language. It easily outclasses both VBScript and the DOS-derived command shell available in Windows; but one of the areas that takes a bit of getting used to is the availability of the .Net Framework. This leads to some interesting combinations when trying to solve particular problems.

VNC is a great piece of software. Some of the work I do involves computers where VNC is is used as a remote support tool. In the era of UAC using VNC has its challenges, mainly due to the fact that UAC-elevated programs run in a different session to the session of the current user. User-initiated connections from VNC in application mode to a listening viewer can only interact with processes from that session. UAC prompts don’t appear in the VNC viewer, so remote support staff can’t work in an elevated context. The answer to this is to run VNC as a system service, which has access to all sessions. For various non-technical reasons it’s not acceptable to leave the VNC service running all of the time – the user needs to be seen to request the remote connection. The users don’t have permission to start the VNC service; they can however initiate a connection from an application-mode VNC server to a listening VNC viewer on a support person’s computer.

Finally, here is where PowerShell and .Net come in to the picture. Using .Net we can identify which computer is connected, kill off any VNC server processes using WMI, stop any running service instances, start our own server instance and start a VNC viewer connection to it.

The script to do this is below:


<#
VNC Helper
Detect incoming VNC connection port 5500
Kill instance on remote box
start service
connect
#>


Function Get-ActiveTCPConnections {            
[cmdletbinding()]            
param(            
)            
            
try {            
    $TCPProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()            
    $Connections = $TCPProperties.GetActiveTcpConnections()            
    foreach($Connection in $Connections) {            
        if($Connection.LocalEndPoint.AddressFamily -eq "InterNetwork" ) { $IPType = "IPv4" } else { $IPType = "IPv6" }            
        $OutputObj = New-Object -TypeName PSobject            
        $OutputObj | Add-Member -MemberType NoteProperty -Name "LocalAddress" -Value $Connection.LocalEndPoint.Address            
        $OutputObj | Add-Member -MemberType NoteProperty -Name "LocalPort" -Value $Connection.LocalEndPoint.Port            
        $OutputObj | Add-Member -MemberType NoteProperty -Name "RemoteAddress" -Value $Connection.RemoteEndPoint.Address            
        $OutputObj | Add-Member -MemberType NoteProperty -Name "RemotePort" -Value $Connection.RemoteEndPoint.Port            
        $OutputObj | Add-Member -MemberType NoteProperty -Name "State" -Value $Connection.State            
        $OutputObj | Add-Member -MemberType NoteProperty -Name "IPV4Or6" -Value $IPType            
        $OutputObj            
    }            
            
} catch {            
    Write-Error "Failed to get active connections. $_"            
}           
}

$connections  = Get-ActiveTCPConnections
$vncconnection = $connections | Where-Object { $_.localport -eq '5500' }
$remoteip = $vncconnection.RemoteAddress
$remoteip
$remotename = [System.Net.Dns]::GetHostEntry($remoteip).HostName
$remotename = $remotename.ToUpper()
$vncconnection
$remotename
Get-service -ComputerName $remotename *vnc* | Stop-Service
(Get-WmiObject Win32_Process -ComputerName $remotename | ?{ $_.ProcessName -match "vncserver" })[0].Terminate()
Start-Sleep 5
Get-service -ComputerName $remotename *vnc* | Start-Service
&'C:\Program Files\RealVNC\VNC Viewer\vncviewer.exe' $remotename

Get familiar with the .Net framework if you want to get the best out of PowerShell.

Posted in IT, PowerShell | Leave a comment

What I use – Deployment of Windows tablets

Some of the work I do involves the deployment of Windows devices. Desktops, laptops and tablets running Windows 7, 8.1 and 10. The best software tool to use is definitely the Microsoft Deployment Toolkit (MDT). The most difficult form factor to deploy to is the tablet. These devices typically lack the wired Ethernet port needed for Preboot eXecution  Environment start up. Rather than keep a variety of different docking stations and network adapters I use the Startech USB Hub with Ethernet . This device allows me a fixed network connection plus enough USB ports for a bootable USB memory pen, keyboard and mouse. I’ve used these with the Microsoft Surface Pro 4, its younger cousin the Microsoft Surface 3, the HP ElitePad 900 and the HP Elite X2 1012. The only slight drawback with the hub is the USB cable connecting the hub to the tablet; it’s a proprietary connector at the hub end that dislodges more often than I’d like.

 

Posted in IT | Leave a comment