如果没有主机名出现错误
if no host name present error
如果用户输入了错误的主机名。我希望脚本显示一个消息框 "PC wmi broken or is offline" 它会显示一个错误,但由于这是发送给其他人的,我只想要一个基本的错误消息框
Sub WindowsLoad1
strHTML = strHTML & "<br>" & "<font face='Arial' size='2' color='white''>"
strComputer = MachineName.Value
strHTML = strHTML & "<b>Sys Info</b><br> <br>"
strHTML = strHTML & "</font><font face='Arial' size='2'>"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
strHTML = strHTML & "<b>Hostname</b>: " & objComputer.Name & "<br>"
strHTML = strHTML & "<b>Current User</b>: " & objComputer.UserName & "<br>"
strHTML = strHTML & "<b>Manufacturer</b>: " & objComputer.Manufacturer & "<br>"
strHTML = strHTML & "<b>Model</b>: " & objComputer.Model & "<br>"
Next
strHTML = strHTML& "<br>" & "<font face='Arial' size='2' color='white''>"
strComputer = MachineName.Value
strHTML = strHTML& "<b>Serial Number</b><br> <br>"
strHTML = strHTML&"</font><font face='Arial' size='2'>"
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS")
For Each objItem in colItems
strHTML4 = strHTML4 & " " & objItem.SerialNumber & "<br><br>"
next
strHTML3 = "<font face='Arial' size='2' color='white''>"
strComputer = MachineName.Value
strHTML3 = strHTML3 & "<b>Windows Operating System Summary</b><br> <br>"
strHTML3 = strHTML3 &"</font><font face='Arial' size='2'>"
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objItem in colItems
strHTML3 = strHTML3 & "<b>Caption</b>: " & objItem.Caption & "<br>"
strHTML3 = strHTML3 & "<b>Version</b>: " & objItem.Version & "<br>"
strHTML3 = strHTML3 & "<b>Service Pack</b>: " & objItem.ServicePackMajorVersion & "<br>"
Next
strHTML3 = strHTML3 & "<br>" & "<br>" & "<font face='Arial' size='2' color='white''>"
'strHTML3 = strHTML3 & "<b>Last Reboot</b><br> <br>"
strHTML3 = strHTML3 & "</font><font face='Arial' size='2'>"
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objItem in colItems
dtmInstallDate = objItem.LastBootUpTime
'WMIDateStringToDate = FormatDateTime(dtmInstallDate,1) & " " & FormatDateTime(dtmInstallDate,4)
'WMIDateStringToDate = CDate(Left(dtmInstallDate, 4) & "/" & Mid(dtmInstallDate, 5, 2) & "/" & Mid(dtmInstallDate, 7, 2) _
'& " " & Mid (dtmInstallDate, 9, 2) & ":" & Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate, 13, 2))
WMIDateStringToDate= dtmInstallDate
'strHTML3 = strHTML3 & "<b>Computer Last Booted </b>: " & WMIDateStringToDate & "<br>"
Next
strHTML2 = "<font face='Arial' size='2' color='white''>"
strComputer = MachineName.Value
strHTML2 = strHTML2 & "<b>Network Adapter Properties</b><br> <br>"
strHTML2 = strHTML2 &"</font><font face='Arial' size='2'>"
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objItem in colItems
strHTML2 = strHTML2 & "<b>Description</b>: " & objItem.Description & "<br>"
strHTML2 = strHTML2 & "<b>Physical (MAC) address</b>: " & objItem.MACAddress & "<br>"
strHTML2 = strHTML2 & "<b>Host name</b>: " & objItem.DNSHostName & "<br>"
If Not IsNull(objItem.IPAddress) Then
For i = 0 To UBound(objItem.IPAddress)
strHTML2 = strHTML2 & "<b>IP address</b>: " & objItem.IPAddress(i) & "<br>"
Next
End If
If Not IsNull(objItem.IPSubnet) Then
For i = 0 To UBound(objItem.IPSubnet)
strHTML2 = strHTML2 & "<b>Subnet</b>: " & objItem.IPSubnet(i) & "<br>"
Next
End If
strHTML2 = strHTML2 & "<b>DHCP server</b>: " & objItem.DHCPServer & "<br>"
strHTML2 = strHTML2 & "<b>Primary WINS server</b>: " & objItem.WINSPrimaryServer & "<br>"
strHTML2 = strHTML2 & "<b>Secondary WINS server</b>: " & objItem.WINSSecondaryServer & "<br> <br>"
Next
DataArea.InnerHTML = strHTML & strHTML4 & strhtml3 & strhtml2
End Sub
下一个具有适当 error handling 的代码片段可能会有所帮助:
Sub WindowsLoad1
strHTML = strHTML & "<br>" & "<font face='Arial' size='2' color='white''>"
strComputer = MachineName.Value
strHTML = strHTML & "<b>Sys Info</b><br> <br>"
strHTML = strHTML & "</font><font face='Arial' size='2'>"
On Error Resume Next
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\" _
& strComputer & "\root\cimv2")
If Err.Number = 0 Then
On Error Goto 0
Else
msgbox "Data for current computer displayed due to the error: " _
& Err.Description _
, vbOKOnly + vbExclamation _
, "An error occured"
On Error Goto 0
strComputer = "."
MachineName.Value = strComputer
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\" _
& strComputer & "\root\cimv2")
End If
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
'''''''''''''''''''
' etc.
'''''''''''''''''''
如果用户输入了错误的主机名。我希望脚本显示一个消息框 "PC wmi broken or is offline" 它会显示一个错误,但由于这是发送给其他人的,我只想要一个基本的错误消息框
Sub WindowsLoad1
strHTML = strHTML & "<br>" & "<font face='Arial' size='2' color='white''>"
strComputer = MachineName.Value
strHTML = strHTML & "<b>Sys Info</b><br> <br>"
strHTML = strHTML & "</font><font face='Arial' size='2'>"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
strHTML = strHTML & "<b>Hostname</b>: " & objComputer.Name & "<br>"
strHTML = strHTML & "<b>Current User</b>: " & objComputer.UserName & "<br>"
strHTML = strHTML & "<b>Manufacturer</b>: " & objComputer.Manufacturer & "<br>"
strHTML = strHTML & "<b>Model</b>: " & objComputer.Model & "<br>"
Next
strHTML = strHTML& "<br>" & "<font face='Arial' size='2' color='white''>"
strComputer = MachineName.Value
strHTML = strHTML& "<b>Serial Number</b><br> <br>"
strHTML = strHTML&"</font><font face='Arial' size='2'>"
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS")
For Each objItem in colItems
strHTML4 = strHTML4 & " " & objItem.SerialNumber & "<br><br>"
next
strHTML3 = "<font face='Arial' size='2' color='white''>"
strComputer = MachineName.Value
strHTML3 = strHTML3 & "<b>Windows Operating System Summary</b><br> <br>"
strHTML3 = strHTML3 &"</font><font face='Arial' size='2'>"
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objItem in colItems
strHTML3 = strHTML3 & "<b>Caption</b>: " & objItem.Caption & "<br>"
strHTML3 = strHTML3 & "<b>Version</b>: " & objItem.Version & "<br>"
strHTML3 = strHTML3 & "<b>Service Pack</b>: " & objItem.ServicePackMajorVersion & "<br>"
Next
strHTML3 = strHTML3 & "<br>" & "<br>" & "<font face='Arial' size='2' color='white''>"
'strHTML3 = strHTML3 & "<b>Last Reboot</b><br> <br>"
strHTML3 = strHTML3 & "</font><font face='Arial' size='2'>"
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objItem in colItems
dtmInstallDate = objItem.LastBootUpTime
'WMIDateStringToDate = FormatDateTime(dtmInstallDate,1) & " " & FormatDateTime(dtmInstallDate,4)
'WMIDateStringToDate = CDate(Left(dtmInstallDate, 4) & "/" & Mid(dtmInstallDate, 5, 2) & "/" & Mid(dtmInstallDate, 7, 2) _
'& " " & Mid (dtmInstallDate, 9, 2) & ":" & Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate, 13, 2))
WMIDateStringToDate= dtmInstallDate
'strHTML3 = strHTML3 & "<b>Computer Last Booted </b>: " & WMIDateStringToDate & "<br>"
Next
strHTML2 = "<font face='Arial' size='2' color='white''>"
strComputer = MachineName.Value
strHTML2 = strHTML2 & "<b>Network Adapter Properties</b><br> <br>"
strHTML2 = strHTML2 &"</font><font face='Arial' size='2'>"
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For Each objItem in colItems
strHTML2 = strHTML2 & "<b>Description</b>: " & objItem.Description & "<br>"
strHTML2 = strHTML2 & "<b>Physical (MAC) address</b>: " & objItem.MACAddress & "<br>"
strHTML2 = strHTML2 & "<b>Host name</b>: " & objItem.DNSHostName & "<br>"
If Not IsNull(objItem.IPAddress) Then
For i = 0 To UBound(objItem.IPAddress)
strHTML2 = strHTML2 & "<b>IP address</b>: " & objItem.IPAddress(i) & "<br>"
Next
End If
If Not IsNull(objItem.IPSubnet) Then
For i = 0 To UBound(objItem.IPSubnet)
strHTML2 = strHTML2 & "<b>Subnet</b>: " & objItem.IPSubnet(i) & "<br>"
Next
End If
strHTML2 = strHTML2 & "<b>DHCP server</b>: " & objItem.DHCPServer & "<br>"
strHTML2 = strHTML2 & "<b>Primary WINS server</b>: " & objItem.WINSPrimaryServer & "<br>"
strHTML2 = strHTML2 & "<b>Secondary WINS server</b>: " & objItem.WINSSecondaryServer & "<br> <br>"
Next
DataArea.InnerHTML = strHTML & strHTML4 & strhtml3 & strhtml2
End Sub
下一个具有适当 error handling 的代码片段可能会有所帮助:
Sub WindowsLoad1
strHTML = strHTML & "<br>" & "<font face='Arial' size='2' color='white''>"
strComputer = MachineName.Value
strHTML = strHTML & "<b>Sys Info</b><br> <br>"
strHTML = strHTML & "</font><font face='Arial' size='2'>"
On Error Resume Next
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\" _
& strComputer & "\root\cimv2")
If Err.Number = 0 Then
On Error Goto 0
Else
msgbox "Data for current computer displayed due to the error: " _
& Err.Description _
, vbOKOnly + vbExclamation _
, "An error occured"
On Error Goto 0
strComputer = "."
MachineName.Value = strComputer
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\" _
& strComputer & "\root\cimv2")
End If
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
'''''''''''''''''''
' etc.
'''''''''''''''''''