PowerShell Summit NA 2015 – – Registration open

Registration for the PowerShell summit North America 2015 is open.  Details and registration link  – http://powershell.org/wp/community-events/summit/

Posted in Powershell, Summit | Leave a comment

Cut and paste is not your friend

I was working on some code the other day and as a block of code I needed was very similar to one I already had I did what everyone does & used cut and paste. Unfortunately, I missed out changing one of the variable names in the new block and  spent a long time working out what had gone wrong.

 

Moral of the story – be careful how you copy code

Posted in Powershell | Leave a comment

PowerShell in Depth second edition ebook

The ebook for PowerShell in Depth second edition is available from Manning – http://www.manning.com/jones6/

Posted in Books, Powershell | Leave a comment

JEA ToolKIt helper

JEA – Just Enough Admin – brings Role Based Access Control to Windows. It enables you to delegate specific cmdlets to specific users on specific endpoints.

A tool to help you create and mange JEA configurations is now available form

http://blogs.technet.com/b/privatecloud/archive/2014/10/24/introducing-the-jea-toolkit-helper.aspx

A white paper on JEA is also available from the same link

Posted in PowerShell v5 | Leave a comment

PowerShell classes — using methods

.NET objects usually have methods as well as properties. A method enables you to do something to or with the object.  PowerShell classes can implement methods:

class LastBoot {
[string]$ComputerName
[DateTime]$LastBootime

## methods

[TimeSpan] Uptime([datetime]$lbt)
{
$ts = (Get-Date) – $lbt
return $ts
}

## constructors
LastBoot(){}

LastBoot([string]$computer, [DateTime]$lbt) {
  $ComputerName = $computer
  $LastBootime = $lbt
}

}

$comp = $env:COMPUTERNAME
$lbtime = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $comp |
    select -ExpandProperty LastBootUpTime

$obj = [LastBoot]::new()
$obj | gm

$obj.Uptime($lbtime)

 

A method is defined like this:

[TimeSpan] Uptime([datetime]$lbt)
{
$ts = (Get-Date) – $lbt
return $ts
}

 

Give the type of the return value and the type and name of input parameters. If you don’t give an input type System.Object is assumed.

Write the code to perform the method’s action

use return to return the any values from the method. If your method doesn’t retrun anything then use a return type of [void] in the definition.

You must use return with a method. You can’t just put the object on the pipeline as you would with a function.

PowerShell classes are still a work in progress and you may see changes when we see the next preview

Posted in PowerShell v5 | Leave a comment

PowerShell classes – – overloading constructors

You can usually create a .NET object from a class in one of several ways – no parameters i.e. and empty object up to an object with all of its properties populated as you create it. When you define a class you define the various ways in which it can be created – these are known as its constructors. When you have multiple constructors they are known as overloaded constructors i.e. a number of different ways to create the instance.

 

You have seen the default constructor in the previous post:

 

$obj = [LastBoot]::new()

 

You just use new() without any arguments to create the empty object which you then populate. If you know some of the values before you create the object you can use one of the overloads on the constructor to create the object:

 

$obj2 = [LastBoot]::new($comp, $lbtime)

 

Here you’re supplying the computername and last boot time as you create the object.  The class definition looks like this:

 

class LastBoot {
[string]$ComputerName
[string]$LastBootime

LastBoot(){}

LastBoot([string]$computer, [string]$lbt) {
  $ComputerName = $computer
  $LastBootime = $lbt
}

}

$comp = $env:COMPUTERNAME
$lbtime = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $comp |
    select -ExpandProperty LastBootUpTime

 

$obj = [LastBoot]::new()

$obj.ComputerName = $comp
$obj.LastBootime = $lbtime

$obj

 

$obj2 = [LastBoot]::new($comp, $lbtime)
$obj2

 

Define the two properties as before. You have to explicitly define the default constructor when you are overloading the constructor:

 

LastBoot(){}

 

That says create a new instance of the LastBoot class but don’t set any properties.

 

Our overload:

LastBoot([string]$computer, [string]$lbt) {
  $ComputerName = $computer
  $LastBootime = $lbt
}

 

says create an instance of LAstBoot and I’m going to give you the computer name and last boot time. Map what you’re given to the ComputerName and LastBootime properties as shown.

 

Use the default constructor like this:

$obj = [LastBoot]::new()

 

And the overload like this

$obj2 = [LastBoot]::new($comp, $lbtime)

 

In case you’re wondering why this will useful to know – the simple answer is that you can create DSC resources using PowerShell classes which makes them much easier to write. But before we get to that you need to fully understand how PowerShell classes work.

 

Warning – PowerShell 5.0 is still in preview stage so this could change

Posted in PowerShell v5 | Leave a comment

PowerShell 5.0 – – classes

I’ve shown this method of creating a new object several times:

 

$source = @”
public class LastBoot
{
  public string ComputerName {get; set;}
  public string LastBootime {get; set;}
}
“@

Add-Type -TypeDefinition $source -Language CSharpVersion3

$computer = $env:COMPUTERNAME

$props = [ordered]@{
  ComputerName = $computer
  LastBootime = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $computer |
    select -ExpandProperty LastBootUpTime
}

New-Object -TypeName LastBoot -Property $props

 

Define a class in C# – in this case a simple class that has 2 properties ComputerName and LastBootime which are both strings for simplicity.  Compile the code via Add-Type

 

Define the value of the properties in a hash table and create an object.

 

This approach has a number of benefits – you have given your object a distinct type so you can easily create format and type data for it. Also the properties are strongly typed which means if to try to define a property with a value that isn’t of the correct type or can’t be converted into the correct type the creation will fail.

 

The drawbacks are that you have to use C# to define the class and Add-Type won’t let you redefine the class in the PowerShell session in which you create it.

 

The drawbacks, especially the first one, put most peopel off from using this approach.

 

PowerShell 5.0 has simplified working like this as you can now create classes directly in PowerShell.

 

class LastBoot {
[string]$ComputerName
[string]$LastBootime
}

$obj = [LastBoot]::new()

$obj.ComputerName = $env:COMPUTERNAME
$obj.LastBootime = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $computer |
    select -ExpandProperty LastBootUpTime

$obj

 

Use the class keywaird to start the class definition. The properties are defined in a similar way to advanced functions – note there isn’t a comma after the function.

 

A new instance is created like this:

$obj = [LastBoot]::new()

 

You can’t use New-Object in the latest version of PowerShell 5.0  – I presume that will be added at some thime.

 

You can then populate the properties and output the object.

 

This is just scratching the surface with classes. This whole addition will make working with new objects more flexible as you can easily define what you want your output object to be like and then PowerShell will enforce the property types for you.

In case you were wondering use LastBootime was deliberate  Smile

Posted in PowerShell v5 | Leave a comment