Skip to content

Commit 37d1ece

Browse files
Fix local vs remote WMI access patterns in CCMApplicationService and CCMBaselineService
Co-authored-by: CodyMathis123 <28543620+CodyMathis123@users.noreply.github.com>
1 parent 82a4734 commit 37d1ece

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

src/PSCCMClient.Core/Services/CCMApplicationService.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ public CCMApplicationService(string computerName = ".")
1515
_computerName = computerName;
1616
}
1717

18+
/// <summary>
19+
/// Helper method to get the correct namespace path for local or remote operations
20+
/// </summary>
21+
private string GetNamespacePath(string baseNamespace)
22+
{
23+
bool isLocal = _computerName == "." || _computerName.Equals(Environment.MachineName, StringComparison.OrdinalIgnoreCase);
24+
return isLocal ? baseNamespace : $@"\\{_computerName}\{baseNamespace}";
25+
}
26+
1827
/// <summary>
1928
/// Gets all applications from the Configuration Manager client
2029
/// </summary>
@@ -44,7 +53,7 @@ public IEnumerable<CCMApplication> GetApplications()
4453

4554
try
4655
{
47-
var scope = new ManagementScope($@"\\{_computerName}\root\CCM\ClientSDK");
56+
var scope = new ManagementScope(GetNamespacePath("root\\CCM\\ClientSDK"));
4857
scope.Connect();
4958

5059
using var searcher = new ManagementObjectSearcher(scope, new ObjectQuery("SELECT * FROM CCM_Application"));
@@ -79,7 +88,7 @@ public IEnumerable<CCMApplication> GetApplicationsByName(string applicationName)
7988

8089
try
8190
{
82-
var scope = new ManagementScope($@"\\{_computerName}\root\CCM\ClientSDK");
91+
var scope = new ManagementScope(GetNamespacePath("root\\CCM\\ClientSDK"));
8392
scope.Connect();
8493

8594
var query = $"SELECT * FROM CCM_Application WHERE Name LIKE '%{applicationName}%'";
@@ -123,7 +132,7 @@ public bool InstallApplication(string applicationId)
123132
{
124133
try
125134
{
126-
var scope = new ManagementScope($@"\\{_computerName}\root\CCM\ClientSDK");
135+
var scope = new ManagementScope(GetNamespacePath("root\\CCM\\ClientSDK"));
127136
scope.Connect();
128137

129138
using var appClass = new ManagementClass(scope, new ManagementPath("CCM_Application"), null);

src/PSCCMClient.Core/Services/CCMBaselineService.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ public CCMBaselineService(string computerName)
1515
_computerName = computerName ?? ".";
1616
}
1717

18+
/// <summary>
19+
/// Helper method to get the correct namespace path for local or remote operations
20+
/// </summary>
21+
private string GetNamespacePath(string baseNamespace)
22+
{
23+
bool isLocal = _computerName == "." || _computerName.Equals(Environment.MachineName, StringComparison.OrdinalIgnoreCase);
24+
return isLocal ? baseNamespace : $@"\\{_computerName}\{baseNamespace}";
25+
}
26+
1827
/// <summary>
1928
/// Gets configuration baselines from the client
2029
/// </summary>
@@ -40,7 +49,7 @@ public List<CCMBaseline> GetBaselines(string? baselineName = null)
4049

4150
try
4251
{
43-
using var searcher = new ManagementObjectSearcher($@"\\{_computerName}\root\ccm\dcm", query);
52+
using var searcher = new ManagementObjectSearcher(GetNamespacePath("root\\ccm\\dcm"), query);
4453
using var results = searcher.Get();
4554

4655
foreach (ManagementObject obj in results)
@@ -83,7 +92,7 @@ public bool InvokeBaseline(string baselineName)
8392
try
8493
{
8594
var query = $"SELECT * FROM SMS_DesiredConfiguration WHERE DisplayName = '{baselineName}'";
86-
using var searcher = new ManagementObjectSearcher($@"\\{_computerName}\root\ccm\dcm", query);
95+
using var searcher = new ManagementObjectSearcher(GetNamespacePath("root\\ccm\\dcm"), query);
8796
using var results = searcher.Get();
8897

8998
foreach (ManagementObject obj in results)

0 commit comments

Comments
 (0)