cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

VBScript Script not Running from MSI Package

:confused: I have created a script which searches for a value in a text file and replaces it with the computername varible. The script excutes fine, but when you run it from a msi package it replaces the value with blank not the computer name. I have tried moving it around in the sequence and run it as a deferred and immediate excution custom action. I then change the script to msgbox the computername this does not work for some reason from install the msi im baffled can anyone suggest anything see script below.

Dim FileName, Find, ReplaceWith, FileContents, dFileContents

On Error Resume next

Set objWshNetwork = WScript.CreateObject("WScript.Network")

ComputerName = objWshNetwork.ComputerName

MsgBox ComputerName

Find = "COMPUTERNAME"
ReplaceWith = ComputerName
FileName = "c:\Var\Triarch\ipcroute"

'Read source text file
FileContents = GetFile(FileName)

'replace all string In the source file
dFileContents = replace(FileContents, Find, ReplaceWith, 1, -1, 1)

'Compare source And result
if dFileContents <> FileContents Then
'write result If different
WriteFile FileName, dFileContents

' Wscript.Echo "Replace done."
If Len(ReplaceWith) <> Len(Find) Then 'Can we count n of replacements?
' Wscript.Echo _
' ( (Len(dFileContents) - Len(FileContents)) / (Len(ReplaceWith)-Len(Find)) ) & _
' " replacements."
End If
Else
' Wscript.Echo "Searched string Not In the source file"
End If

'Read text file
function GetFile(FileName)
If FileName<>"" Then
Dim FS, FileStream
Set FS = CreateObject("Scripting.FileSystemObject")
on error resume Next
Set FileStream = FS.OpenTextFile(FileName)
GetFile = FileStream.ReadAll
End If
End Function

'Write string As a text file.
function WriteFile(FileName, Contents)
Dim OutStream, FS

on error resume Next
Set FS = CreateObject("Scripting.FileSystemObject")
Set OutStream = FS.OpenTextFile(FileName, 2, True)
OutStream.Write Contents
End Function
(5) Replies
You hav to know That MSI doesn't recognize wscript so don't write:

Set objWshNetwork = WScript.CreateObject("WScript.Network")

but write

Set objWshNetwork = CreateObject("WScript.Network")

To have the computername you can do this:

Set WshShell = CreateObject("Wscript.Shell")

Computer = WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")

:cool:
Why even use WScript when you have the session object to access the ComputerName property?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/computername.asp
Thanks OMSTEF works great ...

Chris Paint what are you talking about anyway ?
As Chris Paint says, you can also use this example

Computer = Session.Property("COMPUTERNAME")

But only with CA in Immediate mode
Correct, for deferred custom actions you schedule an immeadiate custom action just prior to your CA and set property CustomActionData property to [ComputerName]. Then in your deferred CA you can just say Session.Property("CustomActionData").

Also if it is a deffered custom action there shouldn't be any Wscript.Echo/MsgBox or any other kind of modal and/or visual component since deferred custom actions only run in the execute sequence. If the CA encounters an error it either needs to handle the execption or log the problem and initiate a rollback. It's not pretty when your pushing a program to 15,000 machines using a tool like SMS2003 in fully silent mode and the package just waits for a user input that will never happen. Eventually the package/progam timeout limit occurs and the program is terminated leaving the machine in a half configured state.