Store variable value in MOF file from PowerShell DSC

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Craig Martin Craig Martin 1 year, 6 months ago.

  • Author
  • #24537
    Profile photo of Vinoth M
    Vinoth M

    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 [blockquote]$ServiceCred[/blockquote] 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
    Profile photo of Don Jones
    Don Jones

    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
    Profile photo of Craig Martin
    Craig Martin

    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.

You must be logged in to reply to this topic.