# An item with the same key has

**URL:** https://forums.powershell.org/t/an-item-with-the-same-key-has/7428
**Category:** DSC
**Created:** [October 28, 2016, 5:48am UTC](https://forums.powershell.org/t/an-item-with-the-same-key-has/7428 "2016-10-28T05:48:21Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![bjorn-roalkvam](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bjorn-roalkvam/32/13_2.png) [@bjorn-roalkvam](https://forums.powershell.org/u/bjorn-roalkvam)
#### Post date: [October 28, 2016, 5:48am UTC](https://forums.powershell.org/t/an-item-with-the-same-key-has/7428/1 "2016-10-28T05:48:21Z")

</div>

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

---

<div class="post-metadata">

### Author: ![silent-steel](https://avatars.discourse-cdn.com/v4/letter/s/e5b9ba/32.png) [@silent-steel](https://forums.powershell.org/u/silent-steel)
#### Post date: [October 28, 2016, 2:57pm UTC](https://forums.powershell.org/t/an-item-with-the-same-key-has/7428/2 "2016-10-28T14:57:42Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![bjorn-roalkvam](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bjorn-roalkvam/32/13_2.png) [@bjorn-roalkvam](https://forums.powershell.org/u/bjorn-roalkvam)
#### Post date: [October 29, 2016, 8:44am UTC](https://forums.powershell.org/t/an-item-with-the-same-key-has/7428/3 "2016-10-29T08:44:39Z")

</div>

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

---

<div class="post-metadata">

### Author: ![bjorn-roalkvam](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bjorn-roalkvam/32/13_2.png) [@bjorn-roalkvam](https://forums.powershell.org/u/bjorn-roalkvam)
#### Post date: [November 1, 2016, 4:57am UTC](https://forums.powershell.org/t/an-item-with-the-same-key-has/7428/4 "2016-11-01T04:57:29Z")

</div>

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 
    }
}
```

---

<div class="post-metadata">

### Author: ![jrdnrgrs](https://avatars.discourse-cdn.com/v4/letter/j/7ab992/32.png) [@jrdnrgrs](https://forums.powershell.org/u/jrdnrgrs)
#### Post date: [November 1, 2016, 10:30am UTC](https://forums.powershell.org/t/an-item-with-the-same-key-has/7428/5 "2016-11-01T10:30:16Z")

</div>

From the Microsoft article [https://msdn.microsoft.com/en-us/powershell/dsc/authoringresourceclass](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

---

<div class="post-metadata">

### Author: ![bjorn-roalkvam](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bjorn-roalkvam/32/13_2.png) [@bjorn-roalkvam](https://forums.powershell.org/u/bjorn-roalkvam)
#### Post date: [November 4, 2016, 6:17am UTC](https://forums.powershell.org/t/an-item-with-the-same-key-has/7428/6 "2016-11-04T06:17:28Z")

</div>

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

---

<div class="post-metadata">

### Author: ![bjorn-roalkvam](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bjorn-roalkvam/32/13_2.png) [@bjorn-roalkvam](https://forums.powershell.org/u/bjorn-roalkvam)
#### Post date: [November 4, 2016, 6:28am UTC](https://forums.powershell.org/t/an-item-with-the-same-key-has/7428/7 "2016-11-04T06:28:01Z")

</div>

I’m wondering if it could be connected to loading the sharepoint snapin:  
[http://stackoverflow.com/questions/17710233/add-powershell-snapin-for-powershell-module-and-import-multiple-times](http://stackoverflow.com/questions/17710233/add-powershell-snapin-for-powershell-module-and-import-multiple-times)

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 9:16pm UTC](https://forums.powershell.org/t/an-item-with-the-same-key-has/7428/8 "2024-05-16T21:16:51Z")

</div>


