PowerShell for Windows Admins


April 3, 2014  1:02 PM

Windows Management Framework V5

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The Windows Server blog has an announcement, and download link, for WMF v5.

http://blogs.technet.com/b/windowsserver/archive/2014/04/03/windows-management-framework-v5-preview.aspx

The headline items are OneGet and cmdlets for managing network switches that conform to the Certified for Windows Network program.

OneGet is a way to discover, and install, software packages. In this release you can search for and install software from Chocolatey repositories.

There are also some fixes and enhancements to DSC to improve performance.

April 2, 2014  11:43 AM

CIM snippets–working with file system

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

The latest instalment from the WMI team on using PowerShell and the CIM cmdlets is available – http://blogs.msdn.com/b/wmi/archive/2014/03/28/performing-management-tasks-using-cim-cmdlets-4-files-and-folders.aspx

This time round the examples are to do with working with the file system – files, folders and shares.

If you’ve worked with WMI you’ll be aware of that very often you get 2 classes one with a prefix of CIM (base class from DMTF definition) and one with Win32 prefix which is the Microsoft implementation. They 2 classes are often identical though the Win32 class may have additions.

The WMI class for working with files is different – it only has a CIM version CIM_DataFile.

The first example in the post is about renaming a file. A much simpler coding of the task would be:

Get-CimInstance -ClassName CIM_Datafile -Filter “Name = ‘C:\\Test\\Names.txt’” | Invoke-CimMethod -MethodName Rename -Arguments @{FileName = ‘C:\\Test\\OldNames.txt’}

A couple of points to note:

- when dealing with file paths all \ characters must be doubled. This is because \ is a WMI escape character so you need to escape it to use it literally.

- Invoke-CimMethod uses a hash table for the method arguments with the argument name as the key – this takes away any of the argument order issues you see with Invoke-WmiMethod)

One perennial problem for administrators is users putting their own files on the organization’s file servers.  Want to know if there any files of a specific type in a folder?

Get-CimInstance -ClassName CIM_Datafile -Filter “Extension = ‘txt’ AND Path = ‘\\test\\’”

If you leave the path out of the filter then all files on the drive will be searched – could take a while.  Being specific in your filter will save you a lot of time.

Want to find all the mp3 files on a drive?

Get-CimInstance -ClassName CIM_Datafile -Filter “Extension = ‘mp3′”

You can’t create files and folders with CIM (or WMI) but you can create shares

 

$margs = @{
Path = ‘C:\Test’
Name = ‘Test2April’
Description = ‘TestShare’
Type = [uint32]0
}

Invoke-CimMethod -ClassName Win32_Share -MethodName Create -Arguments $margs

Create the hash table of arguments separately  – its easier to read. Bizarrely this time you don’t need to escape the \ in the path

You can see the shares on a system like this:

Get-CimInstance -ClassName Win32_Share

CIM may not be you first port of call when working with the file system but it can be useful – especially on remote systems.

You can find out much more about using CIM to work with the file system in chapetr 8 of PowerShell and WMI – www.manning.com/siddaway2


April 1, 2014  8:14 AM

MVP award renewed

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Received an email this afternoon renewing my PowerShell MVP award for another year.  This is a great honour and I’d like to thank you for helping to make this possible by reading my blog posts.


March 31, 2014  4:03 AM

DSC resource kit wave 3

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Desired State Configuration (DSC) is the new server configuration and compliance mechanism that ships with PowerShell 4.0 and Windows Server 2012 R2.

Resources are the way you perform configuration. Now you have more options with the release of wave 3 of the DSC resource kit.

Details from http://blogs.msdn.com/b/powershell/archive/2014/03/28/dsc-resource-kit-wave-3.aspx


March 31, 2014  2:18 AM

Last few days to register

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Reaching the end of March there’s not much time left to register for the PowerShell Summit NA 2014 -

http://powershell.org/wp/community-events/summit/powershell-summit-north-america/summit-registration/

as its coming up fast.

See you there.


March 28, 2014  2:35 PM

Discovering namespaces

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Next point on the journey of discovery through CIM is finding the namespaces installed on a machine. I showed how to do this using Get-WmiObject in PowerShell and WMI but this time round decided to come up to date and use Get-CimInstance

function get-cimnamespace {

param (

[string]$namespace = ‘root/cimv2′

)

Get-CimInstance -ClassName __NameSpace -Namespace $namespace |

select @{N=’Name’; E={“$($_.CimSystemProperties.NameSpace)/$($_.Name)”}}

}

This simply searches for instances of the __NameSpace class in a given starting name space. Default is root/cimv2. By using select-object to create a calculated field I can append the name of the namespace to the current namespace to get the full path.

Next time I’ll show how to use recursion to dig through the namespaces we’re discovering to find any namespaces they contain.

 


March 27, 2014  2:49 PM

Improving CIM/WMI method discovery

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I recently showed how to create a function that could be used to simplify the use of Get-CimClass.

In this version I’ve added some features:

- parameter validation

- namespace

