Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

- Handle multiple SCCM configuration
- Add `collection` scope


## [2.5.1] - 2025-10-07

Expand Down
14 changes: 14 additions & 0 deletions inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ public function rawSearchOptions(): array
'datatype' => 'string',
];

$options[] = [
'id' => 8,
'table' => $this->getTable(),
'field' => 'sccm_collection_name',
'name' => __('Collection name', 'sccm'),
'datatype' => 'string',
];

$options[] = [
'id' => 6,
'table' => $this->getTable(),
Expand Down Expand Up @@ -205,6 +213,7 @@ public static function install(Migration $migration): bool
`sccmdb_dbname` VARCHAR(255) NULL,
`sccmdb_user` VARCHAR(255) NULL,
`sccmdb_password` VARCHAR(255) NULL,
`sccm_collection_name` VARCHAR(255) NULL,
`inventory_server_url` VARCHAR(255) NULL,
`active_sync` tinyint NOT NULL DEFAULT '0',
`verify_ssl_cert` tinyint NOT NULL DEFAULT '0',
Expand Down Expand Up @@ -289,6 +298,11 @@ public static function install(Migration $migration): bool
$migration->migrationOneTable($table);
}

if (!$DB->fieldExists($table, 'sccm_collection_name')) {
$migration->addField($table, 'sccm_collection_name', 'string', ['after' => 'sccmdb_password', 'value' => '']);
$migration->migrationOneTable($table);
}

if (!$DB->fieldExists($table, 'use_lasthwscan')) {
$migration->addField($table, "use_lasthwscan", "tinyint NOT NULL default '0'");
$migration->migrationOneTable($table);
Expand Down
26 changes: 22 additions & 4 deletions inc/sccm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ public function getDevices(int $config_id, $where = 0, $limit = 99999999): void
);
}

$query = self::getcomputerQuery();
$config = new PluginSccmConfig();
$config->getFromDB($config_id);

$collection_name = (string) ($config->getField('sccm_collection_name') ?? '');

$query = self::getcomputerQuery($collection_name);

if ($where != 0) {
$query .= " WHERE csd.MachineID = '" . $where . "'";
Expand Down Expand Up @@ -504,9 +509,9 @@ public static function executeCollect($task): int
return $retcode;
}

public static function getcomputerQuery(): string
public static function getcomputerQuery(string $collection_name = ''): string
{
return "SELECT csd.Description00 as \"CSD-Description\",
$query = "SELECT csd.Description00 as \"CSD-Description\",
csd.Domain00 as \"CSD-Domain\",
csd.Manufacturer00 as \"CSD-Manufacturer\",
csd.Model00 as \"CSD-Model\",
Expand Down Expand Up @@ -549,6 +554,18 @@ public static function getcomputerQuery(): string
LEFT JOIN System_DATA sd ON csd.MachineID = sd.MachineID
INNER JOIN v_R_System VrS ON csd.MachineID = VrS.ResourceID
WHERE csd.MachineID is not null and csd.MachineID != ''";

if ($collection_name !== '') {
$safe_name = str_replace("'", "''", $collection_name);
$query .= " AND csd.MachineID IN (
SELECT fcm.ResourceID
FROM v_FullCollectionMembership fcm
INNER JOIN v_Collection vc ON fcm.CollectionID = vc.CollectionID
WHERE vc.Name = N'{$safe_name}'
)";
}

return $query;
}

public static function executePush(CronTask $task): int
Expand Down Expand Up @@ -588,8 +605,9 @@ public static function executePush(CronTask $task): int

$config = new PluginSccmConfig();
$config->getFromDB($config_id);
$collection_name = (string) ($config->getField('sccm_collection_name') ?? '');

$query = self::getcomputerQuery();
$query = self::getcomputerQuery($collection_name);
$result = $PluginSccmSccmdb->exec_query($query);

while ($tab = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
Expand Down
7 changes: 7 additions & 0 deletions templates/config.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@
{'autocomplete': 'off'}
) }}

{{ fields.textField(
'sccm_collection_name',
item.fields['sccm_collection_name'],
__('Collection name', 'sccm'),
{'helper': __('Leave empty to collect all devices', 'sccm')}
) }}

{% set alert %}
<span class="text-danger">{{ url }}</span>
{% endset %}
Expand Down