Reinstall all virtual applications deployed by SCCM 2007

image_pdfimage_print

The client I am working for now, uses SCCM 2007 to deploy App-V 4.6 packages. In the past it was needed to re-install both the SCCM 2007 and App-V client. As a result that the user has to re-install all the virtual applications.

To avoid this I made a vbscript that reruns every advertisement that points to a virtual application. This script is based on the following article on MyITForum: Re-running advertisements on an Advanced Client.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
' ================================================================================================
' Installs all the AppV Packages again
' Created by Willem-Jan Vroom
' Version history:
'
' v0.0.1
'   Initial version
'
' v.0.0.2
'   Added support to rerun all the advertisements for the virtual packages. 
' ================================================================================================
 
' ------------------------------------------------------------------------------------------------
' Declare the most variables. 
' ------------------------------------------------------------------------------------------------
 
  Option Explicit
 
  Dim objShell               : set objShell         = WScript.CreateObject("WScript.Shell")
  Dim objNetWork             : set objNetWork       = Wscript.CreateObject("WScript.Network")
  Dim objLogFileFSO          : Set objLogFileFSO    = CreateObject("Scripting.FileSystemObject")
  Dim objFSO                 : Set objFSO           = CreateObject("Scripting.FileSystemObject")
  Dim objProcessEnv          : Set objProcessEnv    = objShell.Environment("PROCESS")
  Dim objWMIService          : Set objWMIService    = GetObject("winmgmts:\\.\root\cimv2")
  Dim objReg                 : Set objReg           = GetObject("winmgmts:\\.\root\default:StdRegProv")
  Dim objLogFile
  Dim colListOfServices
  Dim objService 	
  Dim strCurrentDir          : strCurrentDir        = Left(Wscript.ScriptFullname, InstrRev(Wscript.ScriptFullname, "\"))
                               strCurrentDir        = Left(strCurrentDir,len(strCurrentDir)-1)
  Dim strCommand             : strCommand           = ""
  Dim strLogLocation         : strLogLocation       = "C:\WINDOWS\system32\Logfiles"
  Dim strcomputerName        : strcomputerName      = objProcessEnv("COMPUTERNAME")
  Dim strOutputFile          : strOutputFile        = strLogLocation & "\" & strcomputerName & "_ReinstallAllAppVPackages_" & Replace(FormatDateTime(Now(), 2),"/","-") & ".log"
  Dim strOS                  : strOS                = ""
  Dim strKeyPath             : strKeyPath           = ""
  Dim strArchitecture        : strArchitecture      = "x86"
  Dim strWindir              : strWinDir            = objShell.ExpandEnvironmentStrings("%WinDir%")
 
 
  Dim valOSBuildNumber, valResult
  Dim objItem, colItems
  Dim arrSubKeys, objSubKey
 
  Const ForWriting           = 2
  Const ForReading           = 1
  Const ForAppending         = 8
  Const OverWriteFiles       = True
  const HKEY_CURRENT_USER    = &H80000001
  const HKEY_LOCAL_MACHINE   = &H80000002
  Const HKEY_USERS           = &H80000003 
 
' ------------------------------------------------------------------------------------------------
' Detect the current OS architecture.  
' ------------------------------------------------------------------------------------------------
 
  if objFSO.FolderExists(strWindir & "\syswow64") Then
     strArchitecture = "x64"
  end if
 
' ------------------------------------------------------------------------------------------------
' If run on a x64 OS, then start the application again, but with the 32 bits wscript.exe.
' This is needed as the SCCM Parts are 32 bits only.   
' ------------------------------------------------------------------------------------------------
 
  if strArchitecture = "x64" and instr(lcase(wscript.FullName),"system32") > 0 Then
     strCommand = strWinDir & "\syswow64\wscript.exe " & chr(34) & wscript.scriptfullname & chr(34)
     valResult = objShell.Run (strCommand,0,True)
     wscript.quit (valResult)
  end if
 
' ------------------------------------------------------------------------------------------------
' Create the log location (if not exists)
' Open the logfile.  
' ------------------------------------------------------------------------------------------------
 
  CreateFolderStructure(strLogLocation)
  OpenLogFile()
  WriteToLog "- ACTION: script started."
 
' ------------------------------------------------------------------------------------------------
' Detect the current OS.  
' ------------------------------------------------------------------------------------------------
 
  Set colItems = objWMIService.ExecQuery("Select Caption,BuildNumber from Win32_OperatingSystem")
  For Each objItem in colItems
      strOS            = objItem.Caption
      valOSBuildNumber = objItem.BuildNumber
  Next
 
  WriteToLog("########## Details regarding operating system and logged on user        ##########")
  WriteToLog("Found Operating System:    " & strOS)
  WriteToLog("Found OS architecture:     " & strArchitecture)
  WriteToLog("Wscript engine:            " & wscript.FullName)
  WriteToLog("########## End details regarding operating system and logged on user    ##########")
  WriteToLog(" ")
 
' ------------------------------------------------------------------------------------------------
' Reinstall the App-V packages that have been installed by SCCM.  
' The location of the package are:
' on x86:
'  HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution\
'                     VirtualAppPackages\{PKG GUID}
' 
' on x64:
'  HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\SMS\Mobile Client\Software Distribution\
'                     VirtualAppPackages\{PKG GUID}
' ------------------------------------------------------------------------------------------------
 
  if strArchitecture = "x64" then
     strKeyPath = "SOFTWARE\Wow6432Node\Microsoft\SMS\Mobile Client\Software Distribution\VirtualAppPackages"
      else
     strKeyPath = "SOFTWARE\Microsoft\SMS\Mobile Client\Software Distribution\VirtualAppPackages"
  end if
  objReg.EnumKey HKEY_LOCAL_MACHINE, strKeyPath, arrSubKeys
  if IsArray(arrSubKeys) Then
     For Each objSubKey in arrSubKeys
         RerunAppVPackageAdvertisement(strKeyPath & "\" & objSubKey)
     Next
     else 
    WriteToLog("No AppV packages in SCCM have been found. Thus quitting.")
  end if
  CloseLogFile()
 
 
Sub OpenLogFile() 
 
' ------------------------------------------------------------------------------------------------
' Subroutine: OpenLogFile() 
' The name of the logfile is mentinoed in the variabele strOutputFile.
' ------------------------------------------------------------------------------------------------
 
  If objLogFileFSO.FileExists(strOutputFile) Then
     Set objLogFile = objLogFileFSO.OpenTextFile(strOutputFile, ForAppending)
         Else
     Set objLogFile = objLogFileFSO.CreateTextFile(strOutputFile)
  End If
 
End Sub
 
 
Function WriteToLog(sLogMessage)
 
' ------------------------------------------------------------------------------------------------
' Function: WriteToLog(sLogMessage)
' Writes an entry 'sLogMessage' in the logfile.
' ------------------------------------------------------------------------------------------------
 
  if instr(sLogMessage, "- ACTION: ") = 0 then sLogMessage = "          " & sLogMessage
  objLogFile.WriteLine("Time: " & now & "  " & sLogMessage)
 
End Function
 
Sub CloseLogFile()
 
' ------------------------------------------------------------------------------------------------
' Subroutine: CloseLogFile()
' Close the log file.
' ------------------------------------------------------------------------------------------------
  WriteToLog("########## End repairing all the AppV Packages.                         ##########")
  WriteToLog("- ACTION: script ended.")           
  objLogFile.Close
  Set objLogfileFSO = Nothing
 
End Sub
 
 
Sub CreateFolderStructure(strFolderNameToBeCreated)
 
' ------------------------------------------------------------------------------------------------
' Subroutine: CreateFolderStructure(strFolderNameToBeCreated)
' Creates the map as mentioned in strFolderNameToBeCreated.
' ------------------------------------------------------------------------------------------------
 
  Dim arrFoldersTMP : arrFoldersTMP = split (strFolderNameToBeCreated,"\")
  Dim strFolder     : strFolder     = ""
  Dim objFolderTMP
 
  For Each objFolderTMP in arrFoldersTMP
      strFolder = strFolder & objFolderTMP
      If NOT objFSO.FolderExists(strFolder) Then
             objFSO.CreateFolder(strFolder)
      end If
      strFolder = strFolder & "\"
  Next
 
End Sub
 
Sub RerunAppVPackageAdvertisement(key)
 
' ------------------------------------------------------------------------------------------------
' Subroutine: RerunAppVPackageAdvertisement(key)
' Reinstalls the AppV application as mentioned in the key by rerunning the advertisement by
' checking the hive 'SCCM Advert IDs'. 
' This part is based on the following article:
' http://www.myitforum.com/articles/8/view.asp?id=6841
' ------------------------------------------------------------------------------------------------
 
  Dim arrSCCMAdvID            : objReg.GetMultiStringValue HKEY_LOCAL_MACHINE, key, "SCCM Advert IDs",       arrSCCMAdvID
  Dim strSoftGridPackageName  : objReg.GetStringValue HKEY_LOCAL_MACHINE,      key, "SoftGrid Package Name", strSoftGridPackageName
  Dim strSCCMAdvID            : strSCCMAdvID=arrSCCMAdvID(0)
  Dim strOldRerunBehavior
  WriteToLog ("---------------------------------------------------------------------------------")
  WriteToLog ("Processing SCCM AppV Package: " & strSoftGridPackageName & " with advertisement ID " & strSCCMAdvID & ".")
 
  WriteToLog ("Step 1: Get the ID of the ScheduledMessage on the client.")
  Dim objNMS                  : Set objNMS        = GetObject("winmgmts:\\.\root\ccm\policy\machine\actualconfig")
  Dim objSched, objScheds     : Set objScheds     = objNMS.ExecQuery("Select * From CCM_Scheduler_ScheduledMessage")
  Dim strSchMsgID
 
  For Each objSched in objScheds
      if instr(objSched.ScheduledMessageID, strSCCMAdvID) > 0 Then
         strSchMsgID = objSched.ScheduledMessageID
         Exit For
      End if
  next
  WriteToLog ("        Found Scheduled Message ID: " & strSchMsgID)
 
  WriteToLog ("Step 2: Change the rerun behavior to 'RerunAlways'.")
  Set objNMS        = GetObject("winmgmts:\\.\root\ccm\policy\machine\actualconfig")
  Set objScheds     = objNMS.ExecQuery("select * from CCM_SoftwareDistribution where ADV_AdvertisementID = '" & strSCCMAdvID & "'")
  For Each objSched in objScheds
      strOldRerunBehavior = objSched.ADV_RepeatRunBehavior
      objSched.ADV_RepeatRunBehavior = "RerunAlways"
      objSched.Put_ 0
  Next
 
  WriteToLog ("Step 3: Rerun the advertisement.")
  Set objNMS        = GetObject("winmgmts:\\.\root\ccm")
  Dim objSMSClient             :  Set objSMSClient = objNMS.Get("SMS_Client")
  objSMSClient.TriggerSchedule strSchMsgID
  wscript.sleep 18000
 
  WriteToLog ("Step 4: Change the rerun behavior back to '" & strOldRerunBehavior & "'.")
  Set objNMS        = GetObject("winmgmts:\\.\root\ccm\policy\machine\actualconfig")
  Set objScheds     = objNMS.ExecQuery("select * from CCM_SoftwareDistribution where ADV_AdvertisementID = '" & strSCCMAdvID & "'")
  For Each objSched in objScheds
      objSched.ADV_RepeatRunBehavior = strOldRerunBehavior
      objSched.Put_ 0
  Next
  WriteToLog ("The rerun for " & strSoftGridPackageName & " has been completed.")
  WriteToLog ("---------------------------------------------------------------------------------")
  WriteToLog (" ")
 
End Sub

Permanent link to this article: https://www.vroom.cc/wordpress/reinstall-all-virtual-applications-deployed-by-sccm-2007/