1. 程式人生 > >讓VBS指令碼也有GUI圖形介面

讓VBS指令碼也有GUI圖形介面

set ie=wscript.createobject("internetexplorer.application","event_") '建立ie物件'
ie.menubar=0 '取消選單欄'
ie.addressbar=0 '取消位址列'
ie.toolbar=0 '取消工具欄'
ie.statusbar=0 '取消狀態列'
ie.width=400 '寬400'
ie.height=400 '高400'
ie.resizable=0 '不允許使用者改變視窗大小'
ie.navigate "about:blank" '開啟空白頁面'
ie.left=fix((ie.document.parentwindow.screen.availwidth-ie.width)/2) '水平居中'
ie.top=fix((ie.document.parentwindow.screen.availheight-ie.height)/2) '垂直居中'
ie.visible=1 '視窗可見'

with ie.document '以下呼叫document.write方法,'
.write "<html><body bgcolor=#dddddd scroll=no>" '寫一段html到ie視窗中。'
.write "<h2 align=center>遠端清除系統日誌</h2><br>"
.write "<p>目標IP:<input id=ip type=text size=15>" '也可以用navigate方法直接開啟一'
.write "<p>使用者名稱:<input id=user type=text size=30>" '個html檔案,效果是一樣的。'
.write "<p>密碼: <input id=pass type=password size=30>"
.write "<p align=center>型別:" '不僅是input物件,所有DHTML支援'
.write "<input id=app type=checkbox>應用程式 " '的物件及其屬性、方法都可以使用。'
.write "<input id=sys type=checkbox>系統 "
.write "<input id=sec type=checkbox>安全" '訪問這些物件的辦法和網頁中訪問'
.write "<p align=center><br>" '框架內物件是類似的。'
.write "<input id=confirm type=button value=確定> "
.write "<input id=cancel type=button value=取消>"
.write "</body></html>"
end with

dim wmi '顯式定義一個全域性變數'
set wnd=ie.document.parentwindow '設定wnd為視窗物件'
set id=ie.document.all '設定id為document中全部物件的集合'
id.confirm.onclick=getref("confirm") '設定點選"確定"按鈕時的處理函式'
id.cancel.onclick=getref("cancel") '設定點選"取消"按鈕時的處理函式'

do while true '由於ie物件支援事件,所以相應的,'
wscript.sleep 200 '指令碼以無限迴圈來等待各種事件。'
loop

sub event_onquit 'ie退出事件處理過程'
wscript.quit '當ie退出時,指令碼也退出'
end sub

sub cancel '"取消"事件處理過程'
ie.quit '呼叫ie的quit方法,關閉IE視窗'
end sub '隨後會觸發event_onquit,於是指令碼也退出了'

sub confirm '"確定"事件處理過程,這是關鍵'
with id
if .ip.value="" then .ip.value="." '空ip值則預設是對本地操作'
if not (.app.checked or .sys.checked or .sec.checked) then 'app等都是checkbox,通過檢測其checked'
wnd.alert("至少選擇一種日誌") '屬性,來判斷是否被選中。'
exit sub
end if
set lct=createobject("wbemscripting.swbemlocator") '建立伺服器定位物件'
on error resume next '使指令碼宿主忽略非致命錯誤'
set wmi=lct.connectserver(.ip.value,"root/cimv2",.user.value,.pass.value) '連線到root/cimv2名字空間'
if err.number then '自己捕捉錯誤並處理'
wnd.alert("連線WMI伺服器失敗") '這裡只是簡單的顯示“失敗”'
err.clear
on error goto 0 '仍然讓指令碼宿主處理全部錯誤'
exit sub
end if
if .app.checked then clearlog "application" '清除每種選中的日誌'
if .sys.checked then clearlog "system"
if .sec.checked then clearlog "security" '注意,在XP下有限制,不能清除安全日誌'
wnd.alert("日誌已清除")
end with
end sub

sub clearlog(name)
wql="select * from Win32_NTEventLogFile where logfilename='"&name&"'"
set logs=wmi.execquery(wql) '注意,logs的成員不是每條日誌,'
for each l in logs '而是指定日誌的檔案物件。'
if l.cleareventlog() then
wnd.alert("清除日誌"&name&"時出錯!")
ie.quit
wscript.quit
end if
next