diff --git a/intune/configmgr/develop/core/understand/calling-code-snippets.md b/intune/configmgr/develop/core/understand/calling-code-snippets.md index 35511cdc5c..fb267f8017 100644 --- a/intune/configmgr/develop/core/understand/calling-code-snippets.md +++ b/intune/configmgr/develop/core/understand/calling-code-snippets.md @@ -6,14 +6,100 @@ 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 SNIPPETMETHODNAME 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). + +## 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' + Class = "SMS_ProviderLocation" + } + if ($Credential) { $rootSmsParams.Credential = $Credential } + + $smsRoot = Get-WmiObject @rootSmsParams - 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). + # Find provider location + $providerLocations = $smsRoot.InstancesOf("SMS_ProviderLocation") + $localProvider = $providerLocations | Where-Object ProviderForLocalSite | Select-Object -First 1 + if (-not $localProvider) { + throw "No SMS_ProviderLocation found for the local site." + } + $siteNamespace = "root\sms\site_$($localProvider.SiteCode)" + + $siteParams = @{ + ComputerName = $localProvider.Machine + Namespace = $siteNamespace + Class = 'SMS_Site' + ErrorAction = 'Stop' + } + if ($Credential) { $siteParams.Credential = $Credential } + + $siteInfo = Get-WmiObject @siteParams + return $siteInfo + } + 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 @@ -101,7 +187,6 @@ Function Connect(server, userName, userPassword) Next Set Connect = null ' Failed to connect. End Function - ``` ```c# @@ -220,22 +305,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)).