PSDesiredStateConfiguration icon indicating copy to clipboard operation
PSDesiredStateConfiguration copied to clipboard

Invoke-DscResource parameters type conversion

Open ricardogaspar2 opened this issue 8 years ago • 2 comments

Steps to reproduce

When calling Invoke-DscResource and passing string arguments that are supposed to be converted to integers (Enumerator) values the command doesn't do it. Only when using a DSC powershell configuration script.

The error is discussed in another issue in Ansible, because, at first, one thought the problem was inside win_dsc Ansible module. Apparently is not. https://github.com/ansible/ansible/issues/25754#issuecomment-309287318

Full thread with all the steps (including DSC Script and PowerShell commands) https://github.com/ansible/ansible/issues/25754

But to shorten it a bit: This DSC Script works fine using the cAdministrativeTemplateSetting from the PolicyFileEditor module (https://github.com/dlwyatt/PolicyFileEditor).

  1. Install the PolicyFileEditor in PowerShell of the target machine:
PS C:\Users\user>Find-DSCResource -ModuleName PolicyFileEditor
PS C:\Users\user> Install-Module PolicyFileEditor
  1. Run a simple DSC Script on the target Windows machine to prove that the resource works fine:
Configuration LocalGPO
{
    param
    (
        [string[]] $NodeName = 'localhost'
    )

    Import-DSCResource -ModuleName PolicyFileEditor

    Node $NodeName
    {
        cAdministrativeTemplateSetting "RDP Users Home Directory Path"
        {
        #    SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services!WFHomeDirUNC 
        #    SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services!WFHomeDir
        #    SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services!WFHomeDirDrive
            KeyValueName = "SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services\WFHomeDir"
            PolicyType = "Machine"
            Data = "\\servershare\test"
            Ensure = "Present"
            Type = "String"
        }

        cAdministrativeTemplateSetting "RDP Users Home Directory Letter"
        {
            KeyValueName = "SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services\WFHomeDirDrive"
            PolicyType = "Machine"
            Data = "X:"
            Ensure = "Present"
            Type = "String"
        }

        cAdministrativeTemplateSetting "RDP Users Home Directory UNC boolean"
        {
            KeyValueName = "SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services\WFHomeDirUNC"
            PolicyType = "Machine"
            Data = "1"
            Ensure = "Present"
            Type = "Dword"
        }

    }
}

LocalGPO
Start-DscConfiguration -Path .\LocalGPO -Wait -Force -Verbose
  1. On the target machine, run the command gpupdate to update the Group Policy. Then Check the registry keys in HKLM:SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services\. There should be a DWORD key with the name WFHomeDirUNC and value 1.

What DOES NOT work:

  1. check the cAdministrativeTemplateSetting resource (from https://github.com/ansible/ansible/issues/25754#issuecomment-309286332):
PS C:\Users\trond\Documents> Get-DscResource cAdministrativeTemplateSetting | select -ExpandProperty Properties

Name                 PropertyType   IsMandatory Values
----                 ------------   ----------- ------
KeyValueName         [string]              True {}
PolicyType           [string]              True {Administrators, Machine, NonAdministrators, User}
Data                 [string[]]           False {}
DependsOn            [string[]]           False {}
Ensure               [string]             False {Absent, Present}
PsDscRunAsCredential [PSCredential]       False {}
Type                 [Int32]              False {Binary, DWord, ExpandString, MultiString...}
  1. Test using Invoke-DscResource (essential for ANSIBLE win_dsc module to call DSC resources). From https://github.com/ansible/ansible/issues/25754#issuecomment-309287318.
$DscParams = @{}
$DscParams.Add("Ensure", "Present")
$DscParams.Add("KeyValueName", "SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services\WFHomeDirUNC")
$DscParams.Add("PolicyType", "Machine")
$DscParams.Add("Data", 1)
$DscParams.Add("Type", "Dword")
Invoke-DscResource -Name cAdministrativeTemplateSetting -ModuleName "PolicyFileEditor" -Method Get -Property $dscparams
Invoke-DscResource -Name cAdministrativeTemplateSetting -ModuleName "PolicyFileEditor" -Method Set -Property $dscparams

The errors:

Invoke-DscResource -Name cAdministrativeTemplateSetting -ModuleName "PolicyFileEditor" -Method Get -Property $dscparams
Convert property 'Type' value from type 'STRING' to type 'SINT32' failed
 At line:12, char:2
 Buffer:
irectResourceAccess";
};^
insta
    + CategoryInfo          : SyntaxError: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : MiClientApiError_Failed
    + PSComputerName        : localhost
 

PS C:\Windows\system32> Invoke-DscResource -Name cAdministrativeTemplateSetting -ModuleName "PolicyFileEditor" -Method Set -Property $dscparams
Convert property 'Type' value from type 'STRING' to type 'SINT32' failed
 At line:12, char:2
 Buffer:
irectResourceAccess";
};^
insta
    + CategoryInfo          : SyntaxError: (root/Microsoft/...gurationManager:String) [], CimException
    + FullyQualifiedErrorId : MiClientApiError_Failed
    + PSComputerName        : localhost

Expected behavior

The Invoke-DscResource should work like calling a DSC resource with parameters in a DSC configuration script. Using string arguments that inside the resource are actually integers.

Actual behavior

It's not able to convert a string (which is supposedly accepted by the resource in the examples: cAdministrativeTemplateSetting from PolicyFileEditor module) to an integer. The resource contains a map to do this "conversion"/substitution of the keys (string values) for integers (enumerators).

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.14409.1005
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14409.1005
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

ricardogaspar2 avatar Jun 19 '17 10:06 ricardogaspar2

cc @nitinbps

SteveL-MSFT avatar Dec 05 '18 20:12 SteveL-MSFT

@StevenBucher98 / @anmenaga can this be closed or moved to the PSDesiredStateConfiguration

ThomasNieto avatar Jul 31 '23 21:07 ThomasNieto