{"id":74741,"date":"2015-11-19T00:01:00","date_gmt":"2015-11-19T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/11\/19\/powershell-and-configuration-manager-2012-r2part-4\/"},"modified":"2019-06-18T23:26:56","modified_gmt":"2019-06-19T07:26:56","slug":"powershell-and-configuration-manager-2012-r2part-4","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-and-configuration-manager-2012-r2part-4\/","title":{"rendered":"PowerShell and Configuration Manager 2012 R2\u2013Part 4"},"content":{"rendered":"<p><strong style=\"font-size: 12px;\">Summary<\/strong><span style=\"font-size: 12px;\">: Use the Configuration Manager cmdlets to work with driver packages.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>\u00a0Hey, Scripting Guy!<\/p>\n<p>My current job involves creating packages in Configuration Manager\u00a02012. Is there some way to automate the process? It\u2019s not that it\u2019s actually difficult, but it is a very repeatable process. I thought to myself, \u201cRepeatable? This sounds like a job for PowerShell!&#8221; Help out a friend and tell me there are cmdlets to do this.<\/p>\n<p>\u2014KB<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>\u00a0Hello KB,<\/p>\n<p>Honorary Scripting Guy, Sean Kearney, is here today with relief for you. Yes, there are cmdlets for that!<\/p>\n<p>If you\u2019ve been following along this week, you have seen that we can manage collections and update distribution points by using the Configuration Manager cmdlets.<\/p>\n<p><strong> \u00a0Note<\/strong>\u00a0\u00a0This is a five-part series that includes the following posts:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-and-configuration-manager-2012-r2part-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">PowerShell and Configuration Manager 2012 R2\u2013Part 1<\/a>\nLoad the Configuration Manager cmdlets and make a basic site connection.<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-and-configuration-manager-2012-r2part-2\" target=\"_blank\" rel=\"noopener noreferrer\">PowerShell and Configuration Manager 2012 R2\u2013Part 2<\/a>\nUse the Configuration Manager cmdlets to work with site collections.<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-and-configuration-manager-2012-r2part-3\" target=\"_blank\" rel=\"noopener noreferrer\">PowerShell and Configuration Manager 2012 R2\u2013Part 3<\/a>\nUse the Configuration Manager cmdlets to update applications in distribution points.<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-and-configuration-manager-2012-r2part-4\" target=\"_blank\" rel=\"noopener noreferrer\">PowerShell and Configuration Manager 2012 R2\u2013Part 4<\/a>\nUse the Configuration Manager cmdlets to work with driver packages.<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-and-configuration-manager-2012-r2part-5\" target=\"_blank\" rel=\"noopener noreferrer\">PowerShell and Configuration Manager 2012 R2\u2013Part 5<\/a>\nUse the Configuration Manager cmdlets to create an application.<\/li>\n<\/ul>\n<p>Yup, there\u2019s 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:<\/p>\n<p style=\"margin-left: 30px;\">Get-Command \u2013Module ConfigurationMananger *Package*<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5670.1.PNG\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/5670.1.PNG\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>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.<\/p>\n<p>I can do something as simple as list all the drivers presently within Configuration Manager by using <b>Get-CmDriver<\/b>, or I can get a list of driver packages with <b>Get-CmDriverPackage<\/b>.<\/p>\n<p>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\u2019ll get the list of objects I find useful to almost mimic the output in the console:<\/p>\n<p style=\"margin-left: 30px;\">Get-CmDriver | Select-Object LocalizedDisplayName,DriverProvider,DriverClass,DriverDate,DriverVersion<\/p>\n<p>\u2026and for driver packages:<\/p>\n<p style=\"margin-left: 30px;\">Get-CmDriverPackage | Select-Object Name,Version,PackageID<\/p>\n<p>Let\u2019s look at something pretty typical\u2014creating a driver package.<\/p>\n<p>You\u2019ve created a driver package in Configuration Manager at least once or twice, right? To create a driver package with Windows PowerShell, you\u2019re going to need three cmdlets:<\/p>\n<ul>\n<li><b>Import-CMDriver<\/b>\u00a0\u00a0 \u00a0Imports a single driver into Configuration Manager<\/li>\n<li><b>New-CMDriverPackage<\/b> \u00a0\u00a0\u00a0Creates a driver package (if needed) in Configuration Manager<\/li>\n<li><b>Add-CMDrivertoDriverPackage<\/b>\u00a0\u00a0\u00a0Adds a tagged driver into a Configuration Manager package<\/li>\n<\/ul>\n<p>If you don\u2019t 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.<\/p>\n<p style=\"margin-left: 30px;\">$PackageName=\u2019My Sample Driver Package\u2019<\/p>\n<p style=\"margin-left: 30px;\">$PackageSource=\u2019\\\\ContosoSCCM\\DriverPackages\\SampleDriverPackage\u2019<\/p>\n<p style=\"margin-left: 30px;\">New-CMDriverPackage \u2013name $Packagename \u2013path $PackageSource<\/p>\n<p>If you need to import a single driver, you would use the following command, which has the driver path supplied in the object named <b>$DriverPath<\/b>:<\/p>\n<p style=\"margin-left: 30px;\">$DriverPath=&#8217;\\\\sccm2012r2\\sourcefiles\\driver\\NetworkCard\\Drivers\\08MVF\\release\\MxG2wDO.inf&#8217;<\/p>\n<p style=\"margin-left: 30px;\">Import-CMDriver \u2013UncFileLocation $DriverPath -ImportDuplicateDriverOption OverwriteCategory -EnableAndAllowInstall $True<\/p>\n<p>For some reason, although the object displays properly on the screen, it does not capture properly. However, if we pipe it through <b>Select-Object<\/b> and grab all available objects, we can capture it. Here is a great blog post from Operating System Deployment Couture that explains this: <a href=\"http:\/\/www.osd-couture.com\/2015\/04\/sccm-2012r2-make-import-cmdriver-cmdlet.html\" target=\"_blank\" rel=\"noopener noreferrer\">SCCM 2012 R2: Make Import-CMDriver cmdlet work for you<\/a>.<\/p>\n<p>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 <b>LocalizedDisplayName<\/b> from the results as we import each one. We need this information and the name of the driver package.<\/p>\n<p style=\"margin-left: 30px;\">$DriverName=(Import-CMDriver \u2013UncFileLocation $DriverPath -ImportDuplicateDriverOption OverwriteCategory -EnableAndAllowInstall $True | Select-Object *).LocalizedDisplayName<\/p>\n<p>We now supply this information to <b>Add-CMDrivertoDriverPackage<\/b>:<\/p>\n<p style=\"margin-left: 30px;\">Add-CmDrivertoDriverPackage \u2013DriverName $DriverName \u2013PackageName $PackageName<\/p>\n<p>There you go! We have now added a driver to a new package by using PowerShell!<\/p>\n<p>A limit to <b>Import-CMDriver<\/b> 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.<\/p>\n<p>If we\u2019d like this same capability in Windows PowerShell, we can use <b>Get-ChildItem<\/b>. We can target only files with the <b>.inf<\/b> extension while recursing the folder structure:<\/p>\n<p style=\"margin-left: 30px;\">Get-Childitem \u2018\\\\ContosoSCCM\\SccmShare\\Drivers\\SampleDriver\u2019 \u2013recurse \u2013include *.inf<\/p>\n<p>This will get stored away as an object:<\/p>\n<p style=\"margin-left: 30px;\">$DriverList=Get-Childitem \u2018\\\\ContosoSCCM\\SccmShare\\Drivers\\SampleDriver\u2019 \u2013recurse \u2013include *.inf<\/p>\n<p>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.<\/p>\n<p>If we wrote this entire process from package creation to driver imports, and added the packages as a script, it would look like this:<\/p>\n<p style=\"margin-left: 30px;\">$PackageName=\u2019My Sample Driver Package\u2019<\/p>\n<p style=\"margin-left: 30px;\">$PackageSource=\u2019\\\\ContosoSCCM\\DriverPackages\\SampleDriverPackage\u2019<\/p>\n<p style=\"margin-left: 30px;\">New-CMDriverPackage \u2013name $Packagename \u2013path $PackageSource<\/p>\n<p style=\"margin-left: 30px;\">Foreach ($Driver in $Driverlist)<\/p>\n<p style=\"margin-left: 30px;\">{<\/p>\n<p style=\"margin-left: 30px;\">$DriverName=(Import-CMDriver \u2013UncFileLocation $DriverPath -ImportDuplicateDriverOption OverwriteCategory -EnableAndAllowInstall $True | Select-Object *).LocalizedDisplayName<\/p>\n<p style=\"margin-left: 30px;\">Add-CmDrivertoDriverPackage \u2013DriverName $DriverName \u2013DriverPackageName $PackageName<\/p>\n<p style=\"margin-left: 30px;\">}<\/p>\n<p>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.<\/p>\n<p>If we decided we need to remove a particular driver from a package, we supply the same information to the <b>Remove-CmDriverfromDriverPackage<\/b> cmdlet:<\/p>\n<p style=\"margin-left: 30px;\">Remove-CmDriverfromDriverPackage \u2013drivername $DriverName \u2013driverpackagename $PackageName<\/p>\n<p>If you make a mistake, you can remove a driver by using <b>Remove-CmDriver<\/b>. The following command would prompt you, then completely delete the driver:<\/p>\n<p style=\"margin-left: 30px;\">Remove-CmDriver $Name<\/p>\n<p>With these cmdlets, you could make your regular process of managing drivers in Configuration Manager so much easier.<\/p>\n<p>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!<\/p>\n<p>I invite you to follow the Scripting Guys on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\" rel=\"noopener noreferrer\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\" rel=\"noopener noreferrer\">Facebook<\/a>. If you have any questions, send email to them at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\" rel=\"noopener noreferrer\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\" rel=\"noopener noreferrer\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, always remember that with great PowerShell comes great responsibility.<\/p>\n<p><b>Sean Kearney, <\/b>Honorary Scripting Guy and Cloud and Datacenter Management MVP<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use the Configuration Manager cmdlets to work with driver packages. \u00a0Hey, Scripting Guy! My current job involves creating packages in Configuration Manager\u00a02012. Is there some way to automate the process? It\u2019s not that it\u2019s actually difficult, but it is a very repeatable process. I thought to myself, \u201cRepeatable? This sounds like a job for [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[487,56,154,45],"class_list":["post-74741","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-configuration-manager","tag-guest-blogger","tag-sean-kearney","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use the Configuration Manager cmdlets to work with driver packages. \u00a0Hey, Scripting Guy! My current job involves creating packages in Configuration Manager\u00a02012. Is there some way to automate the process? It\u2019s not that it\u2019s actually difficult, but it is a very repeatable process. I thought to myself, \u201cRepeatable? This sounds like a job for [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74741","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=74741"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74741\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=74741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=74741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=74741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}