Chapter 3. Scenario-based Scripting for SCCM Administration
Now that we have a basic understanding of how to use PowerShell cmdlets with Configuration Manager, it is time to better understand its real-time concepts. This chapter contains a few scenario-based examples that can help you to get an idea of the real power of PowerShell when used with Configuration Manager.
In this chapter, we will cover the following scenarios:
Adding multiple distribution points to a distribution point group
Creating multiple packages with the .csv
/.txt
file input
Using PowerShell to access the Configuration Manager installation directory
Checking for SCCM services
Operating a system deployment precheck
Running a ping test
Getting a list of primary sites in the Configuration Manager environment
Getting a list of all the site servers in the Configuration Manager environment
Getting component status in Configuration Manager
Note
Installing the SCCM client agent version. The code blocks demonstrated in this chapter will not...
Scenario 1 – adding multiple distribution points to a distribution point group
This is one common scenario where Configuration Manager administrators are required to add multiple distribution points to a distribution point group (DP group). A text file with the distribution point list can be used as the input to the script.
Create a text file at D:\PowerShell\DPFile.txt
(you can use your convenient location) and populate the file with the distribution points to be added to the DP group.
To show you a sample, DPFile.txt
should contain DP names, as follows:
Also, it is important to note that we should enter one DP name per line. Our sample file will look something like the following screenshot:
We will assume the distribution point group is named Test Distribution Group
and the text file location as D:\PowerShell
. Look at the following code:
Scenario 2 – creating multiple packages with the .csv/.txt file input
This example covers the creation of multiple packages with the details present in the .txt
file. Upon successful execution, we can see multiple packages created in the Configuration Manager console with the details or configurations present in the input file.
We are required to create a .csv
file with the details of each package that will be created. The details include the name of the packages, the manufacturer and version, and the description and the path of the source file, with the file located in the D:\SCCM
folder. For reference, let's name the Package.csv
file. For the current example, we will take an example file with contents, as shown in the following screenshot:
Consider the following code:
Scenario 3 – using PowerShell to get the Configuration Manager installation directory
One of the most common automation tasks that we carry out for Configuration Manager is the Configuration Manager health check framework. This primarily involves detecting the Configuration Manager installation directory.
It is not good practice to use the installation directory variable as static, but it is always advised that you make the variable dynamic, so that the script becomes more flexible and reusable. This example shows how PowerShell can be used to access the Configuration Manager installation directory:
You can use Set-Location
only if you are not specifying the full path for the Get-ItemProperty
cmdlet. Here, we use the full path for both cmdlets...
Scenario 4 – checking for SCCM services
Another common scenario when performing Configuration Manager health checks is to check for the existence of SCCM services, including the startup type and the status. We can create a function that receives the server name, service name, expected status, and expected startup type as parameters. It is recommended that you make changing variables parameters of the function so that we can make the function universal:
Scenario 5 – operating a system deployment precheck
One of the vital uses of Configuration Manager is the deployment of an operating system in large-scale enterprises. For successful operating system deployment, clients should have some of the prechecks completed successfully. One of the tests is for the existence of temp profiles in the machine. If temp profiles are present in a client machine, the chances of operating system deployment failure are more 'in both replace and refresh scenarios. The operating system deployment process fails even during the user-state capture step. So, it is good practice to automate prechecks using Configuration Manager before deploying the operating system.
One of the easiest ways to check for the existence of the temp profile is to check for the existence of the registry key. If the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AccountPicture\Users
registry key contains any of the sub keys with a .bkp
extension, we can say that the computer...
Scenario 6 – running a ping test
Before doing any automation on the list of computers, the most important aspect to be tested is whether the computer is reachable or not. If the computer is offline, there is no point in running any automation script block on the computer. Executing script blocks on offline computers will just increase the script execution time. So, it is always advised that you perform a ping test on the list of computers we get as input and run the block of automation script only on the computers that are reachable.
Sometimes, server administrators disable ping on the server. This code works only if the ping is not disabled at the firewall level.
The following code assumes that we have a complist.txt
file in the D
drive, which contains the list of all the computer names that are to be tested whether they are reachable or not:
Scenario 7 – getting a list of primary sites in the Configuration Manager environment
As newbies to the Configuration Manager environment (though not new to Configuration Manager concepts), it is essential that we familiarize ourselves with its site design and implementation. We can list the available primary Configuration Manager sites in the environment by querying local Windows Management Instrumentation (WMI) of the central administrative site (CAS). The following code will demonstrate how to query the CAS WMI to get the list of all the primary sites available in the Configuration Manager environment:
Scenario 8 – getting a list of all site servers in the Configuration Manager environment
Once we know that the primary servers are installed in our environment, the next step is to identify the site servers that are installed in our environment. The following code demonstrates how to get the list of all site servers that are installed in our Configuration Manager environment:
Upon successful execution of the code, the $Servers
variable will have a list of all the site servers installed in the environment.
Scenario 9 – getting component status in Configuration Manager
It is always good to have a periodic check of the component status of the Configuration Manager components. The following code demonstrates how to get Configuration Manager components by querying WMI using PowerShell. To simplify the code, we will display the output on the console. However, in real-time practices, we usually capture the output in a reporting file. The following code will look for all the installed Configuration Manager components and verify the health of each component. The code will query the WMI for the SMS_ComponentSummarizer
class to get the status of the site components:
Scenario 10 – installing the SCCM client agent version
It is a common requirement to pull out a report of the SCCM agent version installed on all the client machines in our Configuration Manager environment. This check can be considered one of the health check activities. The following code demonstrates how to get the client agent version installed on the list of computers.
Pre-Scripting Activities: Before we execute the script, we need to create a CompList.txt
text file in the D
drive (in case of different path, you can update the code block with the corresponding path) containing the list of computers on which the client agent version is to be extracted:
This chapter gave a clear idea of the usage of PowerShell cmdlets in the real-time administration of Configuration Manager. We discussed various scenarios and the usage of PowerShell scripts to get the work done easily without human errors. You should now have a clear idea of how to use PowerShell cmdlets with SCCM 2012. In the next chapter, you will see how to manage another important product of the System Center family—System Center Operations Manager through PowerShell.