iT邦幫忙

DAY 30
5

WSH研習記錄系列 第 32

WSH研習記錄--個人小嘗試分享(續)

昨天一時的興起..
把昨天所分享的做了一個小小的延伸..
我們都知道Windows XP之後才會有內建的防火牆
所以,我們只需要確認XP之後的版本行了
而我公司是2000跟XP共存的
所以我必需先了解Client端的電腦的OS是那一個版本的
才能對它做防火牆開啟與否的判定
所以又做了底下這個去執行...

Option Explicit

Private objFirewall
Private objPolicy
Private FSO
Private LogFilePath
Private ComName
Private OSVer

Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFirewall = CreateObject("HNetCfg.FwMgr")
Set objPolicy = objFirewall.LocalPolicy.CurrentProfile
ComName = CreateObject("WScript.NetWork").ComputerName
LogFilePath = FSO.BuildPath(FSO.GetFolder("L:\fwlog"), ComName & ".log")
OSVer = GetOS
IF OSVer = "WinXP" Then
	With FSO.OpenTextFile(LogFilePath, 8, True)
		.Write Now()
		.Write vbTab
		.Write ComName
		.Write vbTab
		.Write OSVer
		.Write vbTab
		If objPolicy.FirewallEnabled = True then
			.WriteLine "Firewall is On!"
		else
			.WriteLine "Firewall is Off!"
		End If
		.Close
	End With
Else
	With FSO.OpenTextFile(LogFilePath, 8, True)
		.Write Now()
		.Write vbTab
		.Write ComName
		.Write vbTab
		.Write OSVer
		.Close
	End With
End IF
Set FSO = Nothing
Set objFirewall = Nothing
Set ObjPolicy = Nothing

Function GetOS()
  Const OpenAsASCII      =  0
  Const FailIfNotExist   =  0
  Const ForReading       =  1

  Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
  Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
  Dim sTemp, sTempFile, fFile, sResults
  sTemp = WshShell.ExpandEnvironmentStrings("%TEMP%")
  sTempFile = sTemp & "\runresult.tmp"

  WshShell.Run "%comspec% /c ver >" & sTempFile, 0, True

  Set fFile = FSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsASCII)

  sResults = fFile.ReadAll
  fFile.Close
  FSO.DeleteFile(sTempFile)

  Select Case True
    Case InStr(sResults, "Windows 95") > 1 : GetOS = "Win95"
    Case InStr(sResults, "Windows 98") > 1 : GetOS = "Win98"
    Case InStr(sResults, "Windows Millennium") > 1 : GetOS = "WinME"
    Case InStr(sResults, "Windows NT") > 1 : GetOS = "WinNT4"
    Case InStr(sResults, "Windows 2000") > 1 : GetOS = "Win2K"
    Case InStr(sResults, "Windows XP") > 1 : GetOS = "WinXP"
    Case Else : GetOS = "Unknown"
  End Select
End Function 

這裡面運用了前面所談過的部份,像寫入純文字檔作log檔
還加入了對於作業系統別的判別..

提供給有需要的人..


上一篇
WSH研習記錄--個人小嘗試分享
下一篇
WSH研習記錄--Office應用程式操作篇(Word應用)
系列文
WSH研習記錄42

尚未有邦友留言

立即登入留言