尽管每天都在操作window的系统,但是瞄了下MSDN中的相关资料,尼玛,头都大了,英语一项不好。其次对windows上面的一些类库完全没有眼熟的,毕竟更多的时候是在跟JAVA打交道。看了那么多类库,但是用不晓得如何去调用,真是灰常无奈,一个个的去学,目前又有点不切实际。慢慢积累吧...
这次CP让我写个VBS脚本来检查终端设备是否打了指定的补丁,是否开启了远程桌面服务,无奈中只好到处搜罗资料最终出了下面这么个脚本。
关键资料出处:
http://community.spiceworks.com/scripts/show/349-check-if-a-windows-update-is-installed-on-a-pc-vbs
http://msdn.microsoft.com/en-us/library/aa387287%28v=vs.85%29.aspx
http://www.w3school.com.cn/vbscript/index.asp
Set ws = CreateObject("WScript.Shell")
host = WScript.FullName
If LCase( right(host, len(host)-InStrRev(host,"\")) ) = "wscript.exe" Then
ws.run "cscript """ & WScript.ScriptFullName & chr(34), 0
WScript.Quit
End If
Set oexec = ws.Exec("netstat -ano")
Dim allInput
allInput = ""
Do
allInput = allInput & oExec.StdOut.ReadLine & vbCRLF
Loop While Not oExec.StdOut.AtEndOfStream
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("checkResult", 2, True)
If InStr(allInput,"3389")>0 Then
'开通了远程桌面则要检查补丁是否安装
ckinfo = checkOS()
If InStr(ckinfo,"indows 7")>0 Then
'win7是否安装了补丁
status1 = CheckParticularHotfix("2621440") 'KB2621440
status2 = CheckParticularHotfix("2667402") 'KB2621440
status = status1 And status2
If status = true Then
'MsgBox "1", 64, "检查结果"
objTextFile.WriteLine(1)
ElseIf status = false Then
'MsgBox "0", 64, "检查结果"
objTextFile.WriteLine(0)
End If
Else
status = CheckParticularHotfix("2621440") 'KB2621440
If status = true then
'MsgBox "1", 64, "检查结果"
objTextFile.WriteLine(1)
ElseIf status = false Then
'MsgBox "0", 64, "检查结果"
objTextFile.WriteLine(0)
End If
End If
Else
'MsgBox "1", 64, "检查结果"
objTextFile.WriteLine(1)
End If
status = CheckParticularHotfix(HotFixID)
If status = true then
wscript.Echo "The Microsoft KB" & HotFixID & " IS installed."
ElseIf status = false Then
wscript.Echo "The Microsoft KB" & HotFixID & " is NOT installed."
Else
'Error
wscript.Echo "Error, unable to check for Microsoft KB. Error is: " & status
End If
private Function CheckParticularHotfix(strHotfixID)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Version 1.0
' Checks if a particular hotfix is installed or not.
' This function has these 3 return options:
' TRUE, FALSE, <error description>
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
On error resume next
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")
if err.number <> 0 then
CheckParticularHotfix = "WMI could not connect to computer 'localCompute'"
exit function 'No reason to continue
end if
strWMIforesp = "Select * from Win32_QuickFixEngineering where HotFixID = 'Q" & strHotfixID &_
"' OR HotFixID = 'KB" & strHotfixID & "'"
Set colQuickFixes = objWMIService.ExecQuery (strWMIforesp)
if err.number <> 0 Then 'if an error occurs
CheckParticularHotfix = "Unable to get WMI hotfix info"
else 'Error number 0 meaning no error occured
tal = colQuickFixes.count
if tal > 0 then
CheckParticularHotfix = True 'HF installed
else
CheckParticularHotfix = False 'HF not installed
end If
end if
Set colQuickFixes = Nothing
Err.Clear
On Error GoTo 0
End Function
'检查操作系统版本
private Function checkOS()
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
osInfo = objOperatingSystem.Caption
Next
checkOS = osInfo
End Function