November 19th, 2015

PowerShell and Configuration Manager 2012 R2–Part 4

Doctor Scripto
Scripter

Summary: Use the Configuration Manager cmdlets to work with driver packages.

Hey, Scripting Guy! Question Hey, Scripting Guy!

My current job involves creating packages in Configuration Manager 2012. Is there some way to automate the process? It’s not that it’s actually difficult, but it is a very repeatable process. I thought to myself, “Repeatable? This sounds like a job for PowerShell!” Help out a friend and tell me there are cmdlets to do this.

—KB

Hey, Scripting Guy! Answer Hello KB,

Honorary Scripting Guy, Sean Kearney, is here today with relief for you. Yes, there are cmdlets for that!

If you’ve been following along this week, you have seen that we can manage collections and update distribution points by using the Configuration Manager cmdlets.

 Note  This is a five-part series that includes the following posts:

Yup, there’s a cmdlet to meet your needs, too! If you read the previous posts in this series, you can almost guess how to identify the potential cmdlets for working with driver packages. We simply need to execute:

Get-Command –Module ConfigurationMananger *Package*

Image of command output

There is a decent set to handle almost all of the package needs that I can personally see. Cmdlets to handle driver and software packages are ready to go.

I can do something as simple as list all the drivers presently within Configuration Manager by using Get-CmDriver, or I can get a list of driver packages with Get-CmDriverPackage.

These cmdlets need their formatting cleaned up to pull down data. There is way too much information on the screen. To save you some trouble, I’ll get the list of objects I find useful to almost mimic the output in the console:

Get-CmDriver | Select-Object LocalizedDisplayName,DriverProvider,DriverClass,DriverDate,DriverVersion

…and for driver packages:

Get-CmDriverPackage | Select-Object Name,Version,PackageID

Let’s look at something pretty typical—creating a driver package.

You’ve created a driver package in Configuration Manager at least once or twice, right? To create a driver package with Windows PowerShell, you’re going to need three cmdlets:

  • Import-CMDriver    Imports a single driver into Configuration Manager
  • New-CMDriverPackage    Creates a driver package (if needed) in Configuration Manager
  • Add-CMDrivertoDriverPackage   Adds a tagged driver into a Configuration Manager package

If you don’t already have a package for your drivers, you can create one quite easily. You only need two things: a name to give to the package and a UNC path name to store the package contents.

$PackageName=’My Sample Driver Package’

$PackageSource=’\\ContosoSCCM\DriverPackages\SampleDriverPackage’

New-CMDriverPackage –name $Packagename –path $PackageSource

If you need to import a single driver, you would use the following command, which has the driver path supplied in the object named $DriverPath:

$DriverPath=’\\sccm2012r2\sourcefiles\driver\NetworkCard\Drivers\08MVF\release\MxG2wDO.inf’

Import-CMDriver –UncFileLocation $DriverPath -ImportDuplicateDriverOption OverwriteCategory -EnableAndAllowInstall $True

For some reason, although the object displays properly on the screen, it does not capture properly. However, if we pipe it through Select-Object and grab all available objects, we can capture it. Here is a great blog post from Operating System Deployment Couture that explains this: SCCM 2012 R2: Make Import-CMDriver cmdlet work for you.

We will now capture the output from this cmdlet to leverage it as we add it to the driver package. To add our newly imported driver to a driver package, we grab the property LocalizedDisplayName from the results as we import each one. We need this information and the name of the driver package.

$DriverName=(Import-CMDriver –UncFileLocation $DriverPath -ImportDuplicateDriverOption OverwriteCategory -EnableAndAllowInstall $True | Select-Object *).LocalizedDisplayName

We now supply this information to Add-CMDrivertoDriverPackage:

Add-CmDrivertoDriverPackage –DriverName $DriverName –PackageName $PackageName

There you go! We have now added a driver to a new package by using PowerShell!

A limit to Import-CMDriver is that it imports a single driver into Configuration Manager. We all know that we can target a folder and discover all the drivers within in the GUI.

If we’d like this same capability in Windows PowerShell, we can use Get-ChildItem. We can target only files with the .inf extension while recursing the folder structure:

Get-Childitem ‘\\ContosoSCCM\SccmShare\Drivers\SampleDriver’ –recurse –include *.inf

This will get stored away as an object:

$DriverList=Get-Childitem ‘\\ContosoSCCM\SccmShare\Drivers\SampleDriver’ –recurse –include *.inf

With these two simple commands, we can do exactly what you typically do in the GUI of Configuration Manager, which is create driver packages and import the contents there.

If we wrote this entire process from package creation to driver imports, and added the packages as a script, it would look like this:

$PackageName=’My Sample Driver Package’

$PackageSource=’\\ContosoSCCM\DriverPackages\SampleDriverPackage’

New-CMDriverPackage –name $Packagename –path $PackageSource

Foreach ($Driver in $Driverlist)

{

$DriverName=(Import-CMDriver –UncFileLocation $DriverPath -ImportDuplicateDriverOption OverwriteCategory -EnableAndAllowInstall $True | Select-Object *).LocalizedDisplayName

Add-CmDrivertoDriverPackage –DriverName $DriverName –DriverPackageName $PackageName

}

Of course, we could improve this script by adding a parameter for the package name, the source, and the destination. You could even add what you learned yesterday about updating distribution points. But this is a good start.

If we decided we need to remove a particular driver from a package, we supply the same information to the Remove-CmDriverfromDriverPackage cmdlet:

Remove-CmDriverfromDriverPackage –drivername $DriverName –driverpackagename $PackageName

If you make a mistake, you can remove a driver by using Remove-CmDriver. The following command would prompt you, then completely delete the driver:

Remove-CmDriver $Name

With these cmdlets, you could make your regular process of managing drivers in Configuration Manager so much easier.

KB, that is all there is for working with drivers today, but pop in tomorrow when we use PowerShell to create applications in Configuration Manager!

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, always remember that with great PowerShell comes great responsibility.

Sean Kearney, Honorary Scripting Guy and Cloud and Datacenter Management MVP

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.