2011-11-04 20 views
1

我真的被困在一個問題上,所以我想我會得到第二個意見。VbScript,無需用戶輸入遠程安裝EXE?

我試圖遠程安裝.exe和.msi到客戶端計算機。我有一個vb腳本來下載文件並運行該文件,但存在一些問題。首先,我無法使其在本地管理員帳戶上運行。出於測試目的,我以管理員身份運行它,並且工作正常,但如果放在客戶端計算機上,則需要訪問本地管理員。

其次,更重要的是,微軟在安裝exe文件之前需要一定量的用戶輸入。我知道無聲的msi安裝是可能的,但我認爲無聲的exe是不可能的?

作爲解決方案,我正在考慮PsExec,但我覺得我在這裏錯過了一些東西。

供參考,這是我的VB腳本:

Dim TApp 
Dim IEObj 
Dim tArea 
Dim tButton 

Const HIDDEN_WINDOW = 12 
Const SHOW_WINDOW=1 

'Array of Patch files to install. 
Dim InstallFiles() 

'maximum of 100 workstations to install patches to. 
Dim wsNames(100) 
Dim numComputers 
Dim retVal 
Dim PatchFolder 
'Create explorer window 
Set IEObj=CreateObject("InternetExplorer.Application") 
IEObj.Navigate "about:blank" 
IEObj.Height=400 
IEObj.Width=500 
IEObj.MenuBar=False 
IEObj.StatusBar=False 
IEObj.ToolBar=0 
set outputWin=IEObj.Document 
outputWin.Writeln "<title>RemotePatchInstall version 1.0</title>" 
outputWin.writeln "<HTA:APPLICATION ID='objPatchomatic' APPLICATIONNAME='Patchomatic' SCROLL='no' SINGLEINSTANCE='yes' WINDOWSTATE='normal'>" 

outputWin.writeln "<BODY bgcolor=ButtonFace ScrollBar='No'>" 
outputWin.writeln "<TABLE cellSpacing=1 cellPadding=1 width='75pt' border=1>" 
outputWin.writeln "<TBODY>" 

outputWin.writeln "<TR>" 
outputWin.writeln "<TD>" 
outputWin.writeln "<P align=center><TEXTAREA name=Information rows=6 cols=57 style='WIDTH: 412px; HEIGHT: 284px'></TEXTAREA></P></TD></TR>" 
outputWin.writeln "<TR>" 
' outputWin.writeln "<TD><P align=center><INPUT id=button1 style='WIDTH: 112px; HEIGHT: 24px' type=button size=38 value='Install Patches' name=button1></P></TD>" 
outputWin.writeln "</TR>" 
outputWin.writeln "<TR>" 
outputWin.writeln "<TD></TD></TR></TBODY></TABLE>" 
outputWin.writeln "</BODY>" 

IEObj.Visible=True 
'Get the Information textarea object from the window 
set tempObj=outputWin.getElementsByName("Information") 
objFound=false 
'loop through its object to find what we need 
For each objN in tempObj 
if objN.name="Information" then 
objFound=true 
set tArea=objN 
end if 
next 
'if we didnt find the object theres a problem 
if ObjFound=False then 
'so show an error and bail 
MsgBox "Unable to access the TextBox on IE Window",32,"Error" 
WScript.Quit 
end if 



'************************* 
'ADMINS: The below is all you should really have to change. 
'************************* 

'Change this to the location of the patches that will be installed. 
'they should be limited to the amout you try to install at one time. 
'ALSO the order they are installed is how explorer would list them by alphabetically. 
'So given file names: 
'patch1.exe 
'patch2.exe 
'patch11.exe 
'installation order would be patch1.exe,patch11.exe, patch2.exe 

PatchFolder="C:\IUware Online\Install\" 

'Change this to location where the patches will be copied to on remote cp. This directory must exist on remote computer. 
'I have it hidden on all workstations. 
RemotePatchFolder="C:\Users\jorblume\Backup\" 

'Workstation names to refer to as array 
wsNames(1)="129.79.205.153" 
'wsNames(2)="192.168.0.11" 

'number of remote computers 
numComputers=1 
'********************** 
'ADMINS: The above is all you should really have to change. 
'********************** 

'Copy files to remote computers. 

'Get a list of the executable file in the folder and put them into the InstallFiles array 
'on return, retVal will be number of files found. 
retVal=GetPatchFileList (PatchFolder,InstallFiles) 

