我想知道是否可以通过 VBScript 导出 SCCM 2007 查询的结果。如果有任何建议,将不胜感激。
我知道我可以通过 VBScript 通过 SCCM 导出报告,方法是使用报告的 Web 视图并伪造 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
为什么不直接在 VBScript 中执行查询?
您可以在 VBScript 中附加到 SCCM 服务器,并使用 ExecQuery 将 WQL 查询发送到服务器。
要使用将 SCCM 查询转换为 VBScript 的简单示例,您可能有一个列出所有 AD 安全组的查询。右键单击该查询,单击编辑查询语句,然后单击显示查询语言。
你应该有这样的东西:
现在单击 Show Query Design 并复制您感兴趣的属性名称(即列标题),您应该得到如下列表:
select
要获取这些列的实际 SQL 列名(特别是其中有空格的列名),只需查看上一步中的 WQL 查询,您可以在命令后看到列名。你现在应该有这样的东西:现在把所有这些都放到 VBScript 中,比如:
(显然编辑站点服务器和站点代码行以匹配您的环境)。
很棒的帖子。我只是想补充一点,如果您想使用 PowerShell 而不是 VBS,您应该检查一下:
http://blogs.technet.com/b/manageabilityguys/archive/2009/11/27/more-on-sccm-and-powershell.aspx
SCCM 2012 内置了 PowerShell,因此无需为新产品执行此操作。