Skip to content
Open
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
109 changes: 98 additions & 11 deletions intune/configmgr/develop/core/understand/calling-code-snippets.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,97 @@ ms.subservice: sdk
ms.topic: article
ms.collection: tier3
---

# Calling Configuration Manager Code Snippets

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).

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.
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.

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).

## Examples

```powershell
# Prompt for computer name
$computer = Read-Host "Computer you want to connect to (Enter . for local)"

# Determine whether to prompt for credentials
if ($computer -eq ".") {
$credential = $null
}
else {
$credential = Get-Credential -Message "Enter credentials for $computer"
}

# Connect to SMS Provider
$connection = Connect-SMSProvider -Server $computer -Credential $credential

if (-not $connection) {
Write-Host "Call to connect failed"
}
else {
SNIPPETMETHODNAME -Connection $connection
}

#region Functions

function Connect-SMSProvider {
param(
[string]$Server,
[System.Management.Automation.PSCredential]$Credential
)

try {
# If local machine, do not use credentials
if ($Server -eq "." -or $Server -eq $env:COMPUTERNAME) {
$Credential = $null
$Server = "."
}

# Connect to root\sms
$rootSmsParams = @{
ComputerName = $Server
Namespace = "root\sms"
ErrorAction = 'Stop'
}
if ($Credential) { $rootSmsParams.Credential = $Credential }

$smsRoot = Get-WmiObject @rootSmsParams

# Find provider location
$providerLocations = $smsRoot.InstancesOf("SMS_ProviderLocation")
$localProvider = $providerLocations | Where-Object ProviderForLocalSite
$siteNamespace = "root\sms\site_$($localProvider.SiteCode)"

$siteParams = @{
ComputerName = $localProvider.Machine
Namespace = $siteNamespace
ErrorAction = 'Stop'
}
if ($Credential) { $siteParams.Credential = $Credential }

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).
Get-WmiObject @siteParams

return $null
}
catch {
Write-Host "Couldn't connect: $($_.Exception.Message)"
return $null
}
}

## Example
function SNIPPETMETHODNAME {
param(
$Connection
)

# Insert snippet code here
Write-Host "Connected successfully. Insert your code here."
}
#endregion

```

```vbs
Dim connection
Expand Down Expand Up @@ -220,22 +303,26 @@ namespace ConfigurationManagerSnippets
## Compiling the Code

### Namespaces
System

Microsoft.ConfigurationManagement.ManagementProvider
System

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

### Assembly
adminui.wqlqueryengine

microsoft.configurationmanagement.managementprovider
adminui.wqlqueryengine

microsoft.configurationmanagement.managementprovider

> [!NOTE]
> The assemblies are in the \<Program Files>\Microsoft Endpoint Manager\AdminConsole\bin folder.
> The assemblies are in the \<Program Files>\Microsoft Endpoint Manager\AdminConsole\bin folder.

## Runtime Requirements
For more information, see [Configuration Manager Server Runtime Requirements](../../../develop/core/reqs/server-runtime-requirements.md).

For more information, see [Configuration Manager Server Runtime Requirements](../../../develop/core/reqs/server-runtime-requirements.md).

## Robust Programming
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)).

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)).