Solved

install msi file on remote machine using vb.net code

Posted on 2009-05-06
8
5,380 Views
Last Modified: 2013-11-14
I want to run an msi installation file on remote servers from a vb.net application on my machine. I can directly access these machines using the path: \\servername\ and I have administrative rights. Attached is the code I'm trying to use. First, I copy the msi unto the remote server which works but the code to execute the msi doesn't work. I'm using the process.start() method to start the windows msiexec program but it doesn't seem to work. What am I doing wrong?

Also, how can I check if the installation was successfull via my vb.net application and write to a log file.

Thanks

San
Private Sub Myfunc()
 

        Dim fi As System.IO.FileInfo = Nothing
 

        Dim fileWithServerNames As String = System.Environment.CurrentDirectory & "\ListOfServers.txt"

        Dim sr As StreamReader = New StreamReader(fileWithServerNames)
 

        Dim tempServerPath As String = ""

        Dim myMSIFilePath As String = ""

        Dim myMSIFileName As String = ""
 

        'Get the msi file from local machine

        Dim mylocalFilePath As String() = Directory.GetFiles(System.Environment.CurrentDirectory & MSI_FOLDER)

        myMSIFilePath = mylocalFilePath(0)

        myMSIFileName = getFileName(myLocalFilePath(0))
 
 

        Try

            Do While sr.Peek() >= 0
 

                tempServerPath = "\\" & sr.ReadLine() & TEMP_PATH
 

                If Directory.Exists(tempServerPath) Then
 

                    'First copy the MSI file to the remote server

                    File.Copy(myMSIFilePath, tempServerPath & "\" & myMSIFileName, True)

                    If File.Exists(tempServerPath & "\" & myMSIFileName) Then

                        writeToLog("==MSI copied to " & tempServerPath)
 

                        'Next, install the msi on the server

                        Dim procinfo As ProcessStartInfo = New ProcessStartInfo()

                        procinfo.FileName = "msiexec.exe"

                        procinfo.Arguments = "/q /i " & tempServerPath & "\" & myMSIFileName
 

                        Dim proc As New System.Diagnostics.Process

                        proc = System.Diagnostics.Process.Start(procinfo)
 

                        'Wait for the installation to finish

                        proc.WaitForExit()
 

                        'Code to check if the installation successful???

                        '?

                    Else

                        writeToLog("==MSI copy failed to " & tempServerPath)

                    End If
 

                    writeToLog(" ")
 

                Else

                    writeToLog(tempServerPath & "==folder does not exist")

                End If
 

            Loop

            sr.Close()
 

        Catch ex As Exception

            writeToLog(tempServerPath & "==>file read failed" & vbLf & ex.Message)

        End Try
 

    End Sub

Open in new window

0
Comment
Question by:Sanmarie
  • 4
  • 4
8 Comments
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 24324575
1. what credentials has the process on the remote machine? you don't specify any. Can you start there  something simple, for example cmd.exe, using this code?

2. Maybe it's because msiexec on remote machine is supposed to find the msi file on the same share that you are using.

3. I would use Process Monitor on remote machine.
0
 

Author Comment

by:Sanmarie
ID: 24324963
1. What credentials? YOu mean if it has execute rights? Yes, since I'm accessing as administrator. I'll try the cmd.exe and get back to you

2. Well I copied it to C:\temp on the remote machine and tried to run it from there

3. ?

Bear with me if I don't understand the basics. I just know vb.net and we want to install the msi on all the servers next week and I want to run a script instead of logging on to each machine and installing.

Thanks

San
0
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 24325405
1. when msiexec is launched on the remote machine, it runs as some user. Which one?

2. Not so. On the remote machine you run is as
procinfo.Arguments = "/q /i " & tempServerPath & "\" & myMSIFileName

i.e. msiexec on remote machine receives the path of the msi file in the share.

3. http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx

finally,

> we want to install the msi on all the servers

if this is windows network, the best way to achieve this is by publishing your installation in the active directory, making it available to those servers.
0
 

Author Comment

by:Sanmarie
ID: 24326260
Okay. Sorry for the delayed response but I was trying a few things. I added the following credentials to the code so it would use my administrative credentials: (See the attached code)

The thing is, it's launching the msiexec program on my local machine. As you can tell, I don't know what I'm doing.

I'll try your other suggestions as a last resort but I saw vb6 code online that was able to do this so I think vb.net should be able to do it too. I'll try some more and if it doesn't work then I'll have to use AD or your recommended microsoft link.

Thanks

San



procinfo.UseShellExecute = False

        procinfo.LoadUserProfile = True

        procinfo.UserName = "myUsrname"

        procinfo.Domain = "myDomain"

        procinfo.WorkingDirectory = strDestFolderPath
 

        'Security part

        Dim pwdString As New System.Security.SecureString

        Dim strPassword As String = "myStringPassword"
 

        ' Construct the SecureString password

        For Each c As Char In myStringPassword

            pwdString.AppendChar(c)

        Next
 

        procinfo.Password = pwdString
 

        Dim proc As New System.Diagnostics.Process

        proc = System.Diagnostics.Process.Start(procinfo)

        proc.Start()

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 40

Accepted Solution

by:
Vadim Rapp earned 500 total points
ID: 24326849
well, you are absolutely right:

================
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

Process Class

Provides access to local and remote processes and enables you to start and stop local system processes.
================

see http:Q_20659410.html .

Also, isn't it easier to run the installation on each machine from a common centralized share, rather than copy it to each machine?

You can also consider using psexec (google the word) which does exactly this thing, plus is channels stdout output back to you. But the best advise is to set it up by group policy, especially if you do this occasionally.
0
 

Author Comment

by:Sanmarie
ID: 24327586
Right. The Process class is for the LOCAL system as you have said. No wonder it's not working.

I'm writing the code using WMI (Windows Management Information) and I will get back to you.

>>Also, isn't it easier to run the installation on each machine from a common centralized share, rather >>than copy it to each machine?
Hmm, I guess I could connect to each from my local machine and run it instead of copying it to each server first. Is this what you mean? I have access to all the servers from my local machine but I'm not sure I do from a common centralized share. I'd have to go through the network admin group to do that and if I can get the code to work then I won't bother.

Thanks. I'll also look into the psexec later.

I'm learning a lot so far. I will keep you posted once I'm done with the code. Thanks for sticking with me on this.

San
0
 
LVL 40

Expert Comment

by:Vadim Rapp
ID: 24329670
> Hmm, I guess I could connect to each from my local machine and run it instead of copying it to each server first. Is this what you mean?

no. This:

1. put the msi file on some server share seen by all servers where you want to deploy it; can even be your own machine, for example \\mymachine\myshare\test.msi

2. then on each server run

msiexec /i \\mymachine\myshare\test.msi

this way, you don't have to copy to each machine, they all take the file from one centralized share.

0
 

Author Comment

by:Sanmarie
ID: 24330766
Cool!
I was able to install the msi on test servers from my test vb.net application using the example from here:
http://social.msdn.microsoft.com/Forums/en-US/tfsbuild/thread/1b3e1c46-b70d-4caf-86fa-45f3b3c0f8a7

I tweaked the example above to suit my needs and it installs perfectly.

>>put the msi file on some server share seen by all servers where you want to deploy it; can even be >>your own machine, for example \\mymachine\myshare\test.msi
Excellent. Now I get what you mean. I will do this

Thank you so much for helping me Vadimrapp1. You deserve all the points

San


0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now