Create RegistrySetting object with PowerShell

In the comment of the blog “Creating a SCCM 2012 Application” Adam asked the question if it could be expanded with adding Enhanched detection method. Because there isn’t still a good SDK I started to do a little research.I found a great post from Adam Meltzer (link).

Although it is C# it’s easy to translate it to PowerShell. But there I was wrong. I translated this C# command

RegistrySetting registrySetting = new RegistrySetting(null);

to

$registrySetting = New-Object Microsoft.ConfigurationManagement.DesiredConfigurationManagement.RegistrySetting($null)

But then I get the following error just as described in this post (link)
New-Object : Constructor not found. Cannot find an appropriate constructor for type Microsoft.ConfigurationManagement.DesiredConfigurationManagement.RegistrySetting.

After lots of troubleshooting I discoverd that this is an error in de DcomObject.dll. In C# this command works but in PowerShell it fails. I also discoverd a simple workaround. I created a custom dll that creates the object and function that can be called from powershell to get the empty RegistrySetting object.

The following command should be run to get an empty RegistyrSetting object

[System.Reflection.Assembly]::LoadFrom("$PATH\RegistrySetting.dll")
$temp = New-Object RegistrySettingNamespace.RegistrySettingRob ""
$registrysetting = $temp.GetRegistrySetting()
$registrysetting

Location : HKEY_LOCAL_MACHINE\
RootKey : LocalMachine
Key :
ValueName :
IsValueNameRegularExpression : False
Is64Bit : False
CreateMissingPath : True
SupportedMethods : {Value, Count}
IncludeDcmDigestAsNamespace : False
Annotation : Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Rules.Annotation
SettingDataType : Microsoft.SystemsManagementServer.DesiredConfigurationManagement.Expressions.ScalarDataType
Name :
SourceType : Registry
ParentConfigItem :
LogicalName : RegSetting_6186aeeb-1013-4e14-acb7-f4a72495dac1
Applicability :
SettingOptimization :
Description :
ApplicableAtUserLogon : False
IsDirtyExposed : False
SupportsRemediation : True

It is not the most beautiful solution but until this bug is solved its the only solution to work with the RegistrySetting object in PowerShell

********************************
Update
********************************
The previous version only worked with the DcomObject.dll found in SCCM 2012 SP1. So therefore I added also the .cs file and the steps to compile the custom dll. A prerequisite to compile is of course having a visual studio edition installed. The new zip file can be found below

RegistrySetting

6 thoughts on “Create RegistrySetting object with PowerShell”

  1. I’m attempting to use your DLL.

    I place it into C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin and then run [System.Reflection.Assembly]::LoadFrom(‘C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\RegistrySettingRob.dll’) | Out-Null

    When I do this, I receive this error message:

    Exception calling “LoadFrom” with “1” argument(s): “Could not load file or assembly ‘file:///C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\RegistrySettingRob.dll’ or one of its dependencies. Operation is not supported. (Exception
    from HRESULT: 0x80131515)”

    Am I doing something wrong?

  2. Hi,
    great post but i have two Questions?

    How can i compile the custom.dll without visual studio?
    And is this all running with sccm 2012 R2.

    greetz
    André

    1. Hi André,

      Currently I have not tested it on SCCM 2012 R2 maybe Microsoft resolved the issue.
      You can install the free visual studio express.

      Gr Rob

      1. Hi and thanks for your response,

        you are sure that MS fixed it? I can not find a solution to insert a Registrykey as DetectionMethode.

        I try to explain it, i will create and Application with Powershell and filled out all relevant dependencies, Files and Folders. Insert a Wrapper Installation EXE. But if i try to work with registrykey to make a proof i can do it. i don´t know how to do it.

        Is there more information about it?
        Thank you very well.

        André

  3. Hi ,

    If you replace “$null” with “@($null)” you can create a Registrysetting.
    I tested it on SCCM2012 SP1

    i am not sure where I found the advice with the Array Expression, somewhere on TechNet I think.

Comments are closed.