2012-11-01 51 views
0

我需要檢索操作系統名稱,如「Windows 2003」,然後操作系統版本,這將是「服務器標準版」。需要的操作系統名稱,然後版本

我有此腳本

Set colItems = objWMISrvc.ExecQuery("SELECT * FROM Win32_OperatingSystem" 
For Each objOperatingSystem in colItems 
    strOSName = objOperatingSystem.Caption 
Next 

這使得strOSName爲「Windows 2003 Server標準版」或者我開發的系統,「微軟Windows 7專業版」上。

有沒有辦法讓我分開這兩個方面?如果這是有道理的...

回答

2

由於市場營銷在版本控制方面有發言權,我懷疑你可以爲所有Windows版本找到一個通用的策略。對於實施例中給出,我會與一個RegExp開始:

Option Explicit 

Function qq(s) : qq = """" & s & """" : End Function 

Dim aVers : aVers = Array(_ 
    "Windows 2003 Server Standard Edition" _ 
    , "Microsoft Windows 7 Professional" _ 
) 
Dim reVers : Set reVers = New RegExp 
reVers.Pattern = "^(\D+\d+) (.*)$" 
Dim sVers 
For Each sVers In aVers 
    Dim oMTS : Set oMTS = reVers.Execute(sVers) 
    WScript.Echo Join(Array(_ 
     qq(sVers), qq(oMTS(0).SubMatches(0)), qq(oMTS(0).SubMatches(1)) _ 
    ), vbCrLf) 
Next 

輸出:

"Windows 2003 Server Standard Edition" 
"Windows 2003" 
"Server Standard Edition" 
"Microsoft Windows 7 Professional" 
"Microsoft Windows 7" 
"Professional" 

爲了應對例如「Microsoft Windows XP [版本5.1.2600]」,你將不得不修改模式(但是定製RegExp模式比修改包含大量InStr()和Mid()調用的函數更好)。

ADDED/cf。評論)

模式 「^(\ d + \ d +)(*)$」 查找:

  1. ^字符串的開始
  2. (初開始捕獲/組
  3. \ d +序列(1或多個)非數字
  4. \ d + digitis
  5. 的序列)結束第一捕獲/組
  6. < - 看!一片空白!
  7. (開始第二捕獲/組
  8. 。*(可能爲空)字符除了vbLf
  9. 序列)結束第二捕捉/組串
+0

  • $年底。這真是棒極了。我知道一些正則表達式,但我沒有看到你的模式在做什麼。我的意思是,它的工作,這是偉大的,但我只是好奇。 – envinyater

  • +0

    我有一個批處理文件,這樣做。我會建議看看PowerShell的最佳方式。 – Jeff

    相關問題