Skip to content

Commit 17e8e48

Browse files
Update calling-code-snippets.md
Reformatted to follow MS style guidelines. Added PowerShell example code.
1 parent 45de81e commit 17e8e48

1 file changed

Lines changed: 98 additions & 11 deletions

File tree

intune/configmgr/develop/core/understand/calling-code-snippets.md

Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,97 @@ ms.subservice: sdk
66
ms.topic: article
77
ms.collection: tier3
88
---
9+
910
# Calling Configuration Manager Code Snippets
11+
1012
The following code samples show how to set up the calling code for the code examples that are used throughout the Configuration Manager Software Development Kit (SDK).
1113

12-
Replace the SNIPPETMETHOD snippet with the snippet that you want to run. In most cases you will need to make changes, such as adding parameters, to make the code work.
14+
Replace the SNIPPETMETHOD snippet with the snippet that you want to run. In most cases you will need to make changes, such as adding parameters, to make the code work.
15+
16+
For more information about remote Windows Management Instrumentation (WMI) connections, see [Connecting to WMI on a Remote Computer](/windows/win32/wmisdk/connecting-to-wmi-on-a-remote-computer.md).
17+
18+
## Examples
19+
20+
```powershell
21+
# Prompt for computer name
22+
$computer = Read-Host "Computer you want to connect to (Enter . for local)"
23+
24+
# Determine whether to prompt for credentials
25+
if ($computer -eq ".") {
26+
$credential = $null
27+
}
28+
else {
29+
$credential = Get-Credential -Message "Enter credentials for $computer"
30+
}
31+
32+
# Connect to SMS Provider
33+
$connection = Connect-SMSProvider -Server $computer -Credential $credential
34+
35+
if (-not $connection) {
36+
Write-Host "Call to connect failed"
37+
}
38+
else {
39+
SNIPPETMETHODNAME -Connection $connection
40+
}
41+
42+
#region Functions
43+
44+
function Connect-SMSProvider {
45+
param(
46+
[string]$Server,
47+
[System.Management.Automation.PSCredential]$Credential
48+
)
49+
50+
try {
51+
# If local machine, do not use credentials
52+
if ($Server -eq "." -or $Server -eq $env:COMPUTERNAME) {
53+
$Credential = $null
54+
$Server = "."
55+
}
56+
57+
# Connect to root\sms
58+
$rootSmsParams = @{
59+
ComputerName = $Server
60+
Namespace = "root\sms"
61+
ErrorAction = 'Stop'
62+
}
63+
if ($Credential) { $rootSmsParams.Credential = $Credential }
64+
65+
$smsRoot = Get-WmiObject @rootSmsParams
66+
67+
# Find provider location
68+
$providerLocations = $smsRoot.InstancesOf("SMS_ProviderLocation")
69+
$localProvider = $providerLocations | Where-Object ProviderForLocalSite
70+
$siteNamespace = "root\sms\site_$($localProvider.SiteCode)"
71+
72+
$siteParams = @{
73+
ComputerName = $localProvider.Machine
74+
Namespace = $siteNamespace
75+
ErrorAction = 'Stop'
76+
}
77+
if ($Credential) { $siteParams.Credential = $Credential }
1378
14-
For more information about remote Windows Management Instrumentation (WMI) connections, see [Connecting to WMI on a Remote Computer](/windows/win32/wmisdk/connecting-to-wmi-on-a-remote-computer).
79+
Get-WmiObject @siteParams
80+
81+
return $null
82+
}
83+
catch {
84+
Write-Host "Couldn't connect: $($_.Exception.Message)"
85+
return $null
86+
}
87+
}
1588
16-
## Example
89+
function SNIPPETMETHODNAME {
90+
param(
91+
$Connection
92+
)
93+
94+
# Insert snippet code here
95+
Write-Host "Connected successfully. Insert your code here."
96+
}
97+
#endregion
98+
99+
```
17100

18101
```vbs
19102
Dim connection
@@ -220,22 +303,26 @@ namespace ConfigurationManagerSnippets
220303
## Compiling the Code
221304

222305
### Namespaces
223-
System
224306

225-
Microsoft.ConfigurationManagement.ManagementProvider
307+
System
226308

227-
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
309+
Microsoft.ConfigurationManagement.ManagementProvider
310+
311+
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
228312

229313
### Assembly
230-
adminui.wqlqueryengine
231314

232-
microsoft.configurationmanagement.managementprovider
315+
adminui.wqlqueryengine
316+
317+
microsoft.configurationmanagement.managementprovider
233318

234319
> [!NOTE]
235-
> The assemblies are in the \<Program Files>\Microsoft Endpoint Manager\AdminConsole\bin folder.
320+
> The assemblies are in the \<Program Files>\Microsoft Endpoint Manager\AdminConsole\bin folder.
236321
237322
## Runtime Requirements
238-
For more information, see [Configuration Manager Server Runtime Requirements](../../../develop/core/reqs/server-runtime-requirements.md).
323+
324+
For more information, see [Configuration Manager Server Runtime Requirements](../../../develop/core/reqs/server-runtime-requirements.md).
239325

240326
## Robust Programming
241-
The Configuration Manager exceptions that can be raised are [SmsConnectionException](/previous-versions/system-center/developer/cc147431(v=msdn.10)) and [SmsQueryException](/previous-versions/system-center/developer/cc147436(v=msdn.10)). These can be caught together with [SmsException](/previous-versions/system-center/developer/cc147433(v=msdn.10)).
327+
328+
The Configuration Manager exceptions that can be raised are [SmsConnectionException](/previous-versions/system-center/developer/cc147431(v=msdn.10)) and [SmsQueryException](/previous-versions/system-center/developer/cc147436(v=msdn.10)). These can be caught together with [SmsException](/previous-versions/system-center/developer/cc147433(v=msdn.10)).

0 commit comments

Comments
 (0)