- try-catch round getting the class information.

This turns the code into:

function Get-CimMethod {

[CmdletBinding()]

param (

[Parameter(Mandatory=$true)]

[Alias("Class")]

[ValidateNotNullOrEmpty()]

[string]$classname,

[ValidateNotNullOrEmpty()]

[string]$namespace = ‘root/cimv2′,

[Alias("Name")]

[ValidateNotNullOrEmpty()]

[string]$methodname

)

try

{

$class = Get-CimClass -Namespace $namespace -ClassName $classname -ErrorAction Stop

}

catch

{

Throw “Class: $classname NOT FOUND”

}

if ($methodname)

{

$class.CimClassMethods[$methodname].Parameters

}

else

{

$class.CimClassMethods

}

}

The module and function can be used like this:

Import-Module CimInvestigation -Force

Get-Command -Module CimInvestigation

Get-CimMethod -classname Win32_Process

Get-CimMethod -classname Win32_Process -methodname Create

Get-CimMethod -classname Win32_Process -methodname Create -namespace root/cimv2

 


March 25, 2014  2:21 PM

Deal of the Day–26 March

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tomorrow – 26 March – several PowerShell books will feature in Manning’s Deal of the Day:

PowerShell in Depth 2E

PowerShell and WMI

PowerShell Deep Dives

 

All highly recommended and full of PowerShell goodness


March 24, 2014  12:30 PM

A little bit of community

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

One thing I really like about the PowerShell community is the number of people who share information and the way that allows us to incrementally increase our knowledge and skills. This can lead to the situation where you can take ideas from two very separate people, from two different times and put them together to get something different.

I was reading my friend Jason’s blog post – http://powershell.org/wp/2014/03/23/new-tools-in-my-toolbox/ and when I got to the bit about using PowerShell to start Internet Explorer:

PS> Start iexplore www.bing.com

Start is an alias for Start-Process and I remembered something Ed Wilson (The Scripting Guy) once told me about using single letter aliases for his common activities.

Many people are aware of aliases within PowerShell and how they can cut down typing when you are working interactively. What isn’t so widely realised is that you can define aliases for non-PowerShell commands.

So in the interests of absolute brevity let define a new alias S that provides an even shorter way to use Start-Process:

New-Alias -Name S -Value Start-Process

You can test it by trying

s notepad

You should get an instance of notepad started.  This makes starting Internet Explorer as simple as:

s iexplore www.bing.com

Alternatively, you can cut out the use of Start-Process. Define an alias for Internet Explorer

New-Alias -Name I -Value ‘C:\Program Files (x86)\Internet Explorer\IEXPLORE.EXE’

Try it using

i

And a new instance of IE will start

This means that you can start IE in a particular web site very quickly for example:

i www.bing.com i amazon.co.uk

Single letter aliases are a great way to minimise your typing. If you want them to be always available then add them to your profile


March 23, 2014  4:51 AM

Discovering CIM/WMI methods and parameters

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

As you’ve probably gathered I spend a lot of time working with, and investigating, CIM (WMI) classes. CIM and WMI will be treated as synonymous for these articles.

If you want to discover the methods available on a CIM class you can do this:

$class = Get-CimClass -ClassName Win32_Process

$class.CimClassMethods

 

If you want to drill down into an individual method you then have to do this:

$class.CimClassMethods["Create"].Parameters

 

We end up with a two stage process. Firstly discover the available a methods and then discover the parameters on the method.

In a busy session this can turn into a lot of typing. You can short-circuit some that if you using the Get-CimMethodParameter from the CIM Utilities modules that can be downloaded from http://gallery.technet.microsoft.com/scriptcenter/CimUtilities-56d9dd99

 

To my mind it only supplies an answer to the last part of the problem. There is a hidden method – Get-CimMethod – that does the first step but as its not published it doesn’t help us.

I decided that its time to re-write the module to work the way I want. This provides an opportunity to go over the functionality of Get-CimClass, Advanced functions and modules.

To define my problem:

I want a cmdlet that:

provides a list of methods on a WMI class

provides the parameters of a specific method

the standard output of Get-CimClass is acceptable

work with any namespace

work with remote machines

not concerned about pipeline input at this stage (may add later)

not concerned about outputting parameters as a hash table ( though may add later)

 

I’ll start with a simple function to perform the first two requirements.

function Get-CimMethod {

[CmdletBinding()]

param (

[string]$classname,

[string]$methodname

)

if ($methodname)

{

$class = Get-CimClass -ClassName $classname

$class.CimClassMethods[$methodname].Parameters

}

else

{

Get-CimClass -ClassName $classname | select -ExpandProperty CimClassMethods

}

}

 

I decided to save this as a psm1 file (module file) and called the module CimInvestigation.

In PowerShell 3 & 4 the module autoloads so I can do this:

Get-CimMethod -classname Win32_NetworkAdapter

Get-CimMethod -classname Win32_NetworkAdapter -methodname setpowerstate

 

Next time I’ll turn this into more of an advanced function with some validation and error checking

 


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: