我想知道是否可以通过 VBScript 导出 SCCM 2007 查询的结果。如果可以的话,我将不胜感激。
我知道我可以通过 VBScript 使用报告的 Web 视图通过 SCCM 导出报告,并伪造 export=yes 并提交表单。使用查询可以实现类似的效果吗?
我最终得到的工作代码是 VBScript 函数:
'sccmQueryAsArray
'Returns: results in an array built based on the columns specified in the call of the function
' parseInto: an array provided by the calling function that will be filled with the SCCM query results
' sccmSiteServerName: a string representing the sccm site server we are connecting to
' sccmSiteCode: a string representing the sccm site code we are connecting to
' columns: a one dimensional array containing the names of the columns you want to the function to create an array for
' query: the actual full length query that will be passed to WMI object
' example call: sccmQueryAsArray data,"server1","sit",array("Name","UsergroupName"),"select Name, UsergroupName, WindowsNTDomain from sms_r_usergroup where AgentName = 'SMS_AD_SECURITY_GROUP_DISCOVERY_AGENT'"
' results in an array "data" being modified to 2 columns and as many rows as is returned by the query the array would look like:
' (0)(x) where 0 = the first element in the columns row or Name
' (1)(x) where 1 = the second element in the columns row or UsergroupName
function sccmQueryAsArray( ByRef parseInto, ByVal sccmSiteServerName, ByVal sccmSiteCode, ByVal columns, ByVal query )
redim preserve parseInto(ubound(columns),0) 'the array that the query information will be parsed into and then returned
dim objWMIService: set objWMIService = getObject("winmgmts://" & sccmSiteServerName & "\root\sms\site_" & sccmSiteCode) 'set up the connection string
dim colGroups: set colGroups = objWMIService.ExecQuery(query) 'execute the query and put the results into colGroups
dim z: z = 0
dim objGroup: for each objGroup in colGroups
dim y: for y = 0 to ubound(columns) step 1
dim x: x = "objGroup." & columns(y)
parseInto(y,z) = eval(x)
next
redim preserve parseInto(ubound(parseInto,1),ubound(parseInto,2)+1): z = z + 1
next
sccmQueryAsArray = parseInto
end function
答案1
为什么不直接在 VBScript 中执行查询?
您可以使用 VBScript 连接到 SCCM 服务器,并使用 ExecQuery 向服务器发送 WQL 查询。
要使用将 SCCM 查询转换为 VBScript 的简单示例,您可能有一个列出所有 AD 安全组的查询。右键单击该查询,单击编辑查询语句,然后单击显示查询语言。
你应该有类似这样的内容:
select Name, UsergroupName, WindowsNTDomain from sms_r_usergroup where AgentName = 'SMS_AD_SECURITY_GROUP_DISCOVERY_AGENT'
现在单击“显示查询设计”并复制您感兴趣的属性名称(即列标题),您应该得到如下列表:
Name, User Group Name, Domain
要获取这些列的实际 SQL 列名(尤其是带有空格的列名),只需查看上一步中的 WQL 查询,您可以在其中看到命令后直接列出的列名select
。您现在应该有类似以下内容:
Name, UsergroupName, WindowsNTDomain
现在,只需将所有这些放入 VBScript 中,例如:
'Central SCCM Site Server name
strComputer = "SCCM01"
'Central SCCM Site Code
strSiteCode = "ABC"
'Set up the connection String
Set objWMIService = GetObject("winmgmts://" & strComputer & "\root\sms\site_" & strSiteCode)
'Get the info with a query
Set colGroups = objWMIService.ExecQuery("select Name, UsergroupName, WindowsNTDomain from sms_r_usergroup where AgentName = 'SMS_AD_SECURITY_GROUP_DISCOVERY_AGENT'")
'output the info
For Each objGroup in colGroups
Wscript.echo objGroup.Name & ", " & objGroup.UsergroupName & ", " & objGroup.WindowsNTDomain
Next
(显然,编辑站点服务器和站点代码行以匹配您的环境)。
答案2
很棒的帖子。我只是想补充一点,如果你想使用 PowerShell 而不是 VBS,你应该看看这个:
http://blogs.technet.com/b/manageabilityguys/archive/2009/11/27/more-on-sccm-and-powershell.aspx
SCCM 2012 内置有 PowerShell,因此不需要对新产品执行此操作。