Category Archives: AppV5

Changing or virtualizing the Hosts file with AppV 5

Sometimes, applications require specific host file entries. Often you’d probably be able to get around using DNS to resolve the entries the application really needs. But when you can’t, and you want to virtualise your application using AppV 5, it’ll use the hosts file of the OS instead of the hosts file in the virtualized file system.

To get around this, we can let the AppV client fire off a script to modify the actual OS hosts file upon registration of the AppV application. This is done by modifying the DeploymentConfig.xml file and adding a script to your package, detailed descriptions of how this works can be found here. Basically, you add this between the <Machinescripts> tags in the DeploymentConfig.xml file, example:

<AddPackage>;
 <Path>c:\windows\system32\wscript.exe</Path>
 <Arguments>{AppVPackageRoot}]\Scripts\hostfile_edit.vbs</Arguments>
 <Wait RollbackOnError="true" Timeout="10"/>
 </AddPackage>

The VB code in hostfile_edit.vbs that does the actual work is:

Const ForReading = 1, ForWriting = 2, ForAppending = 8
 Set fso = CreateObject("Scripting.FileSystemObject")
 Set WshShell = CreateObject("WScript.Shell")
 WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")
 HostsFile = WinDir & "\System32\Drivers\etc\Hosts"
 Set filetxtR = fso.OpenTextFile(HostsFile, ForReading, True)
 DNSEntry1 = "10.0.0.1 HOSTNAME #DESCRIPTION"
If (checkHostfile(filetxtR, DNSEntry1) = True) Then
 WScript.Quit(0)
 Else
 Set filetxtA = fso.OpenTextFile(HostsFile, ForAppending, True)
 filetxtA.WriteLine (DNSEntry1)
 filetxtA.Close
 End If
filetxtR.Close
 WScript.Quit(0)
Public Function checkHostfile(filetxt, lineToCheckFor)
 checkHostfile = False
 Do Until filetxt.AtEndOfStream
 s = filetxt.readline
 If (s = lineToCheckFor) Then
 checkHostfile = True
 Exit Function
 End If
 Loop
 filetxt.Close
 End Function