Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Microsoft System Center Powershell Essentials

You're reading from  Microsoft System Center Powershell Essentials

Product type Book
Published in Apr 2015
Publisher Packt
ISBN-13 9781784397142
Pages 140 pages
Edition 1st Edition
Languages

Table of Contents (15) Chapters

Microsoft System Center PowerShell Essentials
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. Setting up the Environment to Use PowerShell 2. Administration of Configuration Manager through PowerShell 3. Scenario-based Scripting for SCCM Administration 4. Administration of Operations Manager through PowerShell 5. Scenario-based Scripting for SCOM Administration 6. Administration of Service Manager through PowerShell 7. Scenario-based Scripting for SCSM Administration 8. Best Practices Index

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.

Prescripting activities

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:

GURU.COM
DP1.GURU.COM
DP2.GURU.COM

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:

Assumption

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:

$FilePath = "D:\PowerShell\DPFile.txt"
$DPNames = Get-Content –path $FilePath
ForEach(...

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.

Prescripting activities

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:

$PkgDetails = Import-csv –path "D:\SCCM\Package.csv"
Foreach($Pkg in $PkgDetails)
{
    $PkgName = $($Pkg.Name)
    $Description = $($Pkg.Description)
    $Mnfr = $($Pkg.Manufacturer)
    $Version = ...

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:

Set-Location 'HKLM\SOFTWARE\Microsoft\SMS\Identification\ Installation Directory'
$AllProp = Get-ItemProperty -path "HKLM\SOFTWARE\Microsoft\SMS\Identification\ Installation Directory"
$InstallDir = $AllProp."Installation directory"
Write-Host "$InstallDir"

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:

Function Get-ServiceStatus($ServerList, [string]$Servicename, [string]$Status, [string]$Startup)
{
  foreach ($Comp in $ServerList)
  { 
    if($Temp = Get-WmiObject Win32_Service -ComputerName $Comp | where {$_.Name -eq $Servicename } | select Name, StartMode, State, Displayname)
     {
       $Sname = $Temp.Displayname
       $ServiceState = $Temp.State
       $ServiceMode = $Temp.StartMode
       if(($ServiceState -eq $Status) -and ($ServiceMode -eq $Startup))
       {
         Write-Host "service $Sname on $comp is Healthy" –foregroundcolor...

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.

Prescripting activities

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:

$CompList = Get-Content -Path "D:\PowerShell\Complist.txt"
foreach ($Comp in $CompList)
{
  If (test-Connection -ComputerName...

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:

$PrimarySites = @()
$SiteCodeList = @()

$CentralSiteCode = "ABC"         # SCCM site code
$CentralSiteProvider = "SCCMCAS" # CAS server name  

$Sites=Get-WmiObject -Namespace root\sms\site_$CentralSiteCode SMS_Site -Filter "Type=2" -ComputerName $CentralSiteProvider
foreach ($site in $Sites)
{
  $SiteCode = $site.SiteCode.ToUpper()
  $SiteCodeList += $SiteCode
  $PrimarySites += $site...

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:

$CentralSiteCode = "ABC"
$CentralSiteProvider = "SCCMCAS"

$SiteRoles = Get-WmiObject -ComputerName $CentralSiteProvider -Namespace root\sms\site_$CentralSiteCode SMS_SystemResourceList
$Servers = new-object System.Collections.ArrayList
$ArrServers = new-object System.Collections.ArrayList
Foreach ($item in $SiteRoles)
{
  $StrFQDN = ($item.ServerRemoteName).ToUpper()
  
  $ArrServers.Add($StrFQDN) -
}
$Servers = $arrServers | sort | select –uniq

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:

$TallyInterval = "0001128000080008" #Since site installation
$CentralSiteCode = "ABC"
$CentralSiteProvider = "SCCMCAS"  

$ComputersWithIssues = Get-WmiObject -Namespace root\sms\Site_$CentralSiteCode -query "Select * from SMS_ComponentSummarizer where Status <> 0 AND TallyInterval = '$TallyInterval'" -ComputerName $CentralSiteProvider...

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:

$Complist = Get-Content -Path "D:\CompList.txt"
foreach($Comp in $Complist)
{
  $SCCMClientVersion = (Get-WmiObject -Namespace root\ccm -Class CCM_Client -ComputerName $Comp).ClientVersion
  Write-Host "SMS Client Agent Version For $Comp - $SCCMClientVersion"
}

Summary


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.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Microsoft System Center Powershell Essentials
Published in: Apr 2015 Publisher: Packt ISBN-13: 9781784397142
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}