Universal SCCM Content Prestager

, , , ,

PS1 file can be found at my TechNet gallery: HERE

The Use Case
    Prestaging content is a fact of life in the SCCM world.  Whether you’re standing up a new site, cloning a DP, or sending packages to a site with really bad bandwidth, there are a variety of reasons you need to create prestage packages.  At multiple contracts I worked, these packages were created by finding the content in the Configuration Manager GUI, right clicking, selecting Create Prestage Content File, and going through the wizard.  While this is technically a correct way to do things, it’s cumbersome, requires you to remember where everything is in the menu structure, and ties up your console while you do packages one at a time.   I could see using this method for one or two packages every now and then, but you’re on a PowerShell blog. Here, we’re all about scale. 

Not pictured: efficiency

Making it happen
     The Configuration Manager module actually comes with a prestage cmdlet built right in, but this cmdlet is one of the most poorly written ones in all of PowerShell.  It has no real intelligence of its own, requiring you to spell out exactly what package type you want to back up. Since every package has a unique PackageID value, I never understood why they didn’t just make it use that number and get on with life, but they didn’t.  Feel free to download the script and follow along.

I actually couldn’t fit them all on one screen


The Script
     The first thing we need to do is declare our variables. This script needs to know what the package ID number is (which can be found literally everywhere the package is mentioned in the SCCM console or WMI interface),  what DP we’re pulling the content from, and where the file needs to be saved. All of these are mandatory, so you’ll be prompted for them if you don’t put them inline.  Also, depending on the prestage location, you’ll need admin rights to move files there, so we just check for those at the outset.  We’ll also make sure you’re connected to your CMSite PSDrive. I also have the script make sure the package isn’t already there.  Something else to keep in mind is that the DP name needs to be a FQDN when the command runs. If you enter it with just the hostname, the script will sort that out for you, so no worries.

     After all the pre-reqs have checked out, the real logic comes in. The first thing it does is try to run get-cmpackge with the package ID, if that comes back as $NULL, meaning there was no package with that ID, it starts down the list of available content types. It checks if it’s a software update package, application, boot image, OS image, or driver package.  Until it finds something that actually returns an object, it’ll keep checking. Then, it makes a mental note of what it found so we can use it later.  For troubleshooting and debugging reasons, I like to output the name of the package as well as its source path, but this isn’t necessary. It will also tell you where it’s going and what it will be named.  Then, we use a Switch  statement with the count variable (our mental note from earlier) to select the correct Publish-CMPrestageContent flags that actually do the prestaging work for us.

Figuring out what you wanted
Actually prestaging it

Actually Using It
     On its own, it’s a nice function to have loaded in my shell. Being able to generate a prestage file without tying up my GUI is always handy, and depending on the organizational skills of the previous SCCM admins, not having to dig around to find a package in their menus can be a real time saver.  Where this script comes into its own, however, is when it’s chained together with other commands, which is what we’ll discuss in our next post. 

Working with CSV Files

, , ,

Hey everyone, it’s been a little while since my last post, but work’s been busy.  A recurring issue I see on TechNet is that plenty of people have trouble working with importing/exporting CSV files.  Specifically, I see questions about how to modify CSV files. Personally, I don’t see any value in modifying the CSV file directly, but rather importing the data from said file and working with the dataset natively in PowerShell.  Using one person’s thread as an example, he had a CSV file full of IP addresses that corresponded to his VM’s.  He wanted to add data about the VM based on the IP address, but wanted to know how to “modify the line” in the CSV file.    He had already written part of the script to create the CSV by pinging each computer in his IP range and outputting that to CSV.  For our test, here’s what our CSV looks like:

Now, let’s say we want to find the hostnames for each of these computers and add that to a new column. There are a couple ways we could do this, but in an enterprise environment, a good way to do it is to just ask AD who it is.   First, we create the empty “column” for our Hostname value.

Once that’s created, we need to fill it with values:

Keep in mind that you will see a lot of red text for any computer that doesn’t have an entry in AD. This could be a printer, a switch, etc.  If you’re using Windows 8.1+ or Server 2012R2+, you can use the resolve-dnsname cmdlet. 
You can add more columns to the CSV by using the Add-Member cmdlet to your heart’s content, and when you’re done, you pipe your $dataset variable back to the export-csv cmdlet.   
Really, once you start looking at CSV files as arrays of custom objects, they’re pretty easy to work with.

Importing drivers into SCCM in bulk

, , , , , ,

This is taken from my TechNet gallery here: https://goo.gl/n1QT89

     When you’re tasked with something like a Windows 10 upgrade, you’ll find yourself spending lots of time downloading and importing drivers into SCCM.   While this script won’t go out and download them for you (like the Dell and HP Driver Import tools I’ve seen out there), it manufacturer, model, and architecture agnostic, you don’t get caught up trying to negotiate your way past your firewall and proxy teams, and it runs in a bit under 50 lines of code (including comments). Rather than pasting in the entire thing, I’ll do a screenshot and walk through from there.

     For this script to work, there’s some groundwork required on your part. When you download the drivers, they need to be downloaded into a folder that has whatever name you want for your driver package later.  If you’re like me, you’re already doing this as you download. If I need drivers for an HP Z230 desktop, the folder they’re saved in is already called “HP Z230 Windows 10 x64” or something similar so I can find them later.  The way this script works, whatever your folders’ names are is what names your driver packages will end up with.
    Aside from that, all you need to do is plug in the path to the file share that has all your make/model folders in the root, as well as the location where you want to store your driver packages.
    Something you will notice in this script is that I bounce between my C: drive and my SCCM drive. This is because UNC paths don’t always work as expected when you’re on the SCCM drive, and SCCM cmdlets don’t play nice running from anything other than the SCCM drive.  To guarantee they both work when needed, I just switch between locations, and it’s no big deal. 
    This script can take a little while to run, but it will give you feedback as it goes, and it doesn’t lock you out of the SCCM GUI while it runs.