Anuj75
asked on
Vbscript to Install Agent.exe on all Remote Windows Machine without Admin Rights
Hi
I am new to Vbscript, can you please guide me in creating the vbscript for installing Agent.exe without any Domain admin or local Admin rights.
I have created the script somehow, it works as
1. Looks for ComputerList.csv file
2. extracs hostname from the file
3. Ping the hostname
4. Copies the exe to the remote machine
5. Performs an Silent exe Installation.
But it requires Local domain admin rights
Option Explicit
On Error Resume Next
Dim objFSO, objFile, objRecordset, objConnection
Dim strSource,strProcess,strIn FileName,s trComputer ,strComput erList
Dim strFullPath,strPathtoTextF ile,strLog FileName
Dim intProcessID
Dim arrComputerList
Dim oConnection
Dim oRecordSet
Const ForWriting = 2
Const ForReading = 1
Const FOF_CREATEPROGRESSDLG = &H0&
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001
strLogFileName = "InstallRobot - " & Replace(date,"/","-") & ".log"
strFullPath = WScript.ScriptFullName
strPathtoTextFile = left(strFullPath,instrrev( strFullPat h,"\" ) -1)
'strPathtoTextFile = "C:\Nimbus\"
strInFileName = strPathtoTextFile & "\" & "ComputerList.csv"
wscript.echo strInFileName
' Create and open the log file
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set objFile = objFSO.OpenTextFile(strLog FileName, ForWriting, True)
' If there is a problem opening the Log file warn and quit
If Err.Number <> 0 Then
wscript.echo strLogFileName & " could Not be opened due To Error " & _
Err.description & "."
wscript.Quit
End If
' open the source file and read into a recordset
'Set objConnection = CreateObject("ADODB.Connec tion")
'Set objRecordSet = CreateObject("ADODB.Record set")
'objConnection.Open "Provider=Microsoft.Jet.OL EDB.4.0;" & "Data Source=" & strPathtoTextFile & ";" & "Extended Properties=""text;HDR=yes; FMT=Delimi ted"""
'objRecordset.Open "SELECT * FROM " & strInFileName, objConnection
'On Error Resume Next
'Do Until objRecordset.EOF
Set oConnection = createobject("adodb.connec tion")
Set oRecordSet = createobject("adodb.record set")
oConnection.open "Provider=Microsoft.Jet.OL EDB.4.0;Da ta Source= " & strPathtoTextFile & ";Extended Properties=""text;HDR=yes; FMT=Delimi ted"""
oRecordSet.open "SELECT * FROM " & strInFileName ,oConnection,adOpenStatic, adLockOptimistic, adCmdText
If Err.Number <> 0 Then
wscript.echo strLogFileName & " Error: " & _
Err.description & "."
wscript.quit
End If
Do Until oRecordSet.EOF
' get the field values from the file
strComputer = oRecordSet.Fields.Item(0). value
wscript.echo strComputer
strSource = oRecordSet.Fields.Item(1). value
wscript.echo strSource
strProcess = oRecordSet.Fields.Item(2). value
'wscript.echo strProcess
' Copy the installation files from the source computer
CopyFolder strSource,strComputer
' Start the Install, check that the install has been successful
InstallRobot StrSource, strProcess, strComputer, intProcessID
' Delete the folder one the install has been done
DeleteFolder strSource,strComputer
' Write a string of dashes to seperate
objFile.write vbCrlf & string(20,"-") & vbCrlf
oRecordSet.MoveNext
Loop
'Clean up
objFile.Close
Set objConnection = Nothing
Set oRecordSet = Nothing
Set objFSO = Nothing
Set objFile = Nothing
wscript.quit
'------------------------- ---------- ---------- ---------- ---------- ---------- --
Sub CopyFolder(strSource,strCo mputer)
'use shell object to copy a folder to a remote computer
Dim strDest
Dim objShell, objFolder
If instr(strComputer,"\\") then
strDest = strComputer & "\c$"
Else
strDest = "\\" & strComputer & "\c$"
End If
wscript.echo "Destination ", strDest, ", Source ", strSource
Set objShell = CreateObject("Shell.Applic ation")
Set objFolder = objShell.NameSpace(strDest )
objFolder.CopyHere strSource, FOF_CREATEPROGRESSDLG
WScript.Echo "check that copy is successful and count the number of files in the directory"
wscript.echo "Error:" & Err.number
If Err.number = 0 Then
wscript.echo "Folder " & strSource & " successfuly copied to " & strComputer & vbCrlf
objFile.Write "Folder " & strSource & " successfuly copied to " & strComputer & vbCrlf
Else
wscript.echo "Folder " & strSource & " Not copied to " & strComputer & vbCrlf & " due To Error " & Error & "." & vbCrlf
objFile.Write "Folder " & strSource & " Not copied to " & strComputer & vbCrlf & _
" due To Error " & Error & "." & vbCrlf
End If
'Clean up
Set objShell = Nothing
Set objFolder = Nothing
end sub
'------------------------- ---------- ---------- ---------- ---------- ---------- --
Sub DeleteFolder(strFolderName ,strComput er)
WScript.Echo "use WMI to delete a folder on a remote computer"
Dim objWMIService, colFolders,objFolders
Dim errResults, strDrive
strFolderName = "C:\\" & mid(strFolderName,instrRev (strFolder Name,"\") + 1)
' wscript.echo "Deleting Folder ", strFolderName
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper sonate}!\\ " & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
("Select * from Win32_Directory where Name = '" & strFolderName & "'")
For Each objFolder in colFolders
errResults = objFolder.Delete
Next
If err.number = 0 Then
objFile.Write "Folder " & strFolder & " successfuly deleted from " & strComputer & vbCrlf
Else
objFile.Write "Folder " & strFolder & " Not deleted from " & strComputer & vbCrlf & _
" due To " & Err.description & "." & vbCrlf
End If
'Clean up
Set objWMIService = Nothing
Set colFolders = Nothing
end Sub
'------------------------- ---------- ---------- ---------- ---------- ---------- --
Sub InstallRobot(StrSource, strProcess, strComputer, intProcessID)
' execute a file on a remote machine
Dim objWMIService
wscript.echo "INSTALLROBOT"
WScript.Echo strSource
strProcess = "C:\" & mid(strSource,instrRev(str Source,"\" ) + 1) & "\" & strProcess
WScript.Echo strProcess
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process ")
WScript.Echo"Start the process on the remote machine"
Error = objWMIService.Create(strPr ocess, null, null, intProcessID)
WScript.Echo "ERROR " & Error
If Err.number = 0 Then
objFile.Write "Installation of " & chr(34) & strProcess & chr(34) & vbCrlf & " was started with a process ID of " _
& intProcessID & "." & vbCrlf
Else
objFile.Write "Installation of " & chr(34) & strProcess & chr(34) & vbCrlf & _
"could Not be started on " & strComputer & " due To Error " & _
Err.description & "." & vbCrlf
Exit Sub
End If
WScript.Echo "loop while checking if Nimbus Robot is still running"
wscript.Sleep 1000
Do While NimbusInstallRunning(strCo mputer,int ProcessID)
intLoopCount = intLoopCount + 1
If intLoopCount > 5 Then
' Installation has taken too long, abort
objFile.Write "Nimbus robot installation on " & strComputer & " may have hung - Aborting " & vbCrlf
Exit Sub
End If
If intLoopCount > 3 Then
' installation has taken more than three minutes,
' it may be a slow machine so increase the wait time
wscript.Sleep 50000
Else
wscript.Sleep 10000
End If
Loop
'Clean up
Set objWMIService = Nothing
End Sub
'------------------------- ---------- ---------- ---------- ---------- ---------- --
Function NimbusInstallRunning(strCo mputer,int ProcessID)
' change this to using the PID at some stage to make it independant of
' Process Name
NimbusInstallRunning = False
Dim objWMIService, colProcesses
Dim strProcessID
strProcessID = cstr(intProcessID)
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper sonate}!\\ " & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Processid = '" & strProcessID & "'")
If colProcesses.Count = 0 Then
NimbusInstallRunning = False
Else
NimbusInstallRunning = True
End If
Set objWMIService = Nothing
Set colProcesses = Nothing
End Function
but seems it requires Domain Admin Rights.
I am new to Vbscript, can you please guide me in creating the vbscript for installing Agent.exe without any Domain admin or local Admin rights.
I have created the script somehow, it works as
1. Looks for ComputerList.csv file
2. extracs hostname from the file
3. Ping the hostname
4. Copies the exe to the remote machine
5. Performs an Silent exe Installation.
But it requires Local domain admin rights
Option Explicit
On Error Resume Next
Dim objFSO, objFile, objRecordset, objConnection
Dim strSource,strProcess,strIn
Dim strFullPath,strPathtoTextF
Dim intProcessID
Dim arrComputerList
Dim oConnection
Dim oRecordSet
Const ForWriting = 2
Const ForReading = 1
Const FOF_CREATEPROGRESSDLG = &H0&
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001
strLogFileName = "InstallRobot - " & Replace(date,"/","-") & ".log"
strFullPath = WScript.ScriptFullName
strPathtoTextFile = left(strFullPath,instrrev(
'strPathtoTextFile = "C:\Nimbus\"
strInFileName = strPathtoTextFile & "\" & "ComputerList.csv"
wscript.echo strInFileName
' Create and open the log file
Set objFSO = CreateObject("Scripting.Fi
Set objFile = objFSO.OpenTextFile(strLog
' If there is a problem opening the Log file warn and quit
If Err.Number <> 0 Then
wscript.echo strLogFileName & " could Not be opened due To Error " & _
Err.description & "."
wscript.Quit
End If
' open the source file and read into a recordset
'Set objConnection = CreateObject("ADODB.Connec
'Set objRecordSet = CreateObject("ADODB.Record
'objConnection.Open "Provider=Microsoft.Jet.OL
'objRecordset.Open "SELECT * FROM " & strInFileName, objConnection
'On Error Resume Next
'Do Until objRecordset.EOF
Set oConnection = createobject("adodb.connec
Set oRecordSet = createobject("adodb.record
oConnection.open "Provider=Microsoft.Jet.OL
oRecordSet.open "SELECT * FROM " & strInFileName ,oConnection,adOpenStatic,
If Err.Number <> 0 Then
wscript.echo strLogFileName & " Error: " & _
Err.description & "."
wscript.quit
End If
Do Until oRecordSet.EOF
' get the field values from the file
strComputer = oRecordSet.Fields.Item(0).
wscript.echo strComputer
strSource = oRecordSet.Fields.Item(1).
wscript.echo strSource
strProcess = oRecordSet.Fields.Item(2).
'wscript.echo strProcess
' Copy the installation files from the source computer
CopyFolder strSource,strComputer
' Start the Install, check that the install has been successful
InstallRobot StrSource, strProcess, strComputer, intProcessID
' Delete the folder one the install has been done
DeleteFolder strSource,strComputer
' Write a string of dashes to seperate
objFile.write vbCrlf & string(20,"-") & vbCrlf
oRecordSet.MoveNext
Loop
'Clean up
objFile.Close
Set objConnection = Nothing
Set oRecordSet = Nothing
Set objFSO = Nothing
Set objFile = Nothing
wscript.quit
'-------------------------
Sub CopyFolder(strSource,strCo
'use shell object to copy a folder to a remote computer
Dim strDest
Dim objShell, objFolder
If instr(strComputer,"\\") then
strDest = strComputer & "\c$"
Else
strDest = "\\" & strComputer & "\c$"
End If
wscript.echo "Destination ", strDest, ", Source ", strSource
Set objShell = CreateObject("Shell.Applic
Set objFolder = objShell.NameSpace(strDest
objFolder.CopyHere strSource, FOF_CREATEPROGRESSDLG
WScript.Echo "check that copy is successful and count the number of files in the directory"
wscript.echo "Error:" & Err.number
If Err.number = 0 Then
wscript.echo "Folder " & strSource & " successfuly copied to " & strComputer & vbCrlf
objFile.Write "Folder " & strSource & " successfuly copied to " & strComputer & vbCrlf
Else
wscript.echo "Folder " & strSource & " Not copied to " & strComputer & vbCrlf & " due To Error " & Error & "." & vbCrlf
objFile.Write "Folder " & strSource & " Not copied to " & strComputer & vbCrlf & _
" due To Error " & Error & "." & vbCrlf
End If
'Clean up
Set objShell = Nothing
Set objFolder = Nothing
end sub
'-------------------------
Sub DeleteFolder(strFolderName
WScript.Echo "use WMI to delete a folder on a remote computer"
Dim objWMIService, colFolders,objFolders
Dim errResults, strDrive
strFolderName = "C:\\" & mid(strFolderName,instrRev
' wscript.echo "Deleting Folder ", strFolderName
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper
Set colFolders = objWMIService.ExecQuery _
("Select * from Win32_Directory where Name = '" & strFolderName & "'")
For Each objFolder in colFolders
errResults = objFolder.Delete
Next
If err.number = 0 Then
objFile.Write "Folder " & strFolder & " successfuly deleted from " & strComputer & vbCrlf
Else
objFile.Write "Folder " & strFolder & " Not deleted from " & strComputer & vbCrlf & _
" due To " & Err.description & "." & vbCrlf
End If
'Clean up
Set objWMIService = Nothing
Set colFolders = Nothing
end Sub
'-------------------------
Sub InstallRobot(StrSource, strProcess, strComputer, intProcessID)
' execute a file on a remote machine
Dim objWMIService
wscript.echo "INSTALLROBOT"
WScript.Echo strSource
strProcess = "C:\" & mid(strSource,instrRev(str
WScript.Echo strProcess
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process
WScript.Echo"Start the process on the remote machine"
Error = objWMIService.Create(strPr
WScript.Echo "ERROR " & Error
If Err.number = 0 Then
objFile.Write "Installation of " & chr(34) & strProcess & chr(34) & vbCrlf & " was started with a process ID of " _
& intProcessID & "." & vbCrlf
Else
objFile.Write "Installation of " & chr(34) & strProcess & chr(34) & vbCrlf & _
"could Not be started on " & strComputer & " due To Error " & _
Err.description & "." & vbCrlf
Exit Sub
End If
WScript.Echo "loop while checking if Nimbus Robot is still running"
wscript.Sleep 1000
Do While NimbusInstallRunning(strCo
intLoopCount = intLoopCount + 1
If intLoopCount > 5 Then
' Installation has taken too long, abort
objFile.Write "Nimbus robot installation on " & strComputer & " may have hung - Aborting " & vbCrlf
Exit Sub
End If
If intLoopCount > 3 Then
' installation has taken more than three minutes,
' it may be a slow machine so increase the wait time
wscript.Sleep 50000
Else
wscript.Sleep 10000
End If
Loop
'Clean up
Set objWMIService = Nothing
End Sub
'-------------------------
Function NimbusInstallRunning(strCo
' change this to using the PID at some stage to make it independant of
' Process Name
NimbusInstallRunning = False
Dim objWMIService, colProcesses
Dim strProcessID
strProcessID = cstr(intProcessID)
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=imper
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Processid = '" & strProcessID & "'")
If colProcesses.Count = 0 Then
NimbusInstallRunning = False
Else
NimbusInstallRunning = True
End If
Set objWMIService = Nothing
Set colProcesses = Nothing
End Function
but seems it requires Domain Admin Rights.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Anuj, you did not quite get the point. You won't need to pass credentials if you used the startup script.
You can have it run from a logon script as kevinhsieh and McKnife indicated, it will run with the same rights as the user has, which they are likely admin users. You can modify the vbscript to use an alternate credential: http://blogs.technet.com/b/heyscriptingguy/archive/2004/12/13/how-can-i-run-a-script-under-alternate-credentials.aspx
Or you can push out your program using a GPO, first you have to get your exe into a MSI file:
http://support.microsoft.com/kb/816102 http://www.symantec.com/connect/downloads/exe-msi
"Install" can be a relative term however. If you need the exe on a machine where the user can get to it, copy it to the "all users" path on the computers. If it has to install a service or scheduled task, then you will need to be admin or push it out via the other methods above that get you higher rights.
-rich
Or you can push out your program using a GPO, first you have to get your exe into a MSI file:
http://support.microsoft.com/kb/816102 http://www.symantec.com/connect/downloads/exe-msi
"Install" can be a relative term however. If you need the exe on a machine where the user can get to it, copy it to the "all users" path on the computers. If it has to install a service or scheduled task, then you will need to be admin or push it out via the other methods above that get you higher rights.
-rich
No Rich, a logon script with different credentials will bring up a UAC prompt and needs interactive consent - and as we all know, not all users always click "ok". That's why we voted for the startup script.
Please respond or finalize it, this question is growing old :)
ASKER
I got your point here, so I tried to make the changes in passing User Name and password in the script through which I which I can install the exe on remote machine. But no success.
I appreciate if you can help me with the same
Thanks
Anuj