'for each file copy to remote computers 
For cc=1 to numComputers 'for each computer 
For i = 1 to retVal 'for each file 
Dim copySuccess 
Dim SharedDriveFolder 
'do a replacement on the : to $, this means you must have admin priv 
'this is because i want to copy to "\\remotecpname\c$\PathName" 
SharedDriveFolder=replace(RemotePatchFolder,":","$") 
'copy it from the PatchFolder to the path on destination computer 
'USE: RemoteCopyFile (SourceFilePath,DestinationFilePath, RemoteComputerName) 
CurrentCP=cc 
copySuccess=RemoteCopyFile(PatchFolder & "\" & InstallFiles(i),SharedDriveFolder,wsNames(CurrentCP)) 
if copySuccess=true then 
tArea.Value=tArea.Value & PatchFolder & "\" & InstallFiles(i) & " copy - OK" & vbcrlf 
else 
tArea.Value=tArea.Value & PatchFolder & "\" & InstallFiles(i) & " copy - FAILED" & vbcrlf 
end if 
Next 
Next 


'Install the files on remote computer 

'go through each filename and start that process on remote PC. 
'for each file install them on the computers. 
For cc=1 to numComputers 

'if theres more than one patch 
if retVal>1 then 
For i=1 to retVal-1 
CurrentCp=cc 

'Now create a process on remote computer 
'USE: CreateProcessandwait(ComputerName, ExecutablePathonRemoteComputer 
'Create a process on the remote computer and waits. Now this can return a program terminated which is ok, 
'if it returns cancelled it means the process was stopped, this could happen if the update required a 
'computer restart. 

CreateProcessandWait wsNames(CurrentCP), RemotePatchFolder & InstallFiles(i) & " /quiet /norestart", tArea 

next 
end if 

'do the last patch with a forcereboot 
CreateProcessandWait wsNames(CurrentCP), RemotePatchFolder & InstallFiles(retVal) & " /quiet" & " /forcereboot" , tArea 

next 

tArea.value=tArea.Value & "Script Complete!" & vbcrlf 


'**************************** FUNCTIONS 

'Get list of files in Folder. 

Function GetPatchFileList(FileFolder, FileStringArray()) 

'create file system object 
Set objFS=CreateObject("Scripting.FileSystemObject") 
'set the a variable to point to our folder with the patches in it. 
Set objFolder=objFS.GetFolder(FileFolder) 
'set the initial file count to 0 
numPatches=0 

for each objFile in objFolder.Files 
if UCase(Right(objFile.Name,4))=".EXE" then 
numPatches=numPatches+1 

redim preserve FileStringArray(numPatches) 
FileStringArray(numPatches)=objFile.Name 
end if 

next 
GetPatchFileList=numPatches 
End Function 

'Copy files to remote computer. 

Function RemoteCopyFile(SrcFileName,DstFileName,DestinationComputer) 
Dim lRetVal 
'create file system object 
Set objFS=CreateObject("Scripting.FileSystemObject") 

lRetVal=objFS.CopyFile (SrcFileName, "\\" & DestinationComputer & "\" & DstFileName) 
if lRetVal=0 then 
RemoteCopyFile=True 
else 
RemoteCopyFile=False 
end if 
End Function 


'Create process on remote computer and wait for it to complete. 

Function CreateProcessAndWait(DestinationComputer,ExecutableFullPath,OutPutText) 
Dim lretVal 

strComputer= DestinationComputer 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2:Win32_Process") 
Set objWMIServiceStart= GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2:Win32_ProcessStartup") 

Set objConfig = objWMIServiceStart.SpawnInstance_ 

objConfig.ShowWindow = 1 'show window or use HIDDEN_WINDOW 



lretVal= objWMIService.Create(ExecutableFullPath, null, objConfig, intProcessID) 
if lretVal=0 then 
OutPutText.Value = OutPutText.Value & "Process created with ID of " & intProcessID & " on " & DestinationComputer & vbcrlf 
OutPutText.Value = OutPutText.Value & " Waiting for process " & intProcessID & " to complete." & vbcrlf 

WaitForPID strComputer, intProcessID,OutPutText 
OutPutText.Value = OutPutText.Value & "Process complete." & vbcrlf 
else 
OutPutText.Value = OutPutText.Value & "Unable to start process " & ExecutableFullPath & " on " & DestinationComputer & vbcrlf 
end if 
End Function 


'Wait for PRocess to complete 

Function WaitForPID(ComputerName,PIDNUMBER,OutPutText) 
Dim ProcessNumber 

Set objWMIServiceQ = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & ComputerName & "\root\cimv2") 
Set colItems = objWMIServiceQ.ExecQuery("Select * from Win32_Process",,48) 
For Each objItem in colItems 
'check if this process is the one we are waiting for 
if objItem.ProcessID=PIDNUMBER then 
OutPutText.Value = OutPutText.Value & "Process Info:" & vbcrlf 
OutPutText.Value = OutPutText.Value & " Description: " & objItem.Description & vbcrlf 
OutPutText.Value = OutPutText.Value & " ExecutablePath: " & objItem.ExecutablePath & vbcrlf 
OutPutText.Value = OutPutText.Value & " Name: " & objItem.Name & vbcrlf 
OutPutText.Value = OutPutText.Value & " Status: " & objItem.Status & vbcrlf 
OutPutText.Value = OutPutText.Value & " ThreadCount: " & objItem.ThreadCount & vbcrlf 
ProcessNumber=objItem.ProcessID 
end if 

Next 

PidWaitSQL="SELECT TargetInstance.ProcessID " & " FROM __InstanceDeletionEvent WITHIN 4 " _ 
& "WHERE TargetInstance ISA 'Win32_Process' AND " _ 
& "TargetInstance.ProcessID= '" & ProcessNumber & "'" 

Set Events = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & ComputerName & "\root\cimv2").ExecNotificationQuery (PidWaitSQL) 



Set TerminationEvent = Events.nextevent 
OutPutText.Value = OutPutText.Value & "Program " & TerminationEvent.TargetInstance.ProcessID & _ 
" terminated. " & vbcrlf 
set TerminationEvent=Nothing 
exit function 

End Function 
+0

我認爲你需要爲此使用psexec。 – HK1

+2

許多基於exe的安裝程序都可以靜默運行,但您需要知道正確的命令行參數。例如,大多數MS軟件更新可以通過指定/ passive/norestart進行靜默安裝。 –

+0

根據您的設計目標,使用psexec的替代方案將包括將代碼作爲啓動腳本或系統服務運行。 –

回答

2

正如評論所說,PSEXEC將是此方案中的最佳解決方案。請不要忘記在語法中使用/ acceptedteula,以確保它在等待某人接受其EULA時不會有效「掛起」。 :)如果您在安裝中對psexec有任何疑問或問題,請回到此處評論。