Manual for plugin “ConfigMgr”¶
In this article:
General assumptions:
- Actions with
Workflow
prefix combine multiple actions. Workflow actions exist to standardize frequent used action combos. - Every action returns
Base.Result
. For more info go to Base.Result article. - Most of the functions accept parameters via a specific parameter class
Connection¶
In the Connection namespace all connection relevant parameters are stored.
Authentication Methods (or integrated authentication)
- Windows Auth: Do not set the corresponding credentials attributes
UserName
,UserPassword
.
Example:
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'MyConfigMgrHost'
$ConfigMgrConnectionSettings.SiteCode = 'P01'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName
- Clear text: Set the corresponding credentials attributes
UserName
,UserPassword
.
Example:
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'MyConfigMgrHost'
$ConfigMgrConnectionSettings.SiteCode = 'P01'
$ConfigMgrConnectionSettings.WMIUserDomainName = 'MyDomain'
$ConfigMgrConnectionSettings.WMIUserName = 'administrator'
$ConfigMgrConnectionSettings.WMIUserPassword = 'Password123'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnectionSettings.SQLUserName = "administrator";
$ConfigMgrConnectionSettings.SQLUserPassword = "Password123";
- Powershell Secure String: Will be supported in a later version.
Examples¶
Create collection, assign app, create membership¶
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'SIM'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$ApplicationName = "Test App 1"
$CollectionName = "Test Collection 1"
$ComputerName = "TestComputer1"
$ConfigMgrComputersObject = New-Object SIM.ConfigMgr.Computers.Computer($ConfigMgrConnection)
$objParameterCollection = New-Object SIM.ConfigMgr.ParameterCollection;
$objParameterCollection.CollectionName = $CollectionName;
$objParameterCollection.CollectionType = [SIM.ConfigMgr.ParameterCollectionTypes]::Device
$objParameterCollection.Folder = New-Object SIM.ConfigMgr.ParameterFolder(16777217);
#Check if collection exists
[Base.Result] $resFindColletion = $ConfigMgrComputersObject.GetWMIPredefinedClassProperty([SIM.ConfigMgr.SuperClass+ConfigMgrQueries]::CollectionId_By_CollectionName,$objParameterCollection.CollectionName)
if ($resFindColletion.ExitCode.Code -eq [Base.ResultSuperClass+ExitCodeType]::ElementFound)
{
$objParameterCollection.CollectionId = $resFindColletion.ReturnObj;
}
else
{
#If not exists, create collection
$res.ChildAdd($ConfigMgrComputersObject.CollectionCreate($objParameterCollection))
if ($res.Successful -eq $true)
{
#Create assignment to application, if application does not exist, the action will fail with corresponding message
$objParameterAssignment = New-Object SIM.ConfigMgr.ParameterAssignment;
$objParameterAssignment.Collection = $objParameterCollection;
$objParameterAssignment.Application = New-Object SIM.ConfigMgr.ParameterApplication($ApplicationName);
$objParameterAssignment.AssignmentType = [SIM.ConfigMgr.ParameterAssignment+AssignmentTypes]::Install
$objParameterAssignment.OfferType = [SIM.ConfigMgr.ParameterAssignment+OfferTypes]::Optional
$res.ChildAdd($ConfigMgrComputersObject.AssignmentCreate($objParameterAssignment))
}
}
#If everthing before was executed without any errors, a collection membership is created
if ($res.Successful -eq $true)
{
$objParametersCollectionMembership = New-Object SIM.ConfigMgr.ParametersCollectionMembership;
$objParametersCollectionMembership.CollectionMembershipType = [SIM.ConfigMgr.ParametersCollectionMembership+CollectionMembershipTypes]::RuleDirectComputer;
$objParametersCollectionMembership.Collection = $objParameterCollection
$objParametersCollectionMembership.ResourceName = $ComputerName;
$res.ChildAdd($ConfigMgrComputersObject.CollectionMembershipAdd($objParametersCollectionMembership));
$res.ChildAdd($ConfigMgrComputersObject.CollectionMembershipRequestRefresh($objParametersCollectionMembership,10,20000,20000));
}
}
$res.Dump()
Example “reseting” existing computer¶
This example shows how to
- Check whether a computer is found by Hostname
- Delete every direct collection membership
- Reset cmputer variables
- Add new variables
- Reset PXE flags
- Add to specific collection
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'SIM'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$ConfigMgrComputersSettings = New-Object SIM.ConfigMgr.Computers.ComputerParameters
#Define Target Computer by Name
$ConfigMgrComputersSettings.ComputerName = 'TestComputer1'
$res.ChildAdd($ConfigMgrComputersSettings.TryResolve($ConfigMgrConnection))
$ResourceId = $ConfigMgrComputersSettings.ResourceId
If ($ResourceId)
{
# REMOVE FROM COLLECTIONS
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $ConfigMgrConnection.SQLConnection
$Command.CommandText = "SELECT [CollectionID] FROM [v_CollectionRuleDirect] WHERE [ResourceID] = '$ResourceId' AND [ResourceType] = 5"
$DataAdapter = new-object System.Data.SqlClient.SqlDataAdapter $Command
$Dataset = new-object System.Data.Dataset
$DataAdapter.Fill($Dataset)
$QueryResultCount = $Dataset.Tables[0].Rows.ToString()
if ($QueryResultCount -gt 0)
{
Foreach ($Row in $Dataset.Tables[0])
{
$ConfigMgrComputersObject = New-Object SIM.ConfigMgr.Computers.Computer($ConfigMgrConnection)
$objParametersCollectionMembership = New-Object SIM.ConfigMgr.ParametersCollectionMembership;
$objParametersCollectionMembership.CollectionMembershipType = [SIM.ConfigMgr.ParametersCollectionMembership+CollectionMembershipTypes]::RuleDirectComputer;
$objParametersCollectionMembership.CollectionId = $Row["CollectionID"]
$objParametersCollectionMembership.ResourceId = $ResourceId
$res.ChildAdd($ConfigMgrComputersObject.CollectionMembershipRemove($objParametersCollectionMembership));
}
}
# ADD RES VARS
$res.ChildAdd($ConfigMgrComputersObject.DeleteVariables($ConfigMgrComputersSettings))
$MyVar1 = New-Object SIM.ConfigMgr.Parameter("MyVar1", "Value1")
$MyVar2 = New-Object SIM.ConfigMgr.Parameter("MyVar2", "Value2")
$ConfigMgrComputersSettings.Variables.Add($MyVar1)
$ConfigMgrComputersSettings.Variables.Add($MyVar2)
$res.ChildAdd($ConfigMgrComputersObject.AddResourceVariables($ConfigMgrComputersSettings))
# ClearPxeAdvertisementResource
$res.ChildAdd($ConfigMgrComputersObject.ClearPxeAdvertisementResource($ConfigMgrComputersSettings))
# ADD TO COLL
$objParametersCollectionMembership = New-Object SIM.ConfigMgr.ParametersCollectionMembership;
$objParametersCollectionMembership.CollectionMembershipType = [SIM.ConfigMgr.ParametersCollectionMembership+CollectionMembershipTypes]::RuleDirectComputer;
$objParametersCollectionMembership.CollectionName = "DeployColl";
$objParametersCollectionMembership.ResourceName = $ConfigMgrComputersSettings.ComputerName
$res.ChildAdd($ConfigMgrComputersObject.CollectionMembershipAdd($objParametersCollectionMembership));
}
}
$res.Dump()
Generic¶
GetWMIPredefinedClassProperty¶
Get for predefined WMI classes specific properties. Thise queries can either be used to lookup data or to determine of the object exists.
Definition:
public Base.Result GetWMIPredefinedClassProperty(ConfigMgrQueries Query, string FindValue, string FindValue2 = "")
- Get data
Example:
- Check if object exists.
Example:
Computers¶
Everthing concering computer management is stored in the Computers
namespace.
AddResourceVariables¶
Adds variables to a specific computer system.
Example:
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'SIM'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$ConfigMgrComputersObject = New-Object SIM.ConfigMgr.Computers.Computer($ConfigMgrConnection)
$ConfigMgrComputersSettings = New-Object SIM.ConfigMgr.Computers.ComputerParameters
#Define Target Computer by ResourceId
$ConfigMgrComputersSettings.ResourceId = '16777221'
#Define Target Computer by ComputerName
$ConfigMgrComputersSettings.ComputerName = 'TestComputer478'
$MyVar1 = New-Object SIM.ConfigMgr.Parameter("MyVar1", "Value1")
$MyVar2 = New-Object SIM.ConfigMgr.Parameter("MyVar2", "Value2")
$ConfigMgrComputersSettings.Variables.Add($MyVar1)
$ConfigMgrComputersSettings.Variables.Add($MyVar2)
$res.ChildAdd($ConfigMgrComputersObject.AddResourceVariables($ConfigMgrComputersSettings))
}
$res.Dump()
AdvertisementCreate¶
Creates an package advertisement for a specific collection
Example:
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Database.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'P01'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$ConfigMgrComputersObject = New-Object SIM.ConfigMgr.Computers.Computer($ConfigMgrConnection)
$objParameterCollection = New-Object SIM.ConfigMgr.ParameterCollection;
$objParameterCollection.CollectionName = "TestCollection";
$objParameterCollection.CollectionType = [SIM.ConfigMgr.ParameterCollectionTypes]::Device
$objParameterAdvertisement = New-Object SIM.ConfigMgr.ParameterAdvertisement;
$objParameterAdvertisement.Collection = $objParameterCollection;
$objParameterAdvertisement.Package = New-Object SIM.ConfigMgr.ParameterPackage("P0100050");
$objParameterAdvertisement.Package.ProgramName = "Install"
$objParameterAdvertisement.OfferType = [SIM.ConfigMgr.ParameterAssignment+OfferTypes]::Optional
$res.ChildAdd($ConfigMgrComputersObject.AdvertisementCreate($objParameterAdvertisement))
}
$res.Dump()
AssignmentCreate¶
Creates an application assignment for users or computers
Example:
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'SIM'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$ConfigMgrComputersObject = New-Object SIM.ConfigMgr.Computers.Computer($ConfigMgrConnection)
$objParameterCollection = New-Object SIM.ConfigMgr.ParameterCollection;
$objParameterCollection.CollectionName = "Test Collection 1";
$objParameterCollection.CollectionType = [SIM.ConfigMgr.ParameterCollectionTypes]::Device
$objParameterAssignment = New-Object SIM.ConfigMgr.ParameterAssignment;
$objParameterAssignment.Collection = $objParameterCollection;
$objParameterAssignment.Application = New-Object SIM.ConfigMgr.ParameterApplication("Test App 1");
$objParameterAssignment.AssignmentType = [SIM.ConfigMgr.ParameterAssignment+AssignmentTypes]::Install
$objParameterAssignment.OfferType = [SIM.ConfigMgr.ParameterAssignment+OfferTypes]::Optional
$res.ChildAdd($ConfigMgrComputersObject.AssignmentCreate($objParameterAssignment))
}
$res.Dump()
ClearPxeAdvertisementResource¶
Clears the PXE advertisements for a computer.
Example:
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'SIM'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$ConfigMgrComputersObject = New-Object SIM.ConfigMgr.Computers.Computer($ConfigMgrConnection)
$ConfigMgrComputersSettings = New-Object SIM.ConfigMgr.Computers.ComputerParameters
$ConfigMgrComputersSettings.ResourceId = "16777221"
$res.ChildAdd($ConfigMgrComputersObject.ClearPxeAdvertisementResource($ConfigMgrComputersSettings))
}
$res.Dump()
CollectionCreate¶
Create a collection.
Example:
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'SIM'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$ConfigMgrComputersObject = New-Object SIM.ConfigMgr.Computers.Computer($ConfigMgrConnection)
$objParameterCollection = New-Object SIM.ConfigMgr.ParameterCollection;
$objParameterCollection.CollectionName = "Test Collection 1";
$objParameterCollection.CollectionType = [SIM.ConfigMgr.ParameterCollectionTypes]::Device
$objParameterCollection.Folder = New-Object SIM.ConfigMgr.ParameterFolder(16777217); #If FolderId is defined, the collection will be moved on creation
$objParameterCollection.LimitingCollectionId = "SMS00001";
$res.ChildAdd($ConfigMgrComputersObject.CollectionCreate($objParameterCollection))
}
$res.Dump()
CollectionMembershipAdd¶
Currently the following methods are supported by this action:
- RuleDirectUser
- RuleDirectComputer
- RuleInclude
Example for RuleDirectComputer:
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'SIM'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$ConfigMgrComputersObject = New-Object SIM.ConfigMgr.Computers.Computer($ConfigMgrConnection)
$objParametersCollectionMembership = New-Object SIM.ConfigMgr.ParametersCollectionMembership;
$objParametersCollectionMembership.CollectionMembershipType = [SIM.ConfigMgr.ParametersCollectionMembership+CollectionMembershipTypes]::RuleDirectComputer;
$objParametersCollectionMembership.CollectionName = "ParentColl";
$objParametersCollectionMembership.ResourceName = "TestComputer478";
$res.ChildAdd($ConfigMgrComputersObject.CollectionMembershipAdd($objParametersCollectionMembership));
}
$res.Dump()
Example for RuleInclude:
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'SIM'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$ConfigMgrComputersObject = New-Object SIM.ConfigMgr.Computers.Computer($ConfigMgrConnection)
$objParametersCollectionMembership = New-Object SIM.ConfigMgr.ParametersCollectionMembership;
$objParametersCollectionMembership.CollectionMembershipType = [SIM.ConfigMgr.ParametersCollectionMembership+CollectionMembershipTypes]::RuleInclude;
$objParametersCollectionMembership.CollectionName = "ParentColl";
$objParametersCollectionMembership.ResourceName = "ChildColl";
$res.ChildAdd($ConfigMgrComputersObject.CollectionMembershipAdd($objParametersCollectionMembership));
}
$res.Dump()
CollectionMembershipRequestRefresh¶
Requests a refresh of the effective collection memberships. The request will be performed after the corresponding resource was found in the limiting collection.
Definition:
Base.Result CollectionMembershipRequestRefresh(ParametersCollectionMembership objParams, int RetriesMax = 45, int RetryWaitMilisec = 20000, int SleepAfterFound = 60000)
Example:
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'SIM'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$ConfigMgrComputersObject = New-Object SIM.ConfigMgr.Computers.Computer($ConfigMgrConnection)
$objParametersCollectionMembership = New-Object SIM.ConfigMgr.ParametersCollectionMembership;
$objParametersCollectionMembership.CollectionMembershipType = [SIM.ConfigMgr.ParametersCollectionMembership+CollectionMembershipTypes]::RuleDirectComputer;
$objParametersCollectionMembership.CollectionName = "Test Collection"
$objParametersCollectionMembership.ResourceName = "Testcomputer1"
$res.ChildAdd($ConfigMgrComputersObject.CollectionMembershipRequestRefresh($objParametersCollectionMembership,10,20000,20000));
}
$res.Dump()
CollectionMembershipRemove¶
Currently the following methods are supported by this action:
- RuleDirectUser
- RuleDirectComputer
Example for RuleDirectComputer:
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'SIM'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$ConfigMgrComputersObject = New-Object SIM.ConfigMgr.Computers.Computer($ConfigMgrConnection)
$objParametersCollectionMembership = New-Object SIM.ConfigMgr.ParametersCollectionMembership;
$objParametersCollectionMembership.CollectionMembershipType = [SIM.ConfigMgr.ParametersCollectionMembership+CollectionMembershipTypes]::RuleDirectComputer;
$objParametersCollectionMembership.CollectionId = "SIM00017"
$objParametersCollectionMembership.ResourceId = "16777228"
$res.ChildAdd($ConfigMgrComputersObject.CollectionMembershipRemove($objParametersCollectionMembership));
}
$res.Dump()
ComputerExists¶
Checks wether a computer exists (by Name, MAC or SMBIOSGUID).
Found computers can be deleted directly via secondary function parameter DeleteSystem
.
Example:
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'SIM'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$ConfigMgrComputersObject = New-Object SIM.ConfigMgr.Computers.Computer($ConfigMgrConnection)
$ConfigMgrComputersSettings = New-Object SIM.ConfigMgr.Computers.ComputerParameters
$ConfigMgrComputersSettings.ComputerName = "SIMTestPC"
$ConfigMgrComputersSettings.MACAddress = "00-00-00-00-00-12"
$DeleteSystem = $true
[Base.Result]$existsResult = $ConfigMgrComputersObject.ComputerExists($ConfigMgrComputersSettings,$DeleteSystem)
[string]$ResourceId = $existsResult.ReturnObj
$res.ChildAdd($existsResult)
if ($existsResult.ExitCode.Code -eq 'ElementFound')
{
Write-Host "Computer was found and has following ResourceId: $ResourceId"
}
}
$res.Dump()
Create¶
Creates a computer.
Example:
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'SIM'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$ConfigMgrComputersObject = New-Object SIM.ConfigMgr.Computers.Computer($ConfigMgrConnection)
$ConfigMgrComputersSettings = New-Object SIM.ConfigMgr.Computers.ComputerParameters
$ConfigMgrComputersSettings.ComputerName = "SIMTestPC"
$ConfigMgrComputersSettings.MACAddress = "00-00-00-00-00-11"
$resCreate = $ConfigMgrComputersObject.Create($ConfigMgrComputersSettings)
[string]$ResourceId = $resCreate.ReturnObj
$res.ChildAdd($resCreate)
if ($res.Successful -eq $true -And $ResourceId -ne $null)
{
"Computer was created with ResourceId: $ResourceId"
}
}
$res.Dump()
Delete¶
Deletes a computer. Can only be deleted with ResourceId.
If deleting via Computername, MAC or SMBIOSGUID is needed, use ComputerExists
function.
Example:
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'SIM'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$ConfigMgrComputersObject = New-Object SIM.ConfigMgr.Computers.Computer($ConfigMgrConnection)
$ConfigMgrComputersSettings = New-Object SIM.ConfigMgr.Computers.ComputerParameters
$ConfigMgrComputersSettings.ResourceId = "16777225"
$res.ChildAdd($ConfigMgrComputersObject.Delete($ConfigMgrComputersSettings))
}
$res.Dump()
DeleteVariables¶
Deletes the variables on the corresponding system.
Example:
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'SIM'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$ConfigMgrComputersObject = New-Object SIM.ConfigMgr.Computers.Computer($ConfigMgrConnection)
$ConfigMgrComputersSettings = New-Object SIM.ConfigMgr.Computers.ComputerParameters
$ConfigMgrComputersSettings.ResourceId = "16777221"
$res.ChildAdd($ConfigMgrComputersObject.DeleteVariables($ConfigMgrComputersSettings))
}
$res.Dump()
PrimaryUserAdd¶
Add a user to a computer as a PrimaryUser reference.
Example:
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'SIM'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$ConfigMgrComputersObject = New-Object SIM.ConfigMgr.Computers.Computer($ConfigMgrConnection)
$ConfigMgrComputersSettings = New-Object SIM.ConfigMgr.Computers.ComputerParameters
#Define Target Computer by ResourceId
$ConfigMgrComputersSettings.ResourceId = '16777222'
$UserParameters = New-Object SIM.ConfigMgr.Users.UserParameters($ConfigMgrConnection)
$UserParameters.ResourceId = "2063597568"
$ParameterPrimaryUser = New-Object SIM.ConfigMgr.ParameterPrimaryUser($ConfigMgrComputersSettings, $UserParameters)
$res.ChildAdd($ConfigMgrComputersObject.PrimaryUserAdd($ParameterPrimaryUser))
}
$res.Dump()
WorkflowCreate¶
This worklow combines the following actions:
- Check if computer exists (Delete if
ComputerParameters.ComputerOverwriteExistingObject
is true) - Create computer
- Add variables to computer object
- Add collection memberships to computer
Example (complete with connection):
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'MyConfigMgrHost'
$ConfigMgrConnectionSettings.SiteCode = 'P01'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$ConfigMgrComputersSettings = New-Object SIM.ConfigMgr.Computers.ComputerParameters
$ConfigMgrComputersSettings.ComputerName = "TestComputer"
$ConfigMgrComputersSettings.MACAddress = "E4-F8-9C-5D-DE-39"
$ConfigMgrComputersSettings.ComputerOverwriteExistingObject = $true
$val = New-Object SIM.ConfigMgr.Parameter("Var1", "Val1")
$ConfigMgrComputersSettings.Variables.Add($val)
$ParametersCollectionMembership = New-Object SIM.ConfigMgr.ParametersCollectionMembership
$ParametersCollectionMembership.IsComputerMembership = $true
$ParametersCollectionMembership.CollectionName = "Windows Server 2012 Deploy"
$ParametersCollectionMembership.CreateCollectionIfNotExist = $false
$ConfigMgrComputersSettings.Collections.Add($ParametersCollectionMembership);
$ConfigMgrComputersWorkflowCreate = New-Object SIM.ConfigMgr.Computers.Computer($ConfigMgrConnection)
$ResCreate = $ConfigMgrComputersWorkflowCreate.WorkflowCreate($ConfigMgrComputersSettings)
$ResCreate.Dump()
Expected output:
Loaded assembly: ConfigMgr, Version=6.1.0.6, Culture=neutral, PublicKeyToken=null
+ Ok | Starting Workflow for creating computer... (ComputerOverwriteExistingObject: True) | 11/18/2016 9:19:10 AM
++ Ok | Connecting to ConfigMgr Site with connection settings: Hostname: "192.168.42.193" SiteCode: "P01" UserName: "administrator" UserDomainName: "SCCM12". | 11/18/2016 9:19:10 AM
+++ Ok | Connected to scope (Path: \\192.168.42.193\root\SMS\site_P01). | 11/18/2016 9:19:11 AM
+++ Ok | Connecting to ConfigMgr SQL database "Data Source= 192.168.42.193; Initial Catalog= CM_P01;" | 11/18/2016 9:19:11 AM
++ Ok | Validating Computers.Settings... | 11/18/2016 9:19:11 AM
+++ Ok | Valiation ok! | 11/18/2016 9:19:11 AM
++ ElementFound | Checking if computer already exists (1: SMBIOSGUID: "" / 2. MacAddress: "E4:F8:9C:5D:DE:39" / 3. NetBiosname: TestComputer4")... | 11/18/2016 9:19:11 AM
+++ ElementFound | Executing "SELECT System_MAC_Addres_ARR.ItemKey FROM System_MAC_Addres_ARR JOIN v_R_System ON v_R_System.ResourceId = System_MAC_Addres_ARR.ItemKey WHERE System_MAC_Addres_ARR.[MAC_Addresses0] = 'E4:F8:9C:5D:DE:39' ORDER BY v_R_System.Creation_Date0 DESC" | 11/18/2016 9:19:11 AM
+++ Ok | Computer was found (ResourceId: "16777736"). | 11/18/2016 9:19:11 AM
++ Ok | Deleting computer with ResourceID "16777736"... | 11/18/2016 9:19:11 AM
++ Ok | Creating computer... (NetbiosName = TestComputer4, MACAddress = E4:F8:9C:5D:DE:39, SMBIOSGUID = ) | 11/18/2016 9:19:11 AM
+++ Ok | Computer was created with ResourceId: '16777737' | 11/18/2016 9:19:11 AM
++ Ok | Adding variables to resouce with ResourceID "16777737"... (Variable count: 1) | 11/18/2016 9:19:11 AM
+++ Ok | Adding "Var1" (Value: "Val1222")... | 11/18/2016 9:19:11 AM
+++ Ok | Done! | 11/18/2016 9:19:12 AM
++ Ok | Starting AddCollectionMembership...(CollectionId: "P01000A2", CollectionName: "Windows Server 2012 Deploy", ResourceName: "TestComputer4", ResourceId: "16777737", CreateCollectionIfNotExist: "False", IsComputerMembership: "True") | 11/18/2016 9:19:12 AM
+++ Ok | Validating CollectionMembershipParameters... | 11/18/2016 9:19:12 AM
+++ Ok | Done! | 11/18/2016 9:19:12 AM
Applications¶
Everthing concering application management is stored in the Application
namespace.
CreateApplication¶
Creates an Application with a XML definition provided.
Example (complete with connection):
Security¶
Everthing concering computer management is stored in the Computers
namespace.
SecurityScopeAdd¶
Adds a existing security scope (by name) to a ConfigMgr object.
Example (complete with connection):
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Database.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'P01'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$SecurityScopeManagement = New-Object SIM.ConfigMgr.Security.SecurityScopeManagement($ConfigMgrConnection);
$securityScopeList = New-Object SIM.ConfigMgr.Security.SecurityScopeList
$securityScopeList.SecurityScopes.Add( (New-Object SIM.ConfigMgr.Security.SecurityScope ("","GERMANY Scope","") ));
$res.ChildAdd( ($SecurityScopeManagement.SecurityScopeAdd($securityScopeList, "P0100052", [SIM.ConfigMgr.ObjectTypeID]::SMS_Package) ));
}
$res.Dump()
Possible values for ObjectTypeId:
public enum ObjectTypeID
{
SMS_Package = 2,
SMS_OperatingSystemInstallPackage = 14,
SMS_ImagePackage = 18,
SMS_BootImagePackage = 19,
SMS_DriverPackage = 23,
SMS_SoftwareUpdatesPackage = 24,
SMS_Application = 31
}
SecurityScopeRemove¶
Removes a security scope (by name) from a ConfigMgr object.
Example (complete with connection):
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Base.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\Database.dll")
$Assembly = [Reflection.Assembly]::LoadFile("$PSScriptRoot\ConfigMgr.dll")
$ConfigMgrConnectionSettings = New-Object SIM.ConfigMgr.ConnectionSettings
$ConfigMgrConnectionSettings.WMIHostName = 'localhost'
$ConfigMgrConnectionSettings.SiteCode = 'P01'
$ConfigMgrConnectionSettings.SQLHostname = $ConfigMgrConnectionSettings.WMIHostName;
$ConfigMgrConnection = New-Object SIM.ConfigMgr.Connection($ConfigMgrConnectionSettings)
$res = New-Object Base.Result("Starting ConfigMgr script...")
$res.ChildAdd($ConfigMgrConnection.Connect())
if ($res.Successful -eq $true)
{
$SecurityScopeManagement = New-Object SIM.ConfigMgr.Security.SecurityScopeManagement($ConfigMgrConnection);
$securityScope = New-Object SIM.ConfigMgr.Security.SecurityScope ("","GERMANY Scope","")
$res.ChildAdd( ($SecurityScopeManagement.SecurityScopeRemove($securityScope, "P0100052", [SIM.ConfigMgr.ObjectTypeID]::SMS_Package) ));
}
$res.Dump()