Monthly Archive

Categories

Monthly Archives: November 2014

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.