Categories

PowerShell books = Deal of the Day

Manning’s Deal of the Day for 18 November 2014 includes PowerShell in Depth, second edition and PowerShell and WMI

PowerShell is 8 years old this month

Strange as it may seem PowerShell 1.0 was release in November 2006. In that time we’ve had a lot of changes:

- PowerShell support added to all major Microsoft products – except Office!

- Third party adoption of PowerShell – I am now disappointed if a product doesn’t have PowerShell support rather than being surprised that it does

 

Version 2.0

- PowerShell remoting

- Background jobs

- More WMI cmdlets

- More cmdlets

 

Version 3.0

- CIM cmdlets

- CIM sessions

- CDXML

- PowerShell workflow

 

Version 4.0

- Desired State configuration

 

Version 5.)

- Oneget

- PowerShell get

 

The PowerShell community has grown phenomenally:

- powershell.org

- PowerShell Summits in North America & Europe

- User groups across the world

 

Really looking forward to the next 8 years

PowerShell in Depth, second edition in print

I received my copies of the print version of PowerShell in Depth, second edition today.  Amazon shows it being available in a couple of days

Enjoy

Transcripts

The ability to create transcripts of your PowerShell activity is great for keeping track of what you’ve done. They are also very useful when testing as you can keep a record of your results. 

 

There’s one problem though – transcripts don’t work in the ISE.  Try this in PowerShell 4.0 or earlier:

Start-Transcript -Path C:\Temp\tran1.txt
Get-Process
Stop-Transcript

 

You’ll get an error like this:

Start-Transcript : This host does not support transcription.
At line:1 char:1
+ Start-Transcript -Path C:\Temp\tran1.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotImplemented: (:) [Start-Transcript], PSNotSup
   portedException
    + FullyQualifiedErrorId : NotSupported,Microsoft.PowerShell.Commands.Start
   TranscriptCommand

 

Your script will run then you’ll get another error:

Stop-Transcript : This host does not support transcription.
At line:3 char:1
+ Stop-Transcript
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : NotImplemented: (:) [Stop-Transcript], PSNotSupp
   ortedException
    + FullyQualifiedErrorId : NotSupported,Microsoft.PowerShell.Commands.StopT
   ranscriptCommand

 

Now try the same in PowerShell 5.0 and you’ll get this:


Transcript started, output file is C:\Temp\tran1.txt

 

Your script will run and you’ll see this at the end:

Transcript stopped, output file is C:\Temp\tran1.txt

 

Your transcript file will will be created and contain the desired output.

 

Another useful addition to your PowerShell tool kit – transcripts now work in ISE

PowerShell classes – – static classes

A static class is one that you don’t need to create  an instance of the object to use – the [math] class provides many examples e.g.

£> [math]::pi
3.14159265358979

 

You can create a class with static methods using PowerShell classes:

class stest {

static [int] doubleup ([int] $in){

return $in * 2
}

}

 

Its used like this:

£> [stest]::doubleup(2)
4

 

Where could you use this – possibly to resolve look up values for instance when decoding WMI integer values or if you have a series of calculations you’ll be using in several select statements

WMI integer properties – alternative decoding options

 

WMI has many properties where the the value is an integer:

£> Get-CimInstance -ClassName Win32_LogicalDisk | Format-Table DeviceId, DriveType, Size, FreeSpace -a

DeviceId DriveType         Size    FreeSpace
-------- ---------         ----    ---------
C:               3 135810510848 120492625920
D:               5

 

In the example drive type 3 is a standard hard disk and drive type 5 is defined as a compact disk

http://msdn.microsoft.com/en-us/library/aa394173%28v=vs.85%29.aspx

 

 

Remembering these can be a pain – there are a couple of ways to decode these values.

 

You could use a hash table – I showed many examples of this in PowerShell and WMI – www.manning.com/siddaway2

 

$dtype = DATA {ConvertFRom-StringData -StringData @'
3 = Hard Drive
5 = Compact Disk
'@}
Get-CimInstance -ClassName Win32_LogicalDisk |
Format-Table DeviceId, DriveTYpe, @{N='TYpe'; E={$dtype["$($_.DriveType)"]}}, Size, FreeSpace –a

 

 

DeviceId DriveTYpe TYpe                 Size    FreeSpace
-------- --------- ----                 ----    ---------
C:               3 Hard Drive   135810510848 120495980544
D:               5 Compact Disk
  

 

 

Define the hash table via ConvertFrom-StringData . You can then just use the hash table as a look up to convert the numeric value of drive type into a descriptive name.

With WMF 5.0 and PowerShell classes there is another option

 

enum dtype {
HardDrive = 3
CompactDisk = 5
}

Get-CimInstance -ClassName Win32_LogicalDisk |
Format-Table DeviceId, DriveTYpe, @{N='TYpe'; E={[dtype]$($_.DriveType)}}, Size, FreeSpace –a

 

DeviceId DriveTYpe        TYpe         Size    FreeSpace
-------- ---------        ----         ----    ---------
C:               3   HardDrive 135810510848 120496607232
D:               5 CompactDisk

 

Create a enumeration using the enum keyword. The descriptive text CANNOT have spaces (delimited strings don’t work either). You can then substitute the enum value into your calculated field.

DSC Resource Kit wave 8

Wave 8 has arrived and its massive with 48 new resources – that’s a 50%+ increase

http://blogs.msdn.com/b/powershell/archive/2014/10/28/powershell-dsc-reskit-wave-8-now-with-100-resources.aspx

 

New modules include:

  • xExchange
  • xSCDPM
  • xSCOM
  • xSCSMA
  • xSCSR
  • xSCVMM
  • xCredSSP
  • xDismFeature
  • xBitlocker
  • xPendingReboot

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/

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

PowerShell in Depth second edition ebook

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