使用ChassisType的Select Case的多个条件

时间:2016-11-22 19:33:39

标签: vbscript case computer-name mdt

我们在环境中使用MDT来对计算机进行映像。我们使用脚本来根据IP位置命名计算机,然后脚本将数据写入数据库。我需要修改脚本以包含一部分附加条件来构造计算机名称。我在下面的脚本中使用了机箱类型附加条件的部分有困难。

我需要帮助的部分是

Case (InStr(1, strIP, ".111.")>0),(strChassisType = 8,9 Or 10)
  strSiteCode="UKL"
Case (InStr(1, strIP, ".112.")>0),(strChassisType = 8,9 Or 10)
  strSiteCode="NYL"
Case (InStr(1, strIP, ".113.")>0),(strChassisType = 8,9 Or 10)
  strSiteCode="HKL"

我想让这三个条件工作,并根据Site的条件和机箱类型设置站点代码,但我不知道如何使用多个条件或如何组合机箱类型并希望指导。

以下是完整的脚本

Function UserExit(sType, sWhen, sDetail, bSkip)
  oLogging.CreateEntry "entered UserExit ", LogTypeInfo
  UserExit = Success
End Function

Function computerName()
  'On Error Resume Next
  Dim strSerial,strAsset, strManufacturer, strIP, strSiteCode
  Set WshNetwork = WScript.CreateObject("WScript.Network")
  Set WSHShell = CreateObject("WScript.Shell")

  '----Establish SQL Connection----
  Set objConnection = CreateObject("ADODB.Connection")
  objConnection.Open "Provider=sqloledb;Data Source=XXXXXXXX;Initial Catalog=NetMetrics;User Id=netmetrics;Password=*********;"

  '----Determine local Service & Asset Tags----
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

  Set colSMBIOS = objWMIService.ExecQuery _
    ("Select * from Win32_SystemEnclosure")

  For Each objSMBIOS In colSMBIOS
    strManufacturer = objSMBIOS.Manufacturer
    strSerial = objSMBIOS.SerialNumber
Next

  '----Determine Site Code based upon IP address----
  Set IPConfigSet = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")

  For Each IPConfig In IPConfigSet
    If Not IsNull(IPConfig.IPAddress) Then 
      For i = LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)
        strIP = strIP + IPConfig.IPAddress(i)
      Next
    End If
  Next

  Set colChassis = objWMIService.ExecQuery _
    ("Select * from Win32_SystemEnclosure")

  For Each objChassis in colChassis
    For Each strChassisType in objChassis.ChassisTypes
      Select Case strChassisType
        Select Case True
          Case (InStr(1, strIP, ".111.")>0)
            strSiteCode = "UK"
          Case (InStr(1, strIP, ".112.")>0)
            strSiteCode = "NY"
          Case (InStr(1, strIP, ".113.")>0)
            strSiteCode = "HK"
          Case (InStr(1, strIP, ".111.")>0),(strChassisType = 8,9 Or 10)
            strSiteCode = "UKL"
          Case (InStr(1, strIP, ".112.")>0),(strChassisType = 8,9 Or 10)
            strSiteCode = "NYL"
          Case (InStr(1, strIP, ".113.")>0),(strChassisType = 8,9 Or 10)
            strSiteCode = "HKL"
        End Select

  If (inStr(1,strManufacturer,"Dell")) Then
    strSQLQuery = "select count(*) from AssetTags where ServiceTag='" & strSerial & "'"
    priorEntry = objConnection.Execute(strSQLQuery)
    If priorEntry(0) = 0 Then
      strSQLQuery = "select right(concat('00',right(max(assettag),3)+1),3) from AssetTags where AssetTag like '" & strSiteCode & "[^S]%'"
      Set arrNewTag = objConnection.Execute(strSQLQuery)
      strSQLQuery = "INSERT INTO AssetTags Values ('" & strSerial & "','"& strSiteCode & arrNewTag(0) & "', 'New')"
      objConnection.Execute(strSQLQuery)
      computerName=(strSiteCode & arrNewTag(0))
    Else
      strSQLQuery = "select assettag from AssetTags where ServiceTag='" & strSerial & "'"
      Set arrNewTag = objConnection.Execute(strSQLQuery)
      computerName=(arrNewTag(0))
    End If
  Else
    computerName = "Set Computer Name"
  End If

  objConnection.Close
End Function

1 个答案:

答案 0 :(得分:2)

如果要在Case语句中使用多个条件,则需要将它们与逻辑运算符连接起来,除非您想要匹配其中任何一个(逗号表示您有一个包含多个独立条件的列表)。

声明

Select Case True
  Case x=1, y=2
    ...
End Select
如果x的值为1或y的值为2,则

匹配。

声明

Select Case True
  Case x=1 And y=2
    ...
End Select

仅当x的值为1 并且 y的值为2时才匹配。

但是,我个人认为这是滥用Select语句,因为它们通常意味着根据一个变量或表达式的不同值进行分支。

Select Case x
  Case 1
    'do some
  Case 2
    'do other
  Case Else
    'didn't expect this value
End Select

我更喜欢If..ElseIf..Else语句来检查多种不同的条件。

If x=1 And y=3 Then
  ...
ElseIf x=2 And y<5 Then
  ...
ElseIf y>2 And z-x=4 Then
  ...
Else
  ...
End If

*离开soapbox *

话虽如此,在你的情况下,你甚至从未进行过三次额外检查,因为前三个条件中的一个首先匹配。您需要在简单条件之前放置组合条件才能使语句正常工作。此外,您无法针对多个值检查变量,如下所示:

<击>

strChassisType = 8,9 Or 10

如果您的值是数字,那么您可以使用这样的比较操作:

If strChassisType >= 8 And strChassisType <= 10 Then

进行字典查找:

chassisTypes = CreateObject("Scripting.Dictionary")
chassisTypes.Add  "8", True
chassisTypes.Add  "9", True
chassisTypes.Add "10", True
...
If chassisTypes.Exists(strChassisType) Then

或定义Contains函数以检查数组中是否存在值:

Function Contains(a, v)
  Contains = False
  For Each e In a
    If e = v Then
      Contains = True
      Exit For
    End If
  Next
End Function
...
If Contains(Array("8", "9", "10"), strChassisType) Then

由于你的第二组条件基本上只是修改了第一组条件的结果,我可能就是这样做了:修改第一组条件的结果。并且可以使用Split(strIP, ".")从IP地址拆分相关子网。

subnet = Split(strIP, ".")(2)
For Each objChassis In colChassis
  For Each strChassisType In objChassis.ChassisTypes
    Select Case subnet
      Case "111": strSiteCode = "UK"
      Case "112": strSiteCode = "NY"
      Case "113": strSiteCode = "HK"
      Case Else
        WScript.Echo "Unknown subnet: " & subnet
        WScript.Quit 1
    End Select

    If strChassisType >= 8 And strChassisType <= 10 Then
      strSiteCode = strSiteCode & "L"
    End If
  Next
Next