Exclude server or computer from discover in SCCM
Spread the love
Option Explicit
' **********************************************************************************
' *
' * Title:              Exculde from SCCM discovery
' *
' * File Name:          excludefromsccm.vbs
' * Programmer(s):      Stephen Wireman
' *
' * First Written:      02/13/2017
' * Last Modified:  
' *
' * Version:            1.0.0
' *
' * Purpose:            So a server/computer can be excluded
' *                     from all methods of SCCM discovery.
' *                 
' *
' * Information:        Modifies the registry key located at
' *                     HKEY_LOCAL_MACHINE
' *                     Software\Microsoft\SMS\Components\SMS_DISCOVERY_DATA_MANAGER\ExcludeServers
' *                     Will maintain the values already in the key
' *
' * Note:               With the use of "Option Explicit" all variables
' *                     must be explicitly defined and declared.
' * 
' **********************************************************************************
 
' ***********************************************************
' * Define script constants                                 *
' ***********************************************************
Const HKEY_LOCAL_MACHINE = &H80000002
 
' ***********************************************************
' * Define variable dimensions, assign variables to objects *
' ***********************************************************
Dim strComputer  : strComputer  = "."
Dim oReg         : Set oReg     = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
                                  strComputer & "\root\default:StdRegProv")
Dim strKeyPath   : strKeyPath   = "Software\Microsoft\SMS\Components\SMS_DISCOVERY_DATA_MANAGER"
Dim strValueName : strValueName = "ExcludeServers"
 
' ***********************************************************
' * Execution phase                                         *
' ***********************************************************
Call Main()
Call Finish()
 
' ***********************************************************
' * Functions and Sub Routines                              *
' ***********************************************************
Sub Main()
    Dim strInput  : strInput  = vbNullString
    Dim strTemp   : strTemp   = vbNullString
    Dim arrValues : arrValues = vbNullString
    Dim iValues   : iValues   = vbNullString
     
    Call oReg.GetMultiStringValue(HKEY_LOCAL_MACHINE,strKeyPath,strValueName,arrValues)
 
    For Each strValue In arrValues
        strTemp = strTemp & ","
    Next
 
    strInput = inputbox("Enter hostname to exclude from discovery","","SCCM Exclusion")
    strTemp  = strTemp & "," & strInput
    iValues  = strToArray(strTemp)
 
    Call oReg.SetMultiStringValue(HKEY_LOCAL_MACHINE,strKeyPath,MultValueName,iValues)
End Sub
 
Sub Finish()
    Set oReg = Nothing
     
    If err then 
        Call wscript.echo("Error occured during processing:" & vbcrlf & vbcrlf & _
                           err.number & "," & err.description & "," & err.source)
        Call wscript.quit(err.number)
    Else
        Call wscript.quit(0)
    End If
End Sub
 
Function strToArray(s)
    Dim a     : a     = vbNullString
    Dim token : token = vbNullString
 
    ' discard blank entries
    For Each token in split(s, ",")
        token = trim(token)
        If token <> "" Then
            If isEmpty(a) Then
                a = token
            Else
                a = a &amp; "," &amp; token
            End If
        End If
    Next
 
    ' return array
    strToArray = split(a, ",")
End Function

Leave a Comment

Your email address will not be published. Required fields are marked *