PowerShell for Windows Admins


October 25, 2014  1:20 PM

PowerShell classes – - overloading constructors

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

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

October 25, 2014  10:16 AM

PowerShell 5.0 – - classes

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

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


October 25, 2014  3:13 AM

PowerShell 5 – zip and unzip

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

One the extras in PowerShell 5.0 is a couple of cmdlets for workign with zip archives. Actually, you’ll find they are PowerShell advanced functions if you look in the module which you’ll find at C:\Windows\System32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive

 

You get 2 cmdlets:

£> Get-Command *archive | ft CommandTYpe, Name -a

CommandType Name
———– —-
Function Compress-Archive
Function Expand-Archive

 

To compress

$files = Get-ChildItem -Path C:\Scripts -Filter *.csv | select -ExpandProperty Fullname
Compress-Archive -Path $files -DestinationPath C:\Scripts\t1.zip -CompressionLevel Optimal

 

or a single file

 

Compress-Archive -Path c:\scripts\test.csv -DestinationPath C:\Scripts\t2.zip -CompressionLevel Optimal

 

To uncompress

Expand-Archive -Path C:\Scripts\t1.zip  -DestinationPath c:\scripts

 

if you need to overwrite files:

 

Expand-Archive -Path C:\Scripts\t1.zip  -DestinationPath c:\scripts -Force


October 22, 2014  2:39 PM

Run with PowerShell

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Came across  something new today – Run with PowerShell.

 

if you have PowerShell 3.0 or later installed – right click on your script and select “Run with PowerShell”

 

A few rules though – The script can’t take parameters or output anything to the prompt. You can’t interact with the script or the console window.

 

Execution policy is set to Bypass – not sure I like that idea  – unless the ExecutionPolicy is Allsigned in which case only signed scripts can be run this way.  See about_Run_With_PowerShell for more details


October 22, 2014  11:59 AM

DSC for Exchange

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

A series of posts on using the Exchange DSC resources – starts here

http://blogs.technet.com/b/mhendric/archive/2014/10/17/managing-exchange-2013-with-dsc-part-1-introducing-xexchange.aspx


October 20, 2014  10:42 AM

Upgrading PowerShell

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The Scripting Guy has started a series on upgrading the version of  PowerShell you run.

My article in the series is out today – http://blogs.technet.com/b/heyscriptingguy/archive/2014/10/20/should-i-upgrade-to-latest-windows-powershell-version.aspx


October 18, 2014  1:49 PM

DSC Resource Kit Wave 8 coming?

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Looks like the next wave of the DSC resource kit is on its way – a set of resources for Exchange 2013 have been published – https://gallery.technet.microsoft.com/office/xExchange-PowerShell-1dd18388 with a wave 8 tag.

 

I’ve been waiting for the Exchange resources – they’re going to make my life soooooo much easier


October 15, 2014  11:44 AM

Default formatting

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

If you run get-process you will see something like this for each process

£> Get-Process | select -f 1

Handles NPM(K) PM(K) WS(K) VM(M)  CPU(s)   Id ProcessName
——- —— —– —– —–  ——   — ———–
80      7   960  4096    44         1560 armsvc

 

You’ll get the same display if you use

£> Get-Process | select -f 1 | ft

 

If you ask for a list – you get something different

£> Get-Process | select -f 1 | fl
Id      : 1560
Handles : 80
CPU     :
Name    : armsvc

 

Looking at all of the data for a single process give you this:

£> Get-Process | select -f 1 | fl *
__NounName                 : Process
Name                       : armsvc
Handles                    : 80
VM                         : 46186496
WS                         : 4194304
PM                         : 983040
NPM                        : 7136
Path                       :
Company                    :
CPU                        :
FileVersion                :
ProductVersion             :
Description                :
Product                    :
Id                         : 1560
PriorityClass              :
HandleCount                : 80
WorkingSet                 : 4194304
PagedMemorySize            : 983040
PrivateMemorySize          : 983040
VirtualMemorySize          : 46186496
TotalProcessorTime         :
BasePriority               : 8
ExitCode                   :
HasExited                  :
ExitTime                   :
Handle                     :
MachineName                : .
MainWindowHandle           : 0
MainWindowTitle            :
MainModule                 :
MaxWorkingSet              :
MinWorkingSet              :
Modules                    :
NonpagedSystemMemorySize   : 7136
NonpagedSystemMemorySize64 : 7136
PagedMemorySize64          : 983040
PagedSystemMemorySize      : 89712
PagedSystemMemorySize64    : 89712
PeakPagedMemorySize        : 1212416
PeakPagedMemorySize64      : 1212416
PeakWorkingSet             : 4300800
PeakWorkingSet64           : 4300800
PeakVirtualMemorySize      : 50155520
PeakVirtualMemorySize64    : 50155520
PriorityBoostEnabled       :
PrivateMemorySize64        : 983040
PrivilegedProcessorTime    :
ProcessName                : armsvc
ProcessorAffinity          :
Responding                 : True
SessionId                  : 0
StartInfo                  : System.Diagnostics.ProcessStartInfo
StartTime                  :
SynchronizingObject        :
Threads                    : {1564, 1572}
UserProcessorTime          :
VirtualMemorySize64        : 46186496
EnableRaisingEvents        : False
StandardInput              :
StandardOutput             :
StandardError              :
WorkingSet64               : 4194304
Site                       :
Container                  :

 

Notice that you don’t see anything corresponding to  any of these fields from the default display – NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)

 

That’s because they are calculated by PowerShell when the data is formatted to display.  See about_Format.ps1xml for more details


October 15, 2014  1:15 AM

PowerShell Summit Europe 2014 – All videos available

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

All of the recordings from the recent PowerShell Summit in Amsterdam are now available through the PowerShell.org channel on youtube. The playlist for the Summit is https://www.youtube.com/playlist?list=PLfeA8kIs7Coehjg9cB6foPjBojLHYQGb_

 

Thank you again to the speakers, and attendees, who made for a wonderful first Summit in Europe and more thanks to the people who donated to our appeal to raise funds for the recording equipment.


October 13, 2014  2:22 PM

WMI Associations

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
CIM, Powershell, WMI

 

I saw a question regarding finding the Win32_NetworkAdapter instance using the matching Win32_NetworkAdapterConfiguration starting point.  This answers the “which adapter has an IP address of X” type question.

 

The Index property on a Win32_NetworkAdapterConfiguration instance has the same value as the DeviceId property on the corresponding Win32_NetworkAdapter.

 

An alternative is to use the ASSOCIATORS WQL keyword.

 

That approach get s a bit messy but looks like this:

 

$query = “ASSOCIATORS OF {Win32_NetworkAdapterConfiguration.Index=’18′} WHERE RESULTCLASS = Win32_NetworkAdapter”
Get-WmiObject -Query $query

 

The CIM cmdlets get a bit better

 

$config = Get-CimInstance win32_networkadapterconfiguration -Filter “Index = 18″
Get-CimAssociatedInstance -InputObject $config -ResultClassName Win32_NetworkAdapter

 

Much simpler and you avoid the WQL


Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: