MEMCM Powershell Get Configuration Item's "Reference's"

Barleyologist 116 Reputation points
2021-06-28T21:44:23.137+00:00

Trying to figure out how to get which CM Baseline a specific Configuration Item is referenced in and remove it's reference via Powershell. Trying to use Get-CMConfigurationItem but do not see 'references' and maybe that information is not included, if not is there another way?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,399 questions
Microsoft Configuration Manager
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. John Blanks 6 Reputation points
    2022-05-26T14:10:52.113+00:00

    The reason I this question caught my eye was i want to act on all the references of a given CI. You know like be able to remove the references without having to hunt the individual baselines down so I can delete a CI. So I wrote this Powershell function that will remove the references from any baseline that references it. I know it is not the cleanest code but it works like a champ in my environment where I have over 700 baselines.

    My Function is meant for interactive use and prompts you before it removes the CI References. If more than one CI is found matching the CIName it will ask you to be more specific.

    Function Remove-CIFromBaselines {  
        Param (  
                [Parameter(Mandatory)]  
                [String] $CIName  
                  
        )   
        $ciObject = (get-CMConfigurationItem  -Name $CIName -fast)  
        $count = $ciObject.count   
        If ($count -gt 1) {  
            Write-Output "There are [$count] CIs in MECM named [$CIName]. Please be more specific..."   
            Break  
        } ELSEIF ($count -eq 0) {  
            Write-Output "There are [$count] CIs in MECM named [$CIName]. Please be more specific..."   
            Break  
        }  
        $AllBaselines= Get-CMBaseline -fast  
        $baselinecount = $allbaselines.count  
        Write-Output "There are [$baselinecount] total baselines to analyze - This could take a few minutes."  
        $prompt = Read-host -Prompt "Are you sure you want to remove All Referennces to [$CIName]? (Y/N) "  
        If ($Prompt -ne "Y") {Break}  
        $prompt = Read-host "Type `'I ConFirm`' to continue"   
        If ($Prompt -eq "I Confirm" -and $prompt -cmatch "I ConFirm") {          
            $CIID = $CIObject.CI_ID  
            $LastIndex = $CIobject.CI_UniqueID.lastIndexof("_") +1  
            $length = $CIObject.CI_UniqueID.substring($lastIndex).length - 2  
            $CIUniqueID = $CIObject.CI_UniqueID.substring($lastIndex,$length)  
            Write-Output "The CIUniqueID is [$CIUniqueID]"   
            $affectedList = New-Object System.Collections.ArrayList  
            $modifiedList = New-Object System.Collections.ArrayList  
            ForEach ($baseline in $AllBaseLines) {  
                $baselineName = $Baseline.localizedDisplayName  
                Write-Output "Analyzing [$baselineName] for reference to [$CIName]"   
                $XMLContent = $baseline | get-CMBaselineXMLDefinition  
                If ($XMLContent.contains($CIUniqueID)){  
                    $AffectedList.Add($BaselineName) |out-Null  
                    Write-Output "[$baselineName] contians [$CIName]"   
                    # Check if it is a OS or An Application CI - 3 = OS ; 5 = Required Application CI - https://learn.microsoft.com/en-us/mem/configmgr/develop/reference/compliance/sms_configurationitemlatestbaseclass-server-wmi-class  
                    If ($CIObject.CIType_ID -eq 3) {  
                        $Baseline |Set-CMBaseline -RemoveOSConfigurationItem $CIObject.CI_ID  
                        $Modifiedlist.Add($Baselinename) |out-null  
                    }  
                    If ($CIObject.CIType_ID -eq 5) {  
                        $Baseline |Set-CMBaseline -RemoveRequiredConfigurationItem $CIObject.CI_ID  
                        $Modifiedlist.Add($Baselinename) |out-Null  
                    }  
                }  
            }  
            $countofAffected = $AffectedList.count  
            $countofmodified = $ModifiedList.Count  
            Write-Output "#################################### SUMMARY ####################################################"   
            Write-Output "There were [$countofAffected] Baselines that contain [$CIName]. [$countofmodified] were updated to remove the reference to [$CIName]"   
            If ($countofAffected -eq $countofmodified) {  
                ForEach ($item in $AffectedList) {  
                    Write-Output "Baseline [$item] contained [$CIName]"   
                }  
                ForEach ($item in $ModifiedList) {  
                    Write-Output "[$CIName] was removed from [$Item]"  
                }  
            }  
            Write-Output "#################################################################################################"   
        } ELSE {  
            Write-Output "Bailing since you did not confirm"  
        }   
    }  
    

    Hope this helps.

    1 person found this answer helpful.
    0 comments No comments

  2. AllenLiu-MSFT 41,131 Reputation points Microsoft Vendor
    2021-06-29T07:09:03.377+00:00

    Hi, @Barleyologist
    Thank you for posting in Microsoft Q&A forum.
    I did some research and couldn't find a way to get this information by powershell.
    However, we can get this from SCCM console, check the configuration item's relationships of properties, and select "Configuration baselines that reference this configuration item", it will show the information.
    110141-13.jpg


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Sherry Kissinger 3,966 Reputation points
    2022-05-26T12:30:20.39+00:00

    This also doesn't answer the specific question (it's asking about references via Powershell); but via SQL query, this works for me.

    Select Baseline.DisplayName as 'BaselineName', ciinfo.DisplayName as 'CIName', *
    from fn_ListConfigurationBaselineInfo(1033) Baseline
    join vSMS_CombinedConfigurationItemRelations cir on cir.FromCI_ID=Baseline.CI_ID
    join v_ConfigurationItems ci on ci.CI_ID=cir.ToCI_ID
    join fn_ListCIs(1033) ciinfo on ciinfo.ci_id=cir.ToCI_id
    where ciinfo.DisplayName = 'The Exact Name of a Configuration Item'

    If that answer still is useless (it might be); you may need to submit a request to support, and see if there is a way currently, and if not, to request that a Powershell cmdlet be created or enhanced to address your requirement.

    0 comments No comments