2015-10-06 65 views
1

我有這段代碼可以遠程殺死5臺服務器上的某個進程。我的問題是,我想縮短它,並在1次點擊中終止所有服務器中的進程。代碼現在詢問我是否確定要殺死進程,然後殺死進程,然後詢問是否要繼續下一臺服務器。我想只是有一個的messge說,「所有的服務器上殺死進程」,並做了遠程殺死一個進程中的進程

`Option Explicit 
Dim objWMIService, objProcess, colProcess 
Dim strProcessKill, item 
Dim strComputer(4) 
Dim ans, killFLag 

strComputer(0) = "server1" 
strComputer(1) = "server2" 
strComputer(2) = "server3" 
strComputer(3) = "server4" 
strComputer(4) = "server5" 

    strProcessKill = "'notepad.exe'" 



For item = 0 To 4 Step 1 

if MsgBox("Are you sure you want to kill " & strProcessKill & " on " & strCOmputer(item) & "?" , vbYesNo + vbQuestion) = vbYes then 
    killFLag = 1 
end if 


if KillFlag = 1 then 
    msgbox "KILLING PROCESS ON" & strComputer(item) 
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer(item) & "\root\cimv2") 

    Set colProcess = objWMIService.ExecQuery _ 
    ("Select * from Win32_Process Where Name = " & strProcessKill) 
     For Each objProcess in colProcess 
      objProcess.Terminate() 
     Next 
    WSCript.Echo "Just killed process " & strProcessKill & " on " & strComputer(item) 

else 

WScript.Quit 

end if 

Next`

回答

0

移動的for循環下來,讓您不會提示爲每個服務器。像這樣的東西應該工作

Option Explicit 
Dim objWMIService, objProcess, colProcess 
Dim strProcessKill, item 
Dim strComputer(4) 
Dim ans, killFLag 

strComputer(0) = "server1" 
strComputer(1) = "server2" 
strComputer(2) = "server3" 
strComputer(3) = "server4" 
strComputer(4) = "server5" 


    strProcessKill = "'notepad.exe'" 

if MsgBox("Are you sure you want to kill " & strProcessKill & " on all servers?" , vbYesNo + vbQuestion) = vbYes then 
    killFLag = 1 
end if 


if KillFlag = 1 then 
    msgbox "KILLING PROCESS ON ALL SERVERS" 
    For item = 0 To 4 Step 1 
     Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer(item) & "\root\cimv2") 

     Set colProcess = objWMIService.ExecQuery _ 
     ("Select * from Win32_Process Where Name = " & strProcessKill) 
      For Each objProcess in colProcess 
       objProcess.Terminate() 
      Next 

    next 
    msgBox "Done" 
else 

WScript.Quit 

end if 
+0

正是我需要的,我在想太辛苦大聲笑。非常感謝你的幫助!!!!!!!!!!!!!!!!!!! – Neil

+0

**更新**代碼由於某種原因不再有效,沒有任何改變。當腳本運行時,它會啓動msg框「在所有服務器上查殺進程」,我點擊確定,腳本似乎退出而沒有發生任何事情。有任何想法嗎?? – Neil

+0

您可以嘗試記錄到文本文件以查看正在發生的事情,甚至可以在for循環中添加一些wscript.echo以查看腳本結束的位置。 – jjeff