Setting PSRepository policy in DSC script resource

Tagged: 

This topic contains 1 reply, has 2 voices, and was last updated by  Don Jones 1 year, 10 months ago.

  • Author
    Posts
  • #30870

    Vish Uma
    Participant

    I am trying to use the following script resource to set the policy for the PSGallery repository before installing from it. If, i just return $false from the TestScript, the SetScript seems to called. But if i use the following to Test, then the SetScript is not called. Any ideas why?

    return (Get-PSRepository | Where-Object{$_.Name -eq "PSGallery" -and $_.InstallationPolicy -eq "Trusted"}).Name -eq "PSGallery"

    The above does return false when i run it manually on the machine.

    The weird part is that, even if the SetScript is called by returning $false in SetScript, if i run Get-PSRepository after the fact on the remote machine, the PSGallary repository is listed as Untrusted. But, the Install-Module that is run after this resource successfully installs the xWebAdministration module

    Script SetDSCNuGetPolicy
    {
    GetScript = {@{}}
    #TestScript = {return (Get-PSRepository | Where-Object{$_.Name -eq "PSGallery" -and $_.InstallationPolicy -eq "Trusted"}).Name -eq "PSGallery"}
    TestScript = {return $false}
    SetScript = {
    Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
    }
    DependsOn = "[Package]InstallPackageManagement"
    }
    Script InstallxWebAdministrationDSC
    {
    GetScript = {@{}}
    TestScript = {return (Get-InstalledModule | Where-Object { $_.Repository -eq "PSGallery" -and $_.Name -eq "xWebAdministration"}).Name -eq "xWebAdministration"}
    SetScript = {
    Install-Module -Name xWebAdministration
    }
    DependsOn = "[Script]InstallPackageManagementNugetProvider"
    }

    Output from the script run on remote machine

    VERBOSE: [WIN-1QQKT2HBN5T]: LCM: [ Start Resource ] [[Script]SetDSCNuGetPolicy]
    VERBOSE: [WIN-1QQKT2HBN5T]: LCM: [ Start Test ] [[Script]SetDSCNuGetPolicy]
    VERBOSE: [WIN-1QQKT2HBN5T]: LCM: [ End Test ] [[Script]SetDSCNuGetPolicy] in 0.015
    0 seconds.
    VERBOSE: [WIN-1QQKT2HBN5T]: LCM: [ Start Set ] [[Script]SetDSCNuGetPolicy]
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Performin
    g the operation "Set-TargetResource" on target "Executing the SetScript with the user suppli
    ed credential".
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Loading m
    odule from path 'C:\Program Files\WindowsPowerShell\Modules\PackageManagement\Microsoft.Powe
    rShell.PackageManagement.dll'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Loading m
    odule from path 'C:\Program Files\WindowsPowerShell\Modules\PackageManagement\PackageManagem
    ent.psd1'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Loading '
    FormatsToProcess' from path 'C:\Program Files\WindowsPowerShell\Modules\PackageManagement\Pa
    ckageManagement.format.ps1xml'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Loading m
    odule from path 'C:\Program Files\WindowsPowerShell\Modules\PackageManagement\Microsoft.Powe
    rShell.PackageManagement.dll'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Exporting
    cmdlet 'Find-Package'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Exporting
    cmdlet 'Get-Package'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Exporting
    cmdlet 'Get-PackageProvider'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Exporting
    cmdlet 'Get-PackageSource'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Exporting
    cmdlet 'Install-Package'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Exporting
    cmdlet 'Register-PackageSource'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Exporting
    cmdlet 'Save-Package'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Exporting
    cmdlet 'Set-PackageSource'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Exporting
    cmdlet 'Uninstall-Package'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Exporting
    cmdlet 'Unregister-PackageSource'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Importing
    cmdlet 'Find-Package'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Importing
    cmdlet 'Get-Package'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Importing
    cmdlet 'Get-PackageProvider'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Importing
    cmdlet 'Get-PackageSource'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Importing
    cmdlet 'Install-Package'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Importing
    cmdlet 'Register-PackageSource'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Importing
    cmdlet 'Save-Package'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Importing
    cmdlet 'Set-PackageSource'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Importing
    cmdlet 'Uninstall-Package'.
    VERBOSE: [WIN-1QQKT2HBN5T]: [[Script]SetDSCNuGetPolicy] Importing
    cmdlet 'Unregister-PackageSource'.
    VERBOSE: [WIN-1QQKT2HBN5T]: LCM: [ End Set ] [[Script]SetDSCNuGetPolicy] in 19.86
    00 seconds.
    VERBOSE: [WIN-1QQKT2HBN5T]: LCM: [ End Resource ] [[Script]SetDSCNuGetPolicy]
    VERBOSE: [WIN-1QQKT2HBN5T]: LCM: [ Start Resource ] [[Script]InstallxWebAdministrationDSC
    ]
    VERBOSE: [WIN-1QQKT2HBN5T]: LCM: [ Start Test ] [[Script]InstallxWebAdministrationDSC
    ]
    VERBOSE: [WIN-1QQKT2HBN5T]: LCM: [ End Test ] [[Script]InstallxWebAdministrationDSC
    ] in 0.4530 seconds.
    VERBOSE: [WIN-1QQKT2HBN5T]: LCM: [ Skip Set ] [[Script]InstallxWebAdministrationDSC
    ]
    VERBOSE: [WIN-1QQKT2HBN5T]: LCM: [ End Resource ] [[Script]InstallxWebAdministrationDSC
    ]

  • #30883

    Don Jones
    Keymaster

    This might come down to the logic you're using.

    (Get-PSRepository | Where-Object{$_.Name -eq "PSGallery" -and $_.InstallationPolicy -eq "Trusted"}).Name -eq "PSGallery"

    I wouldn't except that to always be either true or false; if the Where filter outputs nothing, then it has no Name property, so comparing that to anything would technically be undefined. You're relying on PowerShell's willingness to fudge the results, which isn't reliable.

    I'd probably do...

    if ((Get-PSRepository | Where-Object{$_.Name -eq "PSGallery" -and $_.InstallationPolicy -eq "Trusted"} | Measure-Object).Count -gt 0) {}

    In other words, count the instances that you want, and see if the result is 0 or not. That way, you're actually counting what's there. If you rely on zero being interpreted as $false, and nonzero as $true, then that's safe, because that's how .NET uses those values anyway.

You must be logged in to reply to this topic.