Monthly Archive

PowerShell Summit NA 2015 speakers

Want to learn a bit more about some of the speakers at the PowerShell Summit?  Head over to the Scripting Guy blog - http://blogs.technet.com/b/heyscriptingguy/ – to see a series of posts giving you some background, and pictures, of a number of speakers.

Query vs Filter

I’ve tended to advocate using the –Filter parameter rather than the –Query parameter with the CIM (and WMI) cmdlets but a recent post on the Windows Management Infrastructure blog has me questioning that decision.

 

Using Measure-Command I tried various pairs of commands – such as:

 

Measure-Command {Get-CimInstance -ClassName Win32_Directory -Filter "Name = 'C:\\Test2'"}

 

Measure-Command {Get-CimInstance -Query "SELECT * FROM Win32_Directory WHERE Name = 'C:\\Test2'"}

 

The results weren’t conclusive but it seems that at worst there is no significant difference between the approaches and at best using a query is significantly faster. 

 

At the moment my best advice would be use the –Filter parameter if you want to reduce typing but try –Query if speed becomes your main issue.

MVP renewal

April 1st comes round again and its time for the MVP renewals. I received my renewal email this afternoon – I feel highly honoured that Microsoft have bestowed an MVP award for 8 straight years.

 

Thank you

PowerShell Europe 2015 Agenda

As has been announced registration for the PowerShell Summit Europe 2015 opens on 27 February 2015. http://powershell.org/wp/2015/02/20/powershell-summit-europe-registration/

 

Other details on the Summit can be found here:  http://powershell.org/wp/community-events/summit/powershell-summit-europe-2015/

 

The agenda for the Summit can be found on the event site for the Summit http://eventmgr.azurewebsites.net/event/home/PSEU15

Change to DSC Resource Kit release strategy

The PowerShell team blog http://blogs.msdn.com/b/powershell/archive/2015/02/13/new-and-improved-resource-kit.aspx details the new resources and bug fixes in wave 10 of the DSC resource kit. This is a much smaller release than the of late but includes new resources for initializing and formatting disks

 

IMPORTANT NOTE

 

One point in the “small print” is important to note. This is the last release of these resource as a big zip file for download from the TechNet script gallery. In future these resources will only be available through the PowerShell gallery. Use Find-DscResource to discover the resources available in the gallery and Update-Module & Install-module to get the modules containing resources from the gallery.

DSC Resource Kit Wave 10

The next release (wave 10) of the Desired State Resource Kit is available. You can download the whole of the resource kit from https://gallery.technet.microsoft.com/scriptcenter/DSC-Resource-Kit-All-c449312d

This version includes the IIS resources that were missing in wave 9

WMF 5.0 February 2015 preview

A new preview of Windows Management Framework 5.0 (PowerShell) is available.

 

See http://blogs.msdn.com/b/powershell/archive/2015/02/18/windows-management-framework-5-0-preview-february-2015-is-now-available.aspx

for details.

 

The release notes indicate which new features are considered stable and which are still regarded as experimental and therefore subject to change.

 

Downloads are available for Windows 8.1 (x32 and x64), Windows Server 2012 R2  Windows 2012. You will need .NET 4.5 installed.

Copy a file with WMI

A question came up on the forum about copying files with CIM (WMI). I normally use Copy-Item rather than CIM as its easier. The questioner was using CIM_LogicalFile when I’ve normally used CIM_DataFile so I decided to take a look at the class. In reality the two classes are very similar and CIM-datafile could be substituted for CIM_LogicalFile in the code that follows.

 

The obvious starting point is to use the Copy method on the CIM_LogicalFile class

 

$files = Get-WmiObject -Class CIM_LogicalFile -Filter "Path = '\\Test\\' AND Extension = 'txt'"

foreach ($file in $files) {
$newfile = "C:\Test2\$($file.FileName).$($file.Extension)"
 
$file.Copy($newfile)

}

 

Couple of points to note. In the Path part of the filter you have to escape the \ delimiter.  Extension doesn’t include the ‘.’

You have to give the full path – including file name - to the loaction to which you want to copy the file. In this case you don’t have to escape the \ delimiter. Consistency is a wonderful thing and usually absent from WMI.

 

You can also use Invoke-WmiMethod

 

$files = Get-WmiObject -Class CIM_LogicalFile -Filter "Path = '\\Test\\' AND Extension = 'txt'"

foreach ($file in $files) {
$newfile = "C:\Test2\$($file.FileName).$($file.Extension)"
 
Invoke-WmiMethod -InputObject $file  -Name Copy -ArgumentList $newfile

}

 

OR

use the new CIM cmdlets

 

$files = Get-CimInstance -ClassName CIM_LogicalFile -Filter "Path = '\\Test\\' AND Extension = 'txt'"

foreach ($file in $files) {
$newfile = "C:\Test2\$($file.FileName).$($file.Extension)"
 
Invoke-CimMethod -InputObject $file  -MethodName Copy -Arguments @{Filename = $newfile}

}

 

In this case you have to give the argument name for the method as well as its value. You can discover the method parameters using Get-CimClass

 

$class = Get-CimClass CIM_LogicalFile

£> $class.CimClassMethods["Copy"].Parameters

Shutting down machines in parallel

My test lab is a set of virtual machines running on a Lenovo w1510 laptop. When I’ve finished working for the day I want to shut down the virtual machines and the laptop. I may have anywhere between 2 and 8 (or more) VMs running so scripting the shutdown helps a lot.

 

Machines can be shutdown independently so this is an action that is ideal for parallel execution through a workflow.

 

workflow stop-allvms {
$vms = Get-VM | where State -eq "Running" 
foreach -parallel ($vm in $vms) {
Stop-Computer -PSComputerName $vm.Name -Force -Verbose
}
}

stop-allvms

 

Get the running VMs. use foreach –parallel to run stop-computer against each VM. Notice I’ve had to change the parameter on Stop-Computer from –ComputerName to –PSComputerName

 

Another little workflow that makes life easier. I looks like workflows aren’t going to be the game changer that we originally thought but they do add some interesting options.

Testing AD replication

I thought that using a workflow with its foreach –parallel construct would be a good way to test AD replication.

 

I found that I got double the results – the foreach seemed to go to each machine twice.

 

Eventually decided to perform the task sequentially

Get-ADDomainController -Filter * |
foreach {
  Get-ADReplicationUpToDatenessVectorTable -Target $($psitem.Hostname) -Partition * |
  select Server, LastReplicationSuccess, Partition,
  @{N='Partner'; E={(($_.Partner -split ",")[1]).Remove(0,3)}}, USnFilter
} | ft –a

 

There are a number of cmdlets for working with AD replication that are worth investigating