Stuart Harris Computing

PowerShell is here to stay

Leave a comment

Jeffrey Snover is the inventor of Monad which later came to be known as PowerShell. His Monad Manifesto written in August 2002, describes the road map and value proposition for PowerShell.  PowerShell’s development has kept surprisingly true to this roadmap.

PowerShell is now a mandatory interface for all of Microsoft’s products.  If you work in a Windows environment, it is high time to embrace the new language and put it to work.

PowerShell fills the gap between writing software applications and using a utility application.  Many years ago, your network administrator was writing C code to help automate management of the network.  Today the office or hosted network is a much more complex beast, and needs higher level commands to automate tasks.

PowerShell solves many of the problems with traditional scripting languages.  It is not so much just a scripting language as an automation framework.  Traditional scripting languages uses the pipe | to transfer information between commands.  PowerShell does this too.  The difference is traditional scripting languages pass plain text.  The text must be formatted then parsed to be digested by the next command.  PowerShell passes .NET objects directly through the pipe to the next command.  This alleviates much of the effort required to implement a complex script.

Here is a command that will find the top 3 most error prone applications.

PS C:\> get-eventlog -entrytype error -log system | group source | sort count -desc | select -first 3 | ft count, name -auto
Count Name
----- ----
96 BTHUSB
18 Microsoft-Windows-Hyper-V-Hypervisor
15 DCOM

Most of the commands above are using aliases to keep the command shorter.  PowerShell is also case sensitive.  This is the same command without the aliases.

PS C:\> Get-EventLog -EntryType Error -LogName System | Group-Object Source | Sort-Object -Descending -Property Count | Select-Object -First 3 | Format-Table Count, Name -AutoSize
Count Name
----- ----
96 BTHUSB
18 Microsoft-Windows-Hyper-V-Hypervisor
15 DCOM

Notice that commands all use a Verb-Noun format.

Commands like this are useful for adhoc tasks.  However, for recurring tasks you will want to keep the command somewhere.

Powershell scripts (.ps1) allow you to develop more complex set of functions and run them again.

Here’s a script that will test execution of a remote PowerShell script.  Notice that there are two mandatory parameters.  A third parameter is read during the execution of the script but does not display the characters typed.

# SimpleRemoteTest.ps1: This is my simple remoting test script
Param(
  [Parameter(Mandatory=$TRUE)] [String] $RemoteServer,
  [Parameter(Mandatory=$TRUE)] [String] $NetworkUsername
)
$ErrorActionPreference = "Stop"
$SecurePassword = Read-Host -AsSecureString "Please enter the password"
$Credential = New-Object System.Management.Automation.PSCredential($NetworkUsername, $SecurePassword)
$Session = New-PSSession -ComputerName $RemoteServer -Credential $Credential
Invoke-Command -Session $Session -Script { Get-ChildItem "C:\" }

Writing scripts is a very helpful way of remembering the commands you need to run.

However, if you start using PowerShell heavily, you will find organising scripts and sharing common script code becomes difficult to manage.

Powershell gives you the ability to define your own modules.  This lets you reuse existing script code and helps organise your scripts.

This is a very simple module but it shows the basic structure of a module.  The file ends with .psm1 and must be placed in a folder of the very same name.  To use the module it must be imported with the Import-Module command.

 <#
.Synopsis
   A very polite greeting function
.DESCRIPTION
   This function writes hello to the console
.EXAMPLE
   Say-Hello "Stuart"
#>
function Say-Hello
{
    [CmdletBinding()]
    Param
    (
        # Name The name of the person to greet
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [string] $Name
    )
    Begin
    {
    }
    Process
    {
        Write-Host "Hello $Name"
    }
    End
    {
    }
}

This is a very basic introduction the PowerShell.  I hope it gives a hint of the abilities is has, and has encouraged you to learn and use it.

Advertisement

Author: Stuart Harris

Windows development leader delivering quality business solutions for over 20 years. I am excited about integrating mobile solutions with core business systems and delivering better software more effectively. I am passionate about Agile delivery, cloud services, the lean startup movement, community, responsive design and social equality.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s