troubleshooting Question

Exception calling "Add" with "2" argument(s): "Key cannot be null.

Avatar of SingAbout Martin
SingAbout Martin asked on
ExchangeActive DirectoryPowershell
24 Comments6 Solutions1006 ViewsLast Modified:
Thanks to an expert from this site, I was able to get the following script working. What this script does is check if the IndexedProperty is present in both datasets ($DataA and $DataB), then returns data from both sets corresponding to this indexed property to an Export-CSV. Now, I would like to go a step further and replace the Import-CSV function at the beginning. This is because the imported data (DataA and DataB) are already set in another function in the script ($DataA is set with Get-ADUser and $DataB is set with Get-MailboxStatistics).

For the record, the script below works, but in the new version, the top 2 lines are removed and $DataA and $DataB are already set with Get-ADUser and Get-MailboxStatistics.
$DataA = Import-CSV D:\Import\DataA.csv
$DataB = Import-CSV D:\Import\DataB.csv

$properties = $DataA[0].PSObject.Properties.Name + $DataB[0].PSObject.Properties.Name

$IndexedProperty = 'distinguishedName'

$DataBIndex = @{}
for ($i = 0; $i -lt $DataB.Count; $i++) {
    $DataBIndex.Add($DataB[$i].$IndexedProperty, $i)

foreach ($itemA in $DataA) {
    if ($DataBIndex.Contains($itemA.$IndexedProperty)) {
        $itemB = $DataB[$DataBIndex.($itemA.$IndexedProperty)]

        foreach ($property in $itemB.PSObject.Properties) {
            $itemA | Add-Member $property.Name $property.Value
        $itemA | Select-Object $properties | Export-CSV D:\Export\Result.csv -Append -NoTypeInformation

I'm receiving the following error. When I run the $DataA and $DataB separately in PowerShell, I do get the output from each variable, so they are not empty.

ERROR: System.Management.Automation.MethodInvocationException: Exception calling "Add" with "2" argument(s): "Key cannot be null.
Parameter name: key" ---> System.ArgumentNullException: Key cannot be null.
Parameter name: key
   at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
   at CallSite.Target(Closure , CallSite , Object , Object , Object )
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
Chris Dent
PowerShell Developer
Join our community to see this answer!
Unlock 6 Answers and 24 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 6 Answers and 24 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros