3

I have a hash table like this:

$Arguments = @{
    Name = $DataSourceName
    DriverName = "MS Access"
    SetPropertyValue = @(
         "Server=$ServerIpAddress",
         "Description=$Description"
         "CurrentDomain=$DomainName"
    )
    ErrorAction = 'Stop'
}

If I want to insert entries into the array SetPropertyValue. How can I achieve this? I tried:

$Arguments.SetPropertyValue.Add("Database=$DatabaseName")

But this does not work.

1
  • The array type does not contain an Add() method (see Microsoft Docs). Commented Oct 29, 2018 at 11:54

2 Answers 2

3

Try this:

$Arguments.SetPropertyValue += "Database=$DatabaseName"

This will append an item to the existing array. The += operator is a shorthand equivalent of doing:

$Arguments.SetPropertyValue = $Arguments.SetPropertyValue + "Database=$DatabsaseName"
Sign up to request clarification or add additional context in comments.

Comments

1

.Add() is a method associated with lists in PowerShell but not arrays. So one thing you could do it cast your initial SetPropertyValue as an array list. \

SetPropertyValue = [System.Collections.ArrayList] @(
     "Server=$ServerIpAddress",
     "Description=$Description",
     "CurrentDomain=$DomainName"
)

So with that in place your Add statement would succeed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.