For循环中的变量保留旧值

时间:2021-10-29 20:26:56

I have a program that reads hostnames from a txt file, it scans the network for the hostname and then it displays the hostname and its respective Windows Operating system (CAPTION).

我有一个程序从txt文件读取主机名,它扫描网络中的主机名,然后显示主机名及其各自的Windows操作系统(CAPTION)。

I am trying to get all Windows XP machines upgraded to Windows 7. I am trying to run this list to give me an idea of how many machines that I have upgraded and an idea of how many I still have to upgrade, etc.

我正在尝试将所有Windows XP计算机升级到Windows 7.我正在尝试运行此列表,以便让我了解已升级的计算机数量以及我仍需要升级的数量等等。

The problem is that when I use the statement On Error Resume Next if the script tries to contact a hostname which is a BAD HOST or if the hostname is DOWN it displays the operating system from the last hostname. Then each and every name that is scans moving forward all shows that same operating system.

问题是当我使用语句On Error Resume Next,如果脚本试图联系主机名是一个坏主机,或者主机名是DOWN,它会显示上一个主机名的操作系统。然后,扫描前进的每个名称都显示相同的操作系统。

What might be causing this error?

可能导致此错误的原因是什么?

On Error Resume Next

const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile= objFSO.OpenTextFile _
    ("C:\users\bh\desktop\hostnames.txt", ForReading)

strText = objTextFile.ReadAll
objTextFile.close

arrComputers = Split(strText, vbCrlf)

for Each strComputer in arrComputers
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set colSettings = objWMIService.ExecQuery _
        ("Select * from Win32_OperatingSystem")

    For Each objOperatingSystem in colSettings 
        Wscript.Echo strComputer & ": " & objOperatingSystem.Caption
    Next
Next

1 个解决方案

#1


Using a Global On Error Resume Next is just asking for desaster - all errors will be ignored, assignment won't be done as you expect and stale data will be used.

使用Global On Error Resume Next只是要求desaster - 所有错误都将被忽略,分配将不会按预期完成,并且将使用过时数据。

This:

Dim aErr

arrComputers = Split(". winxpsp3 nix")

for Each strComputer in arrComputers
  On Error Resume Next
   Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
   aErr = Array(Err.Number, Err.Description)
  On Error Goto 0
   If 0 = aErr(0) Then
      Set colSettings = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
      For Each objOperatingSystem in colSettings
          Wscript.Echo strComputer & ": " & objOperatingSystem.Caption
      Next
  Else
    WScript.Echo "can't reach", strComputer, "error:", Join(aErr)
  End If
Next

output:

cscript 30223065.vbs
.: Microsoft Windows XP Professional
winxpsp3: Microsoft Windows XP Professional
can't reach nix error: 462 The remote server machine does not exist or is unavailable

demonstrates a strictly local error handling (max one risky operation between OERN and OEG0) for your first risky task. You'll have to guard/wrap the others accordingly or check return values.

为您的第一个风险任务演示严格的本地错误处理(OERN和OEG0之间的最大风险操作)。你必须相应地保护/包装其他人或检查返回值。

(see this for a strategy for global error handling)

(有关全局错误处理的策略,请参阅此处)

#1


Using a Global On Error Resume Next is just asking for desaster - all errors will be ignored, assignment won't be done as you expect and stale data will be used.

使用Global On Error Resume Next只是要求desaster - 所有错误都将被忽略,分配将不会按预期完成,并且将使用过时数据。

This:

Dim aErr

arrComputers = Split(". winxpsp3 nix")

for Each strComputer in arrComputers
  On Error Resume Next
   Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
   aErr = Array(Err.Number, Err.Description)
  On Error Goto 0
   If 0 = aErr(0) Then
      Set colSettings = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
      For Each objOperatingSystem in colSettings
          Wscript.Echo strComputer & ": " & objOperatingSystem.Caption
      Next
  Else
    WScript.Echo "can't reach", strComputer, "error:", Join(aErr)
  End If
Next

output:

cscript 30223065.vbs
.: Microsoft Windows XP Professional
winxpsp3: Microsoft Windows XP Professional
can't reach nix error: 462 The remote server machine does not exist or is unavailable

demonstrates a strictly local error handling (max one risky operation between OERN and OEG0) for your first risky task. You'll have to guard/wrap the others accordingly or check return values.

为您的第一个风险任务演示严格的本地错误处理(OERN和OEG0之间的最大风险操作)。你必须相应地保护/包装其他人或检查返回值。

(see this for a strategy for global error handling)

(有关全局错误处理的策略,请参阅此处)