Welcome to the Blogcast Repository Sign in | Join | Help
Search BlogCastRepository.com for:
in Search

246 BlogCasts in The BlogCast Repository!

SMS Software Distribution Cleanup script

Last post 08-24-2006 10:18 AM by Matt Broadstock. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 08-22-2006 12:23 PM

    SMS Software Distribution Cleanup script

    (drop me a PM with your email addy if you want me to send this to you directly...I noticed the forum dropped the PIPE symbol and I don't know what else...)

    Here's the script that we cobbled together to search and cleanup user shortcuts and a few other things after a third party software distribution.

    This is probably more complicated than it needs to be.  Our initial thought was to scan the entire c:\documents and settings directory to find/replace shortcuts.  That approach became troublesome because it ran into file locks and it was very resource intensive.

    This *attempt* was written to check the registry for the profile paths, disregard the systemprofile, network service and help assistance profiles that the registry returns...affix specific search paths to those profiles, find the particular shortcuts and acquire new ones from a file share.

    It logs to c:\scriptLog.log

    There is also some crap at the end of the script that checks the registry and updates some settings for the new program version.

    If I remember correctly, a big problem we had with this script is that it seemed to skip over profiles and directories and not even scan them!  We never managed to get this thing to where we wanted it so we ended up manually updating user shortcuts.

    Some of the variable names are going to be screwy because it's a combination of scripts that were pulled off the internet, work I did and work the other admin did.  There may be a fair amount of crap in the script that does absolutely nothing...reviewing it I noticed there are two seperate "Create Object FileScripting" references

    EDIT--->I just noticed that the "arrFileSearch(0) and (1) the forum didn't take the lines correctly.
    It won't take the PIPE character.  should read:

    arrFileSearch(0) = "Shortcut(PIPE)\\myfileserver\install$\sungard\newshortcut\Someshortcut.Ink"

    On Error Resume Next

    Const HKEY_LOCAL_MACHINE = &H80000002
    Const ForWriting = 2
    Dim arrFileSearch(1)

    'shortcuts (renamed
    arrFileSearch(0) = "Shortcut\\myFileServer\install$\Sungard\NewShortuts\Some_Shortcut.lnk"
    arrFileSearch(1) = "Other_Shortcut\\myFileServer\install$\Sungard\NewShortuts\Some_Other_Shortcut.lnk"

    'for logging the script actions
    Set testFSO = CreateObject("Scripting.FileSystemObject")
    Set testFile = testFSO.CreateTextFile("C:\scriptLog.txt", True)
    Set FSO = CreateObject("Scripting.FileSystemObject")

    Dim strComputer
    strComputer = "."
    Dim strValueName
    Dim strValue
    Dim arrDirectories()
    intSize = 0

    'Array to hold the profile paths
    'pre-populate with "c:\documents and settings\all users"

    Dim arrProfilePaths()
    Dim intCount
    intCount = 0
    ReDim Preserve arrProfilePaths(intCount)
    arrProfilePaths(intCount) = "C:\Documents and Settings\All Users"
    intCount = intCount + 1


    Dim strStartingPoint
    Dim arrSearchPaths(3)

    'specific paths we wish to search to add onto the profile paths
    arrSearchPaths(0) = "\Desktop"
    arrSearchPaths(1) = "\Application Data\Microsoft\Internet Explorer\Quick Launch"
    arrSearchPaths(2) = "\Application Data\Microsoft\Office\Shortcut Bar"
    arrSearchPaths(3) = "\Start Menu"
    Dim strResults

     'check the registry
    Set objRegistry=GetObject("winmgmts:\\" & _
        strComputer & "\root\default:StdRegProv")

    Set FSO = CreateObject("Scripting.FileSystemObject")
     
    strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
    objRegistry.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubkeys
     
    For Each objSubkey In arrSubkeys
        strValueName = "ProfileImagePath"
        strSubPath = strKeyPath & "\" & objSubkey
        objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE,strSubPath,strValueName,strValue
     

    'disregard the system profile, local service, network service and help assistant profiles
       
     If strValue = "C:\WINDOWS\system32\config\systemprofile" Or strValue = "C:\Documents and Settings\LocalService" Or strValue = "C:\Documents and Settings\NetworkService" Or strValue = "C:\Documents and Settings\HelpAssistant" Then
     
    'Write to profile paths array
     Else
         ReDim Preserve arrProfilePaths(intCount)
            arrProfilePaths(intCount) = strValue
            intCount = intCount + 1
        End If
    Next

    For Each strProfilePaths in arrProfilePaths
        For Each strSearchPaths in arrSearchPaths
            strStartingPoint = strProfilePaths & strSearchPaths
            If FSO.FolderExists(strStartingPoint) Then
                ShowSubfolders FSO.GetFolder(strStartingPoint)
            End If
        Next
    Next


    'Recursive Look for all sub folders starting at the starting point that are not hidden folders
    Sub ShowSubFolders(Folder)
        'Checks to see if the folder is hidden
    '    If Folder.Name = "TEMP" Or Folder.Name = "Temp" Or Folder.Name = "Temporary Internet Files" Or Folder.Name = "NetHood" Or Folder.Name = "Identities" Or Folder.Name = "Credentials" Or Folder.Name = "CryptnetUrlCache" Or Folder.Name = "HelpAssistant" Or Folder.Name = "LocalService" Or Folder.Name = "NetworkService" Or Folder.Name = "cache" Or Folder.Name = "Installer" Or Folder.Name = "History" Then
    '    Else
         For Each Subfolder in Folder.SubFolders
                'Changes the array size
               
                ReDim Preserve arrDirectories(intSize)
                arrDirectories(intSize) = Subfolder.Path
                intSize = intSize + 1
                ShowSubFolders Subfolder
            Next
    '    End If
    End Sub


    ''Gets all of the files in the Directories previously found
    For Each Directory in arrDirectories
        testFile.Writeline(Directory & vbCRLF)
        For Each objFile in FSO.getfolder(Directory).Files
            'Checks to make sure there is a file
             If IsObject(objFile) Then
                'Goes through the array of items to search for
                For Each strFile in arrFileSearch
                    Dim arrFileSearchSplit
                    strFileSearchSplit = Split(strFile, "|")
                    'Checks to make sure that the length of the file found and
                    'the lenght of the file to search for are at least the same
                   If Len(objFile.Name) >= Len(strFileSearchSplit(0)) then
                        'Checks the to see if the string to search contains the string to search for
                        If InStr(1, objFile.Name, strFileSearchSplit(0)) > 0 And objFile.Type = "Shortcut" Then
                               strResults = strResults & "Found One " & ObjFile.Path & ":" & objFile.Name & vbCRLF
                               FSO.DeleteFile ObjFile.Path, True
                               FSO.CopyFile strFileSearchSplit(1), Directory & "\", True                      
                        End If
                    End If
                Next
             End If
        Next
    Next
    '''Deletes the Old AppFolder if it exists
    If FSO.FolderExists("C:\SomeFolder") Then
        FSO.DeleteFolder("C:\SomeFolder")
    End If
    ''Deletes the BiTech Folder if it exists
    If FSO.FolderExists("C:\AnotherFolder") Then
        FSO.DeleteFolder("C:\AnotherFolder")
    End If
    If FSO.FolderExists("C:\YetAnother") Then
       FSO.DeleteFolder("C:\YetAnother")
    End If

     

    testFile.Close

    'THE FOLLOWING SECTION CHECKS REGISTRY AND UPDATES OPTIONS FOR NEW 'PROGRAM VERSION

    On Error Resume Next

    Const HKCU = &H80000001
    Const HKLM = &H80000002

    strComputer = "."
    Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

    ' check HCLM
    strkeyPath = "SOFTWARE\SomeVendor\SomeProgram"
    valueName = "someValue"
    objReg.GetDWORDValue HKLM, strKeyPath, ValueName, dwValue

    If IsNull(dwValue) or dwValue = 1 Then
    objReg.SetDWORDValue HKLM, strkeyPath, ValueName, 0

    End If

    ' Check HCKU
    strKeyPath = "Software\SomeVendor\SomeProgramOptions\General"
    ObjReg.GetDWORDValue HKCU, strKeyPath, ValueName, dwValue

    If IsNull(dwValue) or dwValue = 1 Then
    ObjReg.SetDWORDValue HKCU, strKeyPath, ValueName, 0

    End If
    FSO = Nothing
    Err.Clear()

    • Post Points: 20
  • 08-23-2006 1:55 PM In reply to

    Re: SMS Software Distribution Cleanup script

    Just to clarify on this... you know the name of the shortcut you want to replace? You don't really care about the target path for each shortcut, you just want to replace shortcuts with a couple of specific names with the new ones from your file share, right?
    • Post Points: 20
  • 08-23-2006 2:56 PM In reply to

    Re: SMS Software Distribution Cleanup script

    Yes exactly...by the way, sorry for the nastiness of the script.  I started looking it over yesterday and it is EL' NASTY!

    I did change some stuff in the script and took out some of my site specific things.  But yes, the idea is, when a piece of software is updated and the shortcuts are no longer valid, to be able to search for those shortcuts and replace them with updated ones.
    • Post Points: 20
  • 08-23-2006 5:32 PM In reply to

    Re: SMS Software Distribution Cleanup script

    Try this. See if it works slower or faster than what you had before. We can add the profile stuff back in but I think it will probably be a lot faster with some of the other improvements. Just make sure that you Dim arrFileSearch with the right number depending on how many files you want to search for.

     

    'On Error Resume Next
    Option Explicit

    'Dim strShortcut1, strShortcut2
    Dim strRootFolder, strShortcutSource
    Dim FSO, objRootFolder
    Dim testfile

    Const ForWriting = 2

    'arrFileSearch(0) = "Shortcut\\myFileServer\install$\Sungard\NewShortuts\Some_Shortcut.lnk"
    'arrFileSearch(1) = "Other_Shortcut\\myFileServer\install$\Sungard\NewShortuts\Some_Other_Shortcut.lnk"

    'arrFileSearch(0) = "Internet Explorer.lnk"
    'arrFileSearch(1) = "Windows Explorer.lnk"

    Dim arrFileSearch(2)
    arrFileSearch(0) = "Copy of Tour Windows XP.lnk"
    arrFileSearch(1) = "Copy of Synchronize.lnk"
    arrFileSearch(2) = "Copy of Synchronize2.lnk"

    strRootFolder = "c:\documents and settings"
    'strShortcutSource = "\\myFileServer\install$\Sungard\NewShortuts\"
    strShortcutSource = "\\w009-0281\c$\test\"

    'for logging the script actions
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set testFile = FSO.CreateTextFile("C:\scriptLog.txt", True)

    Set objRootFolder = FSO.GetFolder(strRootFolder)
    Recurse objRootFolder, arrFileSearch


    testFile.Close
    WScript.Quit
    '*******************END OF SCRIPT*************************

    Function Recurse(oFolder, arrFileSearch)


     On Error Resume Next
     dim subfolders,files,folder,file,i,strFile, strCurrFolderPath

     Set subfolders = oFolder.subfolders

     'Wscript.Echo oFolder.Path
     'For Each folder in subfolders
     ' Wscript.Echo "folder" & vbTab & folder.Name
     'Next
     'WScript.Echo VarType(arrFileSearch)
     strCurrFolderPath = oFolder.Path
     For i = LBound(arrFileSearch) To UBound(arrFileSearch)
      strFile = arrFileSearch(i)
      If FSO.FileExists(strCurrFolderPath & "\" & strFile) Then
       'strIDSource = oFolder.Path & "\" & strFile
       WScript.Echo strCurrFolderPath & "\" & strFile
       Err.Clear
       FSO.CopyFile strShortcutSource & strFile, strCurrFolderPath & "\" & strFile, True
       If Err.Number <> 0 Then
        WScript.Echo "ERROR: Couldn't copy '" & strFile & "' from " & strShortcutSource & _
         " to " & strCurrFolderPath
       End If
       'Exit Function 'Comment this line out if you want to find all copies of a file
      End If
     Next

     'Recurse all of the subfolders.
     For Each folder in subfolders
      Recurse folder, arrFileSearch
     Next

     Set subfolders = Nothing
     Set files = Nothing
     On Error Goto 0

    End Function

    • Post Points: 5
  • 08-24-2006 9:50 AM In reply to

    Re: SMS Software Distribution Cleanup script

    that actually looks like it runs pretty well.  I appreciate the help.  I'll let you know how it turns out when I have a chance to actually run a thorough test where I push it out along with a software package.

    I'm also wondering, do you use scripting with SMS to do any sort of administrative tasks?  I was thinking of the possibilities...for example, setup a script to defrag user's computers and put it out as an advertisement and let them run it...for those who are too inept to actually run it themselves.  Could also write something to clean up temp folders / files and advertise that.

    You ever do anything like that or am I a retard?

    • Post Points: 20
  • 08-24-2006 10:04 AM In reply to

    Re: SMS Software Distribution Cleanup script

    Yep, I do lot's of scripting against SMS and just about anything else.  My job is probably 30-50% scripting right now. It's the lazy (efficient) way to get most things done.

    As far as defrag, you don't need a script! As long as you are using XP, just use Defrag.exe. It is already on the systems and will run in the background. You could schedule it to run monthly for your users or something..

    I don't think it would be too difficult to write something to clean up temp folders. You'd just have to identify what folders were ok to cleanup and if there was any criteria to consider when deleting files.

    • Post Points: 5
  • 08-24-2006 10:13 AM In reply to

    Re: SMS Software Distribution Cleanup script

    When I was in the Marines I had the chance to do alot of development work.  I got really big into VB and web applications...it was fun.

    In my new job I haven't worked with scripting for a good 5 years...not that I was really good back in the day, but I was decent.

    I'd be interested in seeing some of the cooler scripts you have if you are willing to share.  I need to get back into it to really leverage SMS and Domain admin.

    thanks bro.

    • Post Points: 20
  • 08-24-2006 10:18 AM In reply to

    Re: SMS Software Distribution Cleanup script

    I have a few on my blog here. http://blogcastrepository.com/blogs/mattbro/archive/category/1030.aspx

    I have some other ones at www.smsutils.com. Go to the Community Downloads tab.

    I have a bunch of other ones I just haven't gotten around to posting anywhere yet...

    • Post Points: 5
Page 1 of 1 (8 items)