From bbe986dfc6e1bb5408c733da416979d24d1232d2 Mon Sep 17 00:00:00 2001 From: Baard Hermansen Date: Mon, 23 Feb 2026 11:32:54 +0100 Subject: [PATCH] Update calling-code-snippets.md Reformatted to follow MS style guidelines. Added PowerShell example code. --- .../core/understand/calling-code-snippets.md | 109 ++++++++++++++++-- 1 file changed, 98 insertions(+), 11 deletions(-) diff --git a/intune/configmgr/develop/core/understand/calling-code-snippets.md b/intune/configmgr/develop/core/understand/calling-code-snippets.md index 35511cdc5ca..087b40d3ddb 100644 --- a/intune/configmgr/develop/core/understand/calling-code-snippets.md +++ b/intune/configmgr/develop/core/understand/calling-code-snippets.md @@ -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 @@ -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 \\Microsoft Endpoint Manager\AdminConsole\bin folder. +> The assemblies are in the \\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)).