使用 ChassisType 的 Select 个案例的多个条件
Multiple conditions for Select Case using ChassisType
我们在我们的环境中使用 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"
我想让这三个条件起作用,并根据站点条件和底盘类型设置站点代码,但我不确定如何使用多个条件或如何组合底盘类型,我想指导。
以下是完整脚本
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
如果你想在 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
*走下讲台*
话虽如此,在您的情况下,您甚至永远不会进行三个额外的检查,因为前三个条件之一会首先匹配。您需要将组合条件放在简单条件之前才能使您的陈述有效。此外,您不能像这样根据多个值检查变量:
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
我们在我们的环境中使用 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"
我想让这三个条件起作用,并根据站点条件和底盘类型设置站点代码,但我不确定如何使用多个条件或如何组合底盘类型,我想指导。
以下是完整脚本
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
如果你想在 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
*走下讲台*
话虽如此,在您的情况下,您甚至永远不会进行三个额外的检查,因为前三个条件之一会首先匹配。您需要将组合条件放在简单条件之前才能使您的陈述有效。此外,您不能像这样根据多个值检查变量:
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