PowerShellOrg / DSC

DSC Tools and Documentation
MIT License
299 stars 108 forks source link

Add-DscEncryptedPassword functionality #95

Closed BladeFireLight closed 9 years ago

BladeFireLight commented 9 years ago

I'm using the Development version. Is it possible to have different names for credentials besides the user-name. so I have a number of computers with different local admin accounts I need the keys under $ConfigurationData.Credentials to be different. however as it is, whatever I add as the username also becomes the key.

dlwyatt commented 9 years ago

You can use a key of 'ComputerName\UserName', and it should work.

BladeFireLight commented 9 years ago
$configData.Credentials.computername\username
At line:1 char:37
 $configData.Credentials.computername\username
                                     ~~~~~~~~~
Unexpected token '\username' in expression or statement.
     CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
     FullyQualifiedErrorId : UnexpectedToken

$configData.Credentials.'computername\username' works at the console but not in the configuration script. it just gives me this in the .mof

State = "Running";
 Credential = NULL;

That also does not work so well if I have multiple computers with the same local admin account.

say SiteA has 20 computers with one local admin password and SiteB has 10 computers with a different password. I cant use computername\username in that situation.

BladeFireLight commented 9 years ago

Well I figured out, this code works. But does not solve the above shared in small groups administrator account problem In the DSC_Configuration\Services file

    ServiceSettings = @{
        Service =@(
            @{
                Name = 'scsm'
                CredName = 'domain\logagent'
                DependsOn = '[file]LogRythemConfigFile'
                StartupType = 'Automatic'
                State = 'Running'
            }
        )
    }

In the Config called by Invoke-DscBuild

        #region ### Service Settings ###
        if (Test-DscConfigurationPropertyExists -Node $Node -PropertyName ServiceSettings)
        {
            $Services = @(
                Resolve-DscConfigurationProperty -Node $Node -PropertyName ServiceSettings\Service -MultipleResultBehavior AllValues
            )

            foreach ($Service in $Services)
            {
                if ($Service['BuiltInAccount']) {
                    Service $Service['Name']
                    {
                        Name           = $Service['Name']
                        BuiltInAccount = $Service['BuiltInAccount']
                        DependsOn      = $Service['DependsOn']
                        StartupType    = $Service['StartupType']
                        State          = $Service['State']
                    }
                }
                else {
                    Service $Service['Name']
                    {
                        Name           = $Service['Name']
                        Credential     = $ConfigurationData.Credentials."$($Service['CredName'])"
                        DependsOn      = $Service['DependsOn']
                        StartupType    = $Service['StartupType']
                        State          = $Service['State']
                    }
                }
            }
        } #endregion

dlwyatt commented 9 years ago

Well, in the tooling module, the key is the username of the PSCredential. However, there's nothing stopping you from just making up names to go in there, and replacing them at execution time with whatever's appropriate. For example, if we're talking about local admin accounts, then you could just strip out anything before the first backslash of the username, something along these lines:

ServiceSettings = @{
    Service =@(
        @{
            Name = 'scsm'
            CredName = 'SiteAGroup\Administrator'
            DependsOn = '[file]LogRythemConfigFile'
            StartupType = 'Automatic'
            State = 'Running'
        }
    )
}
            $cred = $ConfigurationData.Credentials."$($Service['CredName'])"
            $cred = New-Object pscredential(($cred.UserName -replace '^[^\\]*\\'), $cred.Password)

            Service $Service['Name']
            {
                Name           = $Service['Name']
                Credential     = $cred
                DependsOn      = $Service['DependsOn']
                StartupType    = $Service['StartupType']
                State          = $Service['State']
            }