VBS - 查找计算机所属的SCCM集合ID,名称和程序包名称?

时间:2018-05-16 10:53:27

标签: vbscript sccm

目前我对主题要求有'excel SQL查询',并且执行正常,没有任何问题。

当前的Excel SQL查询脚本:我创建了SQL查询,该查询连接到SCCM服务器并获取以下详细信息。

Excel 2013 / 2016->数据 - >连接 - >工作簿连接 - > Excel SQL Query

连接字符串:

Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=CM_CAS;Data Source=<SCCMServerIP>;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=<SCCMServerHostName>;Use Encryption for Data=False;Tag with column collation when possible=False

命令类型: SQL

命令文字:

DECLARE @machname varchar(max)

SET @machname = '<DeviceHostName>'

select CollectionID,CollectionName,packagename,@machname as MachineName from v_AdvertisementInfo 

-- select AssignmentID,AssignmentName,CollectionID,CollectionName,ApplicationName,AppModelID from v_ApplicationAssignment

where CollectionID in 

(select FCM.CollectionId from dbo.v_R_System r join 

dbo.v_FullCollectionMembership FCM on R.ResourceID = FCM.ResourceID join 

dbo.v_Collection C on C.CollectionID = FCM.CollectionID Where R.Name0 

= @machname) and 

ProgramName not like '%Remove%'and 
 ProgramName not like '%Un-Install%'and 
 CollectionName not like '%Test%' and
CollectionName not like '%temp%'


union

select CollectionID,CollectionName,ApplicationName,@machname as MachineName from v_ApplicationAssignment
where CollectionID in 

(select FCM.CollectionId from dbo.v_R_System r join 

dbo.v_FullCollectionMembership FCM on R.ResourceID = FCM.ResourceID join 

dbo.v_Collection C on C.CollectionID = FCM.CollectionID Where R.Name0 

= @machname) and

CollectionName not like '%Test%' and
CollectionName not like '%temp%'

+++++++++++++++++++++++++++++++++++++++++++++

示例Excel输出如下

hostname 'IN-00001236'

Result/Output

笔记本电脑EOL刷新

要求是

收集IN-00001236(EOL设备)成员的所有集合ID,并将新设备主机名(IN-1111)添加到结果集合ID中!然后从SCCM中删除IN-00001236。在excel / CSV中生成报告?

我有一个挑战在上面的Excel SQL查询?就像我在time/run

下的每个'Command Text'添加/替换主机名一样

那么,我需要使用VBS或PS自动完成这个完整的过程吗?

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望新记录拥有旧记录的所有成员资格。

在我看来,您只关注集合成员资格,只要集合是相同的,对集合的部署是一个包,应用程序或更新是否重要。

首先,您需要将设备名称转换为resourceid,因为这些是所有sccm表中的主键:

$WS = 'IN-00001236'
$resourceID = (Get-WmiObject -Class "SMS_R_System" -Filter "(Name = '$WS')" | Select-Object -last 1).ResourceID

一旦拥有,您就可以获得所有收集成员

$collectionMembers = (Get-WmiObject -Class "SMS_FullCollectionMembership" -Filter "(ResourceID = '$resourceID')"  | where { $_.IsDirect -eq $true }).CollectionID

删除记录使用此

[void](Get-WmiObject -Class "SMS_R_System" -Filter "(ResourceID = '$resourceID')").Delete()

现在你必须将它们添加到新记录中(首先再次获取resourceID)

Foreach($CollID in $collectionMembers) {
    $CollectionQuery = Get-WmiObject -Class "SMS_Collection" -Filter "CollectionID = '$CollID'"
    $directRule = (Get-WmiObject -List -Class "SMS_CollectionRuleDirect").CreateInstance()
    $directRule.ResourceClassName = "SMS_R_System"
    $directRule.ResourceID = $newResourceID
    [void]$CollectionQuery.AddMemberShipRule($directRule)
}

如果要在循环中将多个设备用于此设置,可以使用oldrecord,newrecord创建一个csv文件 导入

$records = Import-Csv <path to csv> -Header old,new

然后在所有代码之外使用循环,如

foreach ($record in $records) {
  # inside here use $record.old or $record.new for the old/new names like
  # $WS = $record.old
}

现在我不知道你是否还需要excel文件,如果你只是有一个脚本来完成工作,但如果你真的这样做,你可以使用export-csv从powershell创建一个excel可读的csv(可能需要选项-Delimiter&#39;;&#39; -NoTypeInformation -Encoding UTF8易于阅读)

理论上你也可以在powershell中执行你的整个SQL查询

$sqlText = "your query"
$SQLConnection = new-object System.Data.SqlClient.SQLConnection("Server=<sccmserver>;Database=<sccm db>;Integrated Security=SSPI")
$SQLConnection.Open();

$cmd = new-object System.Data.SqlClient.SqlCommand($sqlText, $SQLConnection);    
$reader = $cmd.ExecuteReader()

while ($reader.Read()) {
    $reader["<columnname>"] 
}

并在ps脚本中重新创建文件

答案 1 :(得分:0)

最后,我按如下所示完成了VBS。一切正常。

Dim connect, sql, resultSet, pth, txt

Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set connect = CreateObject("ADODB.Connection")
connect.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=CM_CAS;Data Source=<SCCMServerIP>;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=<SCCMServerHostName>;Use Encryption for Data=False;Tag with column collation when possible=False;Trusted_Connection=True;"
connect.Open

sql="select CollectionID,CollectionName,packagename,<HostName> as MachineName from v_AdvertisementInfo where CollectionID in (select FCM.CollectionId from dbo.v_R_System r join dbo.v_FullCollectionMembership FCM on R.ResourceID = FCM.ResourceID join dbo.v_Collection C on C.CollectionID = FCM.CollectionID Where R.Name0 = 'HostName') and ProgramName not like '%Remove%'and ProgramName not like '%Un-Install%'and CollectionName not like '%Test%' and CollectionName not like '%temp%' union select CollectionID,CollectionName,ApplicationName,@machname as MachineName from v_ApplicationAssignment where CollectionID in (select FCM.CollectionId from dbo.v_R_System r join dbo.v_FullCollectionMembership FCM on R.ResourceID = FCM.ResourceID join dbo.v_Collection C on C.CollectionID = FCM.CollectionID Where R.Name0 = 'HostName') and CollectionName not like '%Test%' and CollectionName not like '%temp%'"

Set resultSet = connect.Execute(sql)

pth = "C:\test\test.csv"

Set txt = ObjFSO.CreateTextFile(pth, True)

On Error Resume Next
resultSet.MoveFirst
Do While Not resultSet.eof
  txt.WriteLine(resultSet(0) & "," & resultSet(1) & "," & resultSet(2))
  resultSet.MoveNext
Loop

resultSet.Close
connect.Close
Set connect = Nothing