我一直在研究受这些帖子启发的 VBscript: http://deployhappiness.com/series/creating-an-inventory-with-active-directory/ 我发现,在测试机器中,制造商、型号系列、MTM 和序列号条目取得了相当大的成功。话虽如此,我在上传 CPU 名称和安装的物理内存到我设置的自定义属性(AD 属性,如“RAM”、“处理器”等)时遇到了障碍。用于测试这些脚本的所有机器都已获得适当的 SELF 权限,可以使用脚本写入这些属性,我在将它们放入 netlogon 脚本之前,在每台机器上对它们进行本地测试。
我一直尝试使用的代码是这样的,并且我咨询了 WMIExplorer 以获得指导,但没有成功让这两个脚本正常工作:
strprocessor= GetCPU
updatecpu(strprocessor)
Function GetCPU
strcomputer = "."
set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strcomputer & "root\cimv2")
set colprocessors = objWMIservice.ExecQuery _
("Select * from Win32_Processor")
For each objprocessor in colprocessors
GetCPU= objprocessor.name
Next
End Function
Sub Updatecpu(strprocessor)
Set objSysInfo = CreateObject("ADSystemInfo")
On Error Resume Next
Set objComputer = GetObject ("LDAP://" & objSysInfo.ComputerName)
objProcessor.name = strprocessor
objComputer.SetInfo
End Sub
以及记忆功能:
strRAM= GetRAM
updateRAM(strRAM)
Function GetRAM
strComputer = "*"
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate} !\\" & strcomputer & "root\cimv2")
set colcomputersystem = objWMIService.ExecQuery _
("Select * from Win32_Computersystem")
For each objcomputersystem in colcomputersystem
GetRAM= objcomputersystem.totalphysicalmemory
Next
End Function
Sub UpdateRAM(strRAM)
Set objSysInfo = CreateObject ("ADSystemInfo")
On Error Resume Next
Set objComputer = GetObject ("LDAP://" & objSysInfo.ComputerName)
objcomputersystem.totalphysicalmemory = strRAM
objComputer.SetInfo
End Sub
相比之下,有效的制造商组件的格式几乎相同:
Strmanufacturer = getmanufacturer
updatemanufacturer(strmanufacturer)
Function Getmanufacturer
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colcomputersystem = objWMIService.ExecQuery _
("Select * from Win32_computersystem")
For each objcomputersystem in colcomputersystem
Getmanufacturer = objcomputersystem.manufacturer
Next
End Function
Sub Updatemanufacturer(strmanufacturer)
Set objSysInfo = CreateObject("ADSystemInfo")
On Error Resume Next
Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName)
objComputer.manufacturer = strmanufacturer
objComputer.SetInfo
End Sub
我知道我不得不在这里搞砸一个 obj 或 col 类别,但我还没能弄清楚。有什么建议吗?
答案1
首先,使用
Option Explicit
陈述 总是(如果使用,该Option Explicit
语句必须出现在脚本中的任何其他语句之前)并且On Error GoTo 0
陈述至少对于调试来说。
On Error GoTo 0
使用如下方法调试脚本:
Sub Updatecpu(strprocessor)
On Error GoTo 0 ' don't continue in case of severe error
Set objSysInfo = CreateObject("ADSystemInfo")
''' On Error Resume Next
Set objComputer = GetObject ("LDAP://" & objSysInfo.ComputerName)
objProcessor.name = strprocessor
objComputer.SetInfo
End Sub
您On Error Resume Next
应该至少建立基本的错误处理以公开偶然的错误代码和消息:
Sub Updatecpu(strprocessor)
On Error GoTo 0 ' don't continue in case of severe error
Set objSysInfo = CreateObject("ADSystemInfo")
Set objComputer = GetObject ("LDAP://" & objSysInfo.ComputerName)
On Error Resume Next ' continue, I use my own error handling approach
objProcessor.name = strprocessor
If Err.Number <> 0 Then
msgbox "error 0x" & Hex( Err.Number) & " " & CStr( Err.Number) _
& " >" & Err.Description & "< " & Err.Source
End If
Err.Clear ' clear the Err object after an error has been handled
objComputer.SetInfo
If Err.Number <> 0 Then
msgbox "error 0x" & Hex( Err.Number) & " " & CStr( Err.Number) _
& " >" & Err.Description & "< " & Err.Source
End If
On Error Goto 0 ' return back to system error handling
End Sub
例如,乍一看:objcomputersystem
变量在函数中有局部作用域Getmanufacturer
,在过程中没有定义Updatemanufacturer
;类似地,objProcessor
变量在函数中有局部作用域GetCPU
,在过程中没有定义Updatecpu
过程中定义......阅读VBScript 变量MSDN 文章。
另一个问题:你的GetCPU
函数将返回唯一值的情况下多处理器机器(收藏中的最后一件物品colprocessors
)。
请注意,我把我的答案保留在VBScript
主题一致;我知道没有什么关于ADSystemInfo
对象属性和LDAP
协议。