none
Is this remotely correct?? RRS feed

  • Question

  • I was asked to create a vbscript  for work two days ago.  Never created one.  Still don't understand
    anything about scripting but it needed to be done.  I spent hours trying to
    figure out the correct format to run things.  Basically, I was asked to remove
    an old cisco anyconnect version and install the new
    version
    on all client computers as a startup script for GPO.  Plus I
    would add cisco anyconnect network manager as well.  Then I would take a config file and move it the proper directory.  Did I mention I do not
    know what the hell i'm doing.  Not sure why this was assigned to me but I feel
    like a fool to say no.  Any comments would help.  
    '=========================================================================================================================================
    '=== START SCRIPT                                                                                                                      ===
    '===                                                                                                                                   ===
    Option Explicit
    
    Const HKEY_LOCAL_MACHINE = &H80000002
    Dim CACProductName, CACProductKey, CACVersion, Msg, MsgBoxStyle, RegKey, CACProductKey, CACProductName, AMCProductKey, CACProductName, AMCProductKey, AMCProductName, AMCVersion, NAMProductKey, NAMProductName
    'CAC = Old Cisco AnyConnect
    'AMC = New Cisco AnyConnect
    'NAM = Network Access Manager
    '=========================================================================================================================================
    '=== START SUBs                                                                                                                        ===
    '===                                                                                                                                   ===
    '=========================================================================================================================================
    '=== START Check for Cisco AnyConnect Secure Mobility Client v. 3.1.03103                                                                                                ===
    '===                                                                                                                                   ===
    Sub GetSMCKey()
    
    Dim oReg, sPath, aKeys, sName, sKey, sVersion
    Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    
    sPath = "SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
    oReg.EnumKey HKEY_LOCAL_MACHINE, sPath, aKeys
    
    For Each sKey in aKeys
            oReg.GetStringValue HKEY_LOCAL_MACHINE, sPath & "\" & sKey, "DisplayName", sName, "DisplayVersion", sVersion
            If Not IsNull(sName, sDisplay) Then 
                If ((sName = "Cisco AnyConnect Secure Mobility Client") And (sDisplayVersion = 3.1.03103)) Then
    				CACProductKey = sKey
    				CACProductName = sName
    				CACVersion = sVersion
                End If
            End If
    Next
    End Sub
    '===                                                                                                                                   ===
    '=== END Check for AnyConnect Secure Mobility Client v. 3.1.03103                                                                                              ===
    '=========================================================================================================================================
    '=========================================================================================================================================
    '=== START Check for Cisco AnyConnect Network Access Manager                                                                          ===
    '===                                                                                                                                   ===
    Sub GetNAMKey()
    
    Dim oReg, sPath, aKeys, sName, sKey
    Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    
    sPath = "SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
    oReg.EnumKey HKEY_LOCAL_MACHINE, sPath, aKeys
    
    For Each sKey in aKeys
            oReg.GetStringValue HKEY_LOCAL_MACHINE, sPath & "\" & sKey, "DisplayName", sName
            If Not IsNull(sName) Then 
                If (sName = "Cisco AnyConnect Network Access Manager") Then
    				NAMProductKey = sKey
    				NAMProductName = sName
                End If
    		End If
    Next
    End Sub
    '===                                                                                                                                   ===
    '=== END Check for Cisco AnyConnect Network Access Manager                                                                           ===
    '=========================================================================================================================================
    '=========================================================================================================================================
    '=== START Check for Cisco AnyConnect 3.1.05170                                                                                   ===0
    '===                                                                                                                                   ===
    Sub GetAMCKey()
    
    Dim oReg, sPath, aKeys, sName, sKey, sVersion
    Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
    
    sPath = "SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
    oReg.EnumKey HKEY_LOCAL_MACHINE, sPath, aKeys
    
    For Each sKey in aKeys
            oReg.GetStringValue HKEY_LOCAL_MACHINE, sPath & "\" & sKey, "DisplayName", sName, "DisplayVersion", sVersion
            If Not IsNull(sName) Then 
                If ((sName = "Cisco AnyConnect Secure Mobility Client") And (sVersion = "3.1.05170")) Then
    				AMCProductKey = sKey
    				AMCProductName = sName
    				AMCVersion = sVersion
                End If
            End If
    Next
    End Sub
    '===                                                                                                                                   ===
    '=== END Check for Cisco AnyConnect 3.1.05170                                                                                       ===
    '=========================================================================================================================================
    '=========================================================================================================================================
    '=== START Uninstall Cisco AnyConnect 3.1.03103                                                                                   ===
    '===                                                                                                                                   ===
    Sub UninstallSMC(key, name, version)
    Dim cmd, objShell, iReturn, oshell
    
    cmd = "%SystemRoot%\System32\msiexec.exe /q/x " & key
    Set objShell = wscript.createObject("wscript.shell")
    
    objShell.LogEvent 0, "Removing the program [" & name & "] under Product Key [" & key & "]" & vbCrLf & "Executing command: " & vbCrLf & cmd
    
    iReturn=objShell.Run(cmd,1,TRUE)
     
    	If (iReturn = 0) Then
    		objShell.LogEvent 0, "Program [" & name & "] was successfully removed"
    	Else
    		objShell.LogEvent 0, "Failed to remove the program [" & name & "]."
    	End If
     
    Set objShell = Nothing  
    End Sub
    '===                                                                                                                                   ===
    '=== END Uninstall Cisco AnyConnect 3.1.03103                                                                                    ===
    '=========================================================================================================================================
    '===                                                                                                                                   ===
    '=== END SUBs                                                                                                                          ===
    '=========================================================================================================================================
    '=========================================================================================================================================
    '===== Overwrite existing Configuration.xml
    'if file does not exist in folder then copy file from source OR
    'if file does exist, overwrite existing file
    
    strFileToCopy = "\\mydomain.com\softdist\Software\CiscoISE\configuration.xml"
    strFolder = "C:\ProgramData\Cisco\"
    
    
    Const OverwriteExisting = TRUE
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
     
    If objFSO.FolderExists(strFolder) Then
      objFSO.CopyFile strFileToCopy, strFolder, OverwriteExisting  
    Else
      Wscript.Echo "Target Folder does not exist."
    End If
    '=========================================================================================================================================
    '=== START CALLs (This is the script's logic.)                                                                                         ===
    '=========================================================================================================================================                                                                                                                           
    
    Call GetSMCKey()
    
    	If Not (CACProductKey = "") Then
    		Call UninstallSMC(CACProductKey, CACProductName, CACVersion)
    	End If
    
    	
    AMCProductKey = ""
    AMCProductName = ""
    AMCVersion = ""
    
    Call GetAMCKey()
    
    	If (AMCProductKey = "") Then
    		Dim objWSH
    		Set objWSH = CreateObject("WScript.Shell")
    		'Now we need to produce "msiexec.exe /a "Msi file.msi" /quiet /norestart" for a silent MSI install
    		objWsh.Run "msiexec.exe /i " + Chr(34) + "\\mydomain.com\softdist\Software\CiscoISE\AnyConnectMobilityClient\anyconnect-win-3.1.05170-pre-deploy-k9.msi" + Chr(34) + " /quiet /norestart"
    	End If
    	
    	If (NAMProductKey = "") Then
    		Dim objWS
    		Set objWS = CreateObject ("WScript.Shell")
    		objWS.Run "msiexec.exe /i " + Chr(34) + "\\mydomain.com\softdist\Software\CiscoISE\AnyConnectMobilityClient\anyconnect-nam-win-3.1.05170-k9.msi" + Chr(34) + " /quiet /norestart"
    	Else
    		Wscript.Quit
    	End If
    '===                                                                                                                                   ===
    '=== END CALLs                                                                                                                         ===
    '=========================================================================================================================================
    '===                                                                                                                                   ===
    '=== END SCRIPT                                                                                                                        ===
    '====================================================================================================================== 

    Wednesday, December 10, 2014 6:26 PM

Answers

  • I was asked to create a vbscript  for work two days ago.  Never created one.  Still don't understand
    anything about scripting but it needed to be done.  I spent hours trying to
    figure out the correct format to run things. Did I mention I do not
    know what the hell i'm doing.  Not sure why this was assigned to me but I feel
    like a fool to say no.

    Sounds like your boss asked you to build a wooden cabinet to store things inside, with you having no experience whatsoever with woodwork. This will be one ugly jerry-built contraption. It takes a cabinet maker apprentice several months to learn the basic woodworking skills. Writing scripts is no different. In fact, if you have no experience at all in programming then it might take a bit more than a few months to create a robust, well written script. You should engage a consultant.

    Wednesday, December 10, 2014 6:59 PM

All replies