Store variable value in MOF file from PowerShell DSC

Welcome Forums DSC (Desired State Configuration) Store variable value in MOF file from PowerShell DSC

This topic contains 2 replies, has 3 voices, and was last updated by

 
Member
3 years, 10 months ago.

  • Author
    Posts
  • #24537

    Participant
    Points: 0
    Rank: Member

    I want to update Anonymous authentication user and password from PowerShell DSC. I wrote below script resource to update that. I have issue where to store the Service account user name and password. I am passing

    $ServiceCred

    variable as argument to the configuration.

    Script UpdateServiceUserAnonymous
      {  
        GetScript = {return $null
                }
                SetScript = { 
                 $PasswordPointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR[$ServiceCred.Password]
                 $getPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto[$PasswordPointer] 
                  start-sleep -Seconds 5
                  Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location 'Website'`
                -filter "system.webServer/security/authentication/anonymousAuthentication" -name "userName" -value $ServiceCred.UserName
                  Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location 'Website'`
                 -filter "system.webServer/security/authentication/anonymousAuthentication" -name "password" -value $getPassword
                          }
                TestScript = {
                $PasswordPointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR[$ServiceCred.Password]
                $getPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto[$PasswordPointer]  
                Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location 'Website' -filter "system.webServer/security/authentication/anonymousAuthentication" -Name "userName" | %{$_.Value} | Set-Variable userName
                Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location 'Martini' -filter "system.webServer/security/authentication/anonymousAuthentication" -Name "password" | %{$_.Value} | Set-Variable password     
                if[$userName -eq $ServiceCred.UserName -and $password -eq $getPassword] {$True}
                else {$false}
                }
       } 

    I understand that this configuration argument value will be available only in compilation time(creation of MOF file) and not in run time (applying MOF file). Is there any way to store and refer this values from MOF file to script resource?
    Could you please help me to solve this issue?

    Thanks in advance.

  • #24538

    Keymaster
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    MOF files aren't scripts; they have no logic, and they're not "run" like a script. Everything in a MOF is, and must be, a static value. There isn't a way for them to refer to something else, if that's what you're after.

  • #24556

    Member
    Points: 0
    Rank: Member

    For your example it is probably worth writing a custom resource, which has the ability to use credentials.

    The Script resource is a quick way to get functionality of a custom resource without actually creating a custom resource, but you've just hit one of the limitations.

The topic ‘Store variable value in MOF file from PowerShell DSC’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort