1

I’m writing a batch script to pull SCCM site codes from SCCM clients. I’d like to pull the clients site code and compare that to an array of site codes. If there is a match then I’ll uninstall the client. I have the uninstall portion. This has to be done as a batch file.

I can get the site code with the following WMIC command

WMIC /namespace:\\root\ccm path sms_client CALL GetAssignedSite

This produces the following output

Executing (sms_client)->GetAssignedSite()
Method execution successful.
Out Parameters:[abstract]
class __PARAMETERS
{
        [out, ID(0): DisableOverride ToInstance] string sSiteCode = "BBB";
        [out] uint32 ReturnValue;
};

How can I extract and compare the site code in the batch script – “BBB” in this example.

Here is what I have for Powershell as an example but this has to be a batch file.

$SiteCode = ([wmiclass]'ROOT\ccm:SMS_Client').GetAssignedSite()
$SCCMSiteCodes = @('AAA', 'BBB', 'CCC', 'GGG', 'VVV')
If ($SCCMSiteCodes -Contains $SiteCode.sSiteCode) 
    { uninstall SCCM client }

I'll end up running the .bat file with a psexec command against a server list.

Thanks for the help.

Thanks again, dbenham. I modified the code slightly by adding a second IF statement.

SETLOCAL enableDelayedExpansion
SET "SCCMSiteCodes= 'AAA' 'BBB' 'CCC' 'GGG 'VVV' "
FOR /f delims^=^"^ tokens^=2^ eol^= %%A in (
  'wmic /namespace:\\root\ccm path sms_client CALL GetAssignedSite^|findstr sSiteCode'
) DO (
  IF "!SCCMSiteCodes:'%%A'=!" neq "!SCCMSiteCodes!" (
    CALL :TEE SCCM client - Sitecode match found
    REM Your uninstall CALL goes here
    )
  IF "!SCCMSiteCodes:'%%A'=!" equ "!SCCMSiteCodes!" (
    CALL :TEE SCCM client - Sitecode match not found
    GOTO NOTFOUIND
    )
)

I found another scenario that triggers an uninstall and I'm not exactly sure why since it doesn't contain any of the sitecodes. In this case the server has not reached the SCCM managemnent server so it has not received a sitecode.

For these servers, the response to

WMIC /namespace:\\root\ccm path sms_client CALL GetAssignedSite

is

Executing (sms_client)->GetAssignedSite()
ERROR:
Description = Can't enumerate any more, because the associated data is missing

I'm researching but have not come up with an answer yet. Any suggestions?

Javagenki
  • 13
  • 4

1 Answers1

1

Here is one way to do it:

@echo off
setlocal enableDelayedExpansion
set "SCCMSiteCodes= 'AAA' 'BBB' 'CCC' 'GGG 'VVV' "
for /f delims^=^"^ tokens^=2^ eol^= %%A in (
  'wmic /namespace:\\root\ccm path sms_client CALL GetAssignedSite^|findstr sSiteCode'
) do (
  if "!SCCMSiteCodes:'%%A'=!" neq "!SCCMSiteCodes!" (
      REM Your uninstall CALL goes here
  )
)

The code uses a couple of non-intuitive things:

  • The odd FOR /F option string is a result of needing to set the token delimiter to a quote. It also sets the token number to 2, and disables the EOL feature by setting EOL to nothing.
  • Batch does not have a formal array concept, though it can be emulated. But for this application I just defined a variable containing all possible values, and then use delayed expansion search and replace to remove the found site. If the result is different than the starting string, then the site was found so the uninstall process must be called.
dbenham
  • 651
  • 4
  • 12
  • You are a beautiful person, dbenham. Thank you that did the trick! I’m also really digging your flute improvisations as well…so thank you again. – Javagenki Jul 29 '16 at 16:02
  • @Javagenki - Thanks :-) Don't be afraid to "like" your favorite tracks. And / Or you can follow / subscribe to my music channels - I post new tracks fairly frequently. – dbenham Jul 29 '16 at 16:46
  • As long as the site codes remain constant this is a great approach; is the site codes need to change on an unpredictable basis, You could run a find command against a text file to allow for real-time site code changes. – CoveGeek Aug 01 '16 at 16:43
  • @dbenham - Added (edited original question) another scenario that is baffling me. Any suggestions? Thanks! – Javagenki Aug 08 '16 at 17:59
  • @dbenham – I know this might be a little late but I’m honestly a little confused about the logic in your original answer. It seemed to work on a few of the test servers that I tried so I didn’t question it. Shouldn’t the following ‘neq’ be ‘equ’ since I am only uninstalling when the returned sitecode matches the SCCMSiteCodes? Sorry, I’m a real newbie when it comes to batch. if "!SCCMSiteCodes:'%%A'=!" neq "!SCCMSiteCodes!" If I can follow the logic here then it might help with my other issue when a sitecode has not been assigned to the server. Thanks! – Javagenki Aug 09 '16 at 17:52
  • @Javagenki - No, my code is correct. The SCCMSiteCodes is a list of all relevant site codes. The expansion attempts to replace the returned site code with nothing. If it succeeds, then the expansion result will not match the original variable, meaning it is a site code of interest. If there was no substitution, then the result matches the original value, meaning the site code is not of interest. – dbenham Aug 09 '16 at 19:12
  • @dbenham – That makes more sense - my lack of experience with batch made it seem counter-intuitive. Any idea how to catch that ERROR: for servers that don't have a sitecode. I'm working on it but I'm not sure if because it returns an error that I can't parse for the error. – Javagenki Aug 10 '16 at 20:22