Showing posts with label sdk. Show all posts
Showing posts with label sdk. Show all posts

Thursday, April 25, 2024

Connect Scom powershell using SDK

 The SDK binaries are stored in the following folder on the SCOM MS

C:\Program Files\Microsoft System Center\Operations Manager\Server\SDK Binaries

copy the three dll files to your script folder.

Sample script.

##########################################################################

$rms= "RMS.contoso.com"
$UserName = "domain\username"
$pwd = "password"

$scriptdir = "C:\Scripts"
[System.Reflection.Assembly]::LoadFrom("$scriptdir\Microsoft.EnterpriseManagement.OperationsManager.dll")
[System.Reflection.Assembly]::LoadFrom("$scriptdir\Microsoft.EnterpriseManagement.Core.dll")
[System.Reflection.Assembly]::LoadFrom("$scriptdir\Microsoft.EnterpriseManagement.Runtime.dll")
 $securePassword = ConvertTo-SecureString $pwd –AsPlainText -Force
$MGConnSetting = New-Object Microsoft.EnterpriseManagement.ManagementGroupConnectionSettings($rms)
$MGConnSetting.UserName = $UserName
$MGConnSetting.Domain = $UserDomain
$MGConnSetting.Password = $SecurePassword
$MG = New-Object Microsoft.EnterpriseManagement.ManagementGroup($MGConnSetting)
if(!$MG) {Write-Host "Cannot work on $rms" -ForegroundColor Yellow;continue;}
#######################
Write-host "The Management group is " -nonewline; Write-Host "$MG" -ForegroundColor Yellow

$MG.Administration.GetAllAgentManagedComputers()

###################################################################################

 If you don't use Microsoft.EnterpriseManagement.Runtime.dll binary you will get this error.

New-Object : Exception calling ".ctor" with "1" argument(s): "The service type 'Microsoft.EnterpriseManagement.Runtime.ITaskRuntimeService, Microsoft.EnterpriseManagement.Runtime, Culture="", PublicKeyToken=31bf3856ad364e35,
Version=7.0.5000.0' for the component named 'TaskService' cannot be found."
At line:1 char:1
+ New-Object Microsoft.EnterpriseManagement.ManagementGroup($MGConnSett ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand