Bulk update commandlines in SCCM-programs

Just a small script to update the commandline of a large number of programs in SCCM.

This script will change from “/q” to “/qb!”…

Set oLocator = CreateObject("WbemScripting.SWbemLocator")
Set oSccmWmi = oLocator.ConnectServer(".", "root\sms\site_C01", "", "")

Set oPrograms = oSccmWmi.ExecQuery("select * from SMS_Program where CommandLine LIKE '%msiexec%/q %'")

For Each oProgram In oPrograms
	WScript.Echo "Package: " & oProgram.PackageID & "  (" & oProgram.ProgramName & ")"
	WScript.Echo "Orginal: " & oProgram.CommandLine
	
	sNewCmd = Replace(oProgram.CommandLine, "/q", "/qb!")
	WScript.Echo "    New: " & sNewCmd
	
	Set oModProgram = oSccmWmi.Get("SMS_Program.PackageID='" & oProgram.PackageID & "'" & ",ProgramName='" & oProgram.ProgramName & "'")
	oModProgram.CommandLine = sNewCmd
	oModProgram.Put_ ' Comment out this line if you want to test
	Set oModProgram = Nothing
Next

5 comments

  1. riro

    Might add that you can change “anything” on a program with that code.

    For instance, if you want to change the description just add:
    oModProgram.Description = “My New Description”

    I used that with a couple of queries to filter out install/repair/uninstall and then put them in to different categories (Description).

  2. riro

    If you have a command line like:
    msiexec.exe /i MyFile.MSI TRANSFORMS=MyFile.MST /l X:\Path\MyFile.log /m /qr

    And you want to remove the part “/l …. .log”

    Change:
    sNewCmd = Replace(oProgram.CommandLine, “/q”, “/qb!”)

    To:
    iLogStart = InStr(oProgram.CommandLine, “/l”)-1
    iLogStop = InStr(iStart, oProgram.CommandLine, “.log”) 4
    sNewCmd = Left(oProgram.CommandLine, iLogStart) & Right(oProgram.CommandLine, Len(oProgram.CommandLine)-iLogStop)

  3. Tim

    Love the scripts, I’m just starting out, hence this question!

    I’m trying to do something similar but performing a bulk find/replace on collection query statements. Say for example you have a bunch of collections targeting OUs that are all under one parent OU, you need to change the name of the parent OU and therefore need to update all the collection query statements with the same name change.

    I had a browse through WMI to see if I could find the relevant namespaces to allow me to modify your script above for this purpose, but I didn’t have any luck.

    Any ideas? Any help would be much appreciated!! 🙂

  4. Tim

    After a bit of messing around, this is what I came up with, needless to say that it doesn’t work! HELP! 🙂

    Set oLocator = CreateObject(“WbemScripting.SWbemLocator”)
    Set oSccmWmi = oLocator.ConnectServer(“.”, “root\sms\site_XXX”, “”, “”)

    Set oCollectionRuleQueries = oSccmWmi.ExecQuery(“select * from SMS_CollectionRuleQuery where QueryExpression LIKE ‘%OLD_TEXT%'”)

    For Each oCollectionRuleQuery In oCollectionRuleQueries
    WScript.Echo “Rule: ” & oCollectionRuleQuery.RuleName
    WScript.Echo “Orginal: ” & oCollectionRuleQuery.QueryExpression

    sNewQuery = Replace(oCollectionRuleQuery.QueryExpression, “OLD_TEXT”, “NEW_TEXT”)
    WScript.Echo ” New: ” & sNewQuery

    Set oModQuery = oSccmWmi.Get(“SMS_CollectionRuleQuery.QueryID='” & oCollectionRuleQuery.QueryID & “‘” & “,RuleName='” & oCollectionRuleQuery.RuleName & “‘”)
    oModQuery.QueryExpression = sNewQuery
    oModQuery.Put_ ‘ Comment out this line if you want to test
    Set oModQuery = Nothing
    Next

  5. riro

    Tim, I’m not sure that it is that easy to change Collection Queries… havn’t tired it, but if I remember correct you need do a for each loop on all collections and then go thru every query one by one…

    Here is a start on how to do the loop:
    http://blogs.technet.com/b/smsandmom/archive/2007/11/01/sms-2003-getting-a-list-of-all-of-the-queries-for-each-of-your-collections.aspx

    Then I guess you can use that info to search/replace on the queryexpression and then update it.

    … a lot of “not sure” and “I guess”… 😛