' SoftGridPreloadApps 1.3 [20070720] ' (c) 2007 Kalle Saunamäki (kalle@saunamaki.fi - http://www.virtualapp.net/), All rights reserved ' ' This script will check for and preload only those SoftGrid applications currently not 100% in cache for the user ' Additional checks can be enabled (below, using script parameters) to prevent loading attempts when being offline etc. ' ' Run the script either with cscript.exe or wscript.exe (more useful for silent operation): ' wscript.exe //nologo SoftGridPreloadApps.vbs Option Explicit Dim Silent, SoftgridMimePath, SoftgridMimeQueryPath, SoftgridMimeLoadPath, AppsListTempFile Dim SoftGridServerAddress, CheckAvailability, CheckPerAppAvailability, CheckTimeoutMs, CheckOfflineState ReDim AppsToPreload(0) ReDim AppServerSource(0) Dim WshShell, WshFSO, oExec ' Script parameters ' ------------------------------------------------ ' Change to False to get error/status messages Silent = True ' Change to False to disable offline -mode check (for pre 4.x series client) CheckOfflineState = True ' Change to True to run global server availability check before any loads CheckAvailability = False ' For global server availability check, change to reflect your SoftGrid Server network address ' Applies only if CheckAvailability (below) is set to "True" SoftGridServerAddress = "my.softgrid.server.address" ' Change to True to run pre-load server availability check for each application ' NOTE: causes script to potentially run much longer! CheckPerAppAvailability = False ' Change to larger value if network conditions cause significant latency (potentially failed availability detection) ' Applies only if CheckAvailability or CheckPerAppAvailability is set to "True" CheckTimeoutMs = 30 ' ------------------------------------------------ Function CheckServerAvailability (ServerAddress) Dim PingCommand PingCommand = "ping -n 1 -l 10 -w " & CheckTimeoutMs & " " & ServerAddress Set oExec = WshShell.Exec(PingCommand) Do While oExec.Status = 0 WScript.Sleep 100 Loop If oExec.ExitCode <> 0 Then CheckServerAvailability = False Else CheckServerAvailability = True End If End Function Function DetectOfflineMode Dim OnlineState On Error Resume Next OnlineState = WshShell.RegRead("HKLM\SOFTWARE\Softricity\SoftGrid Client\CurrentVersion\Network\Online") On Error Goto 0 If IsEmpty(OnlineState) Then DetectOfflineMode = False Else If OnlineState = "1" Then DetectOfflineMode = False Else DetectOfflineMode = True End If End If End Function Function ReadSoftgridPath Dim ClientPath On Error Resume Next ClientPath = WshShell.RegRead("HKLM\SOFTWARE\Softricity\SoftGrid Client\CurrentVersion\Configuration\InstallPath") On Error Goto 0 ReadSoftgridPath = ClientPath & "\sftmime.exe" End Function Function ParseApps Dim AppLine, AppDetails, AppFile, OSDFile, OSDLine, GuidStart, GuidEnd, PkgGuid Dim ServerAddrStart, ServerAddrEnd ReDim GuidList(0) Dim temp Dim CodebaseDetected, EncodingChange, BadOSD AppsListTempFile = WshShell.ExpandEnvironmentStrings("%temp%") & "\~sgapps.lst" SoftgridMimeQueryPath = SoftgridMimePath & " query obj:app /log " & AppsListTempFile ' Query applications Set oExec = WshShell.Exec(SoftgridMimeQueryPath) Do While oExec.Status = 0 WScript.Sleep 100 Loop If (WshFSO.FileExists(AppsListTempFile)) Then Set AppFile = WshFSO.OpenTextFile(AppsListTempFile, 1) Do Until AppFile.AtEndOfStream AppLine = AppFile.Readline AppDetails = Split(AppLine , Chr(9)) If AppDetails(5) <> "100%" Then If (WshFSO.FileExists(AppDetails(2))) Then On Error Resume Next CodebaseDetected = False EncodingChange = False BadOSD = False Set OSDFile = WshFSO.OpenTextFile(AppDetails(2), 1, False, 0) Do Until CodebaseDetected = True Or BadOSD = True If OSDFile <> Empty Then Do Until OSDFile.AtEndOfStream OSDLine = OSDFile.Readline If InStr(LCase(OSDLine)," 0 Then CodebaseDetected = True GuidStart = InStr(LCase(OSDLine),"guid=") + 6 GuidEnd = InStr(GuidStart,OSDLine,"""") If CheckPerAppAvailability = True Then ServerAddrStart = InStr(InStr(LCase(OSDLine),"href="),OSDLine,"/") + 2 ServerAddrEnd = InStr(ServerAddrStart,OSDLine,":") End If PkgGuid = Mid(OSDLine,GuidStart,GuidEnd-GuidStart) If GuidStart <> GuidEnd Then temp = UBound(Filter(GuidList,PkgGuid)) If UBound(Filter(GuidList,PkgGuid)) = -1 Then ' This package has not been added to load queue yet AppsToPreload(UBound(AppsToPreload)) = """" & AppDetails(0) & " " & AppDetails(1) & """" ReDim Preserve AppsToPreload(UBound(AppsToPreload) + 1 ) GuidList(UBound(GuidList)) = PkgGuid ReDim Preserve GuidList(UBound(GuidList) + 1 ) If CheckPerAppAvailability = True Then AppServerSource(UBound(AppServerSource)) = Mid(OSDLine,ServerAddrStart,ServerAddrEnd-ServerAddrStart) ReDim Preserve AppServerSource(UBound(AppServerSource) + 1) End If End If End If Exit Do End If Loop OSDFile.Close() Else If Silent = False Then WScript.echo "Could not open cached OSD related to application " & AppDetails(0) & " " & AppDetails(1) & "!" End If End If If (CodebaseDetected = False) And (EncodingChange = False) Then ' reading ASCII/ANSI didn't produce anything sensible, try switching to Unicode EncodingChange = True Set OSDFile = WshFSO.OpenTextFile(AppDetails(2), 1, False, -1) Else If (CodebaseDetected = False) And (EncodingChange = True) Then ' This must be something else but OSD BadOSD = True End If End If Loop On Error GoTo 0 End If End If Loop AppFile.Close() ' Delete temporary file WshFSO.DeleteFile(AppsListTempFile) ParseApps = True Else ParseApps = False End If End Function Function PreloadApps Dim i Dim ContinueLoad If Ubound(AppsToPreload) = 0 Then If Silent = False Then WScript.echo "All applications fully cached already" End If Else For i = 0 to Ubound(AppsToPreload) - 1 ContinueLoad = True If CheckPerAppAvailability = True Then If CheckServerAvailability(AppServerSource(i)) = False Then If Silent = False Then WScript.echo "SoftGrid Server address for application " & AppsToPreload(i) & " cannot be reached!" End If ContinueLoad = False End If End If If ContinueLoad = True Then SoftgridMimeLoadPath = SoftgridMimePath & " load app:" & AppsToPreload(i) If Silent = False Then WScript.echo "Preloading: " & AppsToPreload(i) End If ' Load applications Set oExec = WshShell.Exec(SoftgridMimeLoadPath) Do While oExec.Status = 0 WScript.Sleep 100 Loop End If Next End If End Function ' Main body Dim ExecuteLoad ' Initialize WSH objects Set WshShell = CreateObject("WScript.Shell") Set WshFSO = CreateObject("Scripting.FileSystemObject") SoftgridMimePath = ReadSoftgridPath ExecuteLoad = True ' Check if SoftGrid Client exists on this machine If Not (WshFSO.FileExists(SoftgridMimePath)) Then If Silent = False Then WScript.echo "Could not found SoftGrid Client from this machine!" End If ExecuteLoad = False End If ' Check (optionally) if client is in offline mode currently (prevents loading apps) If CheckOfflineState = True And ExecuteLoad = True Then If DetectOfflineMode = True Then If Silent = False Then WScript.echo "Your SoftGrid Client is in offline mode!" End If ExecuteLoad = False End If End If ' Check (optionally) that SoftGrid Server we try to contact is available If CheckAvailability = True And ExecuteLoad = True Then If CheckServerAvailability(SoftGridServerAddress) = False Then If Silent = False Then WScript.echo "SoftGrid Server address cannot be reached!" End If ExecuteLoad = False End If End If If ExecuteLoad = True Then If ParseApps = True Then ' Issue load commands PreloadApps End If End If