An item with the same key has

Tagged: 

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Bjørn Roalkvam Bjørn Roalkvam 1 month ago.

  • Author
    Posts
  • #56360
    Profile photo of Bjørn Roalkvam
    Bjørn Roalkvam
    Participant

    Hi

    Using Server 2012R2 and PowerShell 5.

    I have created a custom DSC resource (class 5) for running the SharePoint configuration Wizzard (psconfig).

    When trying to use the resource i get:

    VERBOSE: [vm04]: LCM:  [ Start  Resource ]  [[PSConfig]psconfig]
    VERBOSE: [vm04]: LCM:  [ Start  Test     ]  [[PSConfig]psconfig]
    An item with the same key has already been added.
        + CategoryInfo          : NotSpecified: (:) [], CimException
        + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.AddPSSnapinCommand
        + PSComputerName        : localhost
    
    VERBOSE: [vm04]: LCM:  [ End    Test     ]  [[PSConfig]psconfig]  in 1.1100 seconds.
    The PowerShell DSC resource '[PSConfig]psconfig' with SourceInfo '\\share\SharePoint\RoleDataSource\MasterServer.ps1::305::9::Psconfig' threw one or more non-terminating err
    ors while running the Test functionality. These errors are logged to the ETW channel called Microsoft-Windows-DSC/Operational. Refer to this channel for more details.
        + CategoryInfo          : InvalidOperation: (:) [], CimException
        + FullyQualifiedErrorId : NonTerminatingErrorFromProvider
        + PSComputerName        : localhost
    
    VERBOSE: [vm04]:                            [] Consistency check completed.
    The SendConfigurationApply function did not succeed. LCM failed to start desired state configuration manually.
        + CategoryInfo          : NotSpecified: (root/Microsoft/...gurationManager:String) [], CimException
        + FullyQualifiedErrorId : MI RESULT 1
        + PSComputerName        : localhost 

    What i do in the test method is 1):
    Adding the microsoft.sharepoint.powershell snappin:

    If  ((Get-PSSnapin -Name microsoft.sharepoint.powershell -EA "SilentlyContinue") -eq $null)
    {
        Add-PsSnapin microsoft.sharepoint.powershell
    } 

    and 2) running a command to check if servers need to upgrade.

    Anyone experienced this kind of fault?

    brgs

    bjørn

  • #56426
    Profile photo of Arie H
    Arie H
    Participant

    Dont know if this is it, but it sounds like you're trying to add modules to the session and then use it, and I dont think that's how DSC works. You need the modules to exist when the session initializes or you wont be able to call any of the functions inside the modules.

  • #56438
    Profile photo of Bjørn Roalkvam
    Bjørn Roalkvam
    Participant

    The reason for adding the sharepoint powershell snapin is that i have a method in the DSCresource that checks if the SharePoint servers need to upgrade.

    I have made other modules for sql-operations, where i import-sqlps module without issues though...

    I read somewhere that maybe i need to register the sharepoint snapin into my dsc-module manifest..Looking into that.

    brgs

    Bjørn

  • #56569
    Profile photo of Bjørn Roalkvam
    Bjørn Roalkvam
    Participant

    Ill post the dsc resource class:

    [DscResource()]
    class PSConfig
    {
        [DscProperty(key)][Ensure]$Ensure
    
        [void]RunPSConfig()
        {
    
                Write-verbose -message "Running PSconfig.exe on $env:COMPUTERNAME. "; Write-verbose -message "Expect 4-10 minutes per Host."
                CD 'C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\BIN'
                $psconfiginfo = .\Psconfig.exe -cmd upgrade -inplace b2b -force -cmd applicationcontent -install -cmd installfeatures
    
                #Check to see if the 6 steps of the psconfig run have run successfully
                If($psconfiginfo -like "*Total number of successful configuration settings: 6*")
                {
                    Write-verbose -message "The 6 configuration steps have successfully completed [  OK  ]"
                }
                Else
                {
                    Write-verbose -message "Error detected, a configuration step has failed, please review the output bellow. Try restarting affecting server and run this script again." 
                    Write-verbose -message $psconfiginfo
                    Break
                }
        }
    
    
        [bool] GetUpgradeStatus ()
        {      
            Add-PSSnapin microsoft.sharepoint.powershell 
            $Status = (get-spserver $env:computername).NeedsUpgrade 
            Return $Status
    
            If ((Get-PsSnapin |?{$_.Name -eq "Microsoft.SharePoint.PowerShell"})-ne $null)
            {
                Write-Verbose "Removing the PowerShell snapin"
                Remove-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null
            } 
        }
         
        [void] Set()
        {
            if ($this.Ensure -eq [Ensure]::Present)
            {
                Write-Verbose -Message "[Running PSConfig]"
                $this.RunPSConfig()
            }
            elseif ($this.Ensure -eq [Ensure]::Absent)
            {
                Write-Verbose -Message "[Absent setting has no logical property:-)]"
            }
        }
    
    
        [bool] Test()
        {
    
            $ServerNeedsUpgrade = $this.GetUpgradeStatus()
            $there = $false
            If($ServerNeedsUpgrade -eq $true){$there = $true}
            If($There -eq $true){Write-Verbose -Message "[Server requires PSconfig to be run]"}
            If($There -eq $false){Write-Verbose -Message "[Server does not require PSconfig to be run]"}
    
            if ($this.Ensure -eq [Ensure]::Present)
            {
                return $There
            }
            else
            {
                return -not $There
            }
        }
    
    
        [PSConfig] Get()
        {
    
            if($this.GetUpgradeStatus() -eq $true)
            {
                $this.Ensure = [Ensure]::Present
            }
            else
            {
                $this.Ensure = [Ensure]::Absent
            }
    
            return $this 
        }
    } 
    
  • #56599
    Profile photo of JrdnRgrs
    JrdnRgrs
    Participant

    From the Microsoft article https://msdn.microsoft.com/en-us/powershell/dsc/authoringresourceclass

    DscProperty(Key): The property is required. The property is a key. The values of all properties marked as keys must combine to uniquely identify a resource instance within a configuration.

    Pretty sure you shouldn't be using the Key attribute on the Ensure property, since there are only 2 values for this and other resources will be using it.

    Put your Key attribute on another property

  • #56863
    Profile photo of Bjørn Roalkvam
    Bjørn Roalkvam
    Participant

    Hi JrdnRgrs,

    I did what you suggested. Changed the key to another parameter:

    [DscProperty(mandatory)][Ensure]$Ensure
    [DscProperty(key)][string]$Configname

    However, i get the same fault.

    brgs

    Bjørn

  • #56867
    Profile photo of Bjørn Roalkvam
    Bjørn Roalkvam
    Participant

You must be logged in to reply to this topic.