1

I am trying to list some files in a tree to work on them

$files_AppConfig = Get-ChildItem -Path .\App.config -Recurse
$files_WatcherConfig = Get-ChildItem -Path .\WatcherServiceConfig.xml -Recurse
$files_StrControles = Get-ChildItem -Path .\Web.config -Recurse
$files = $files_AppConfig + $files_WatcherConfig + $files_StrControles


ForEach($file in $files)
{   
#Do some stuff
}

But when called by visual studio in prebuild event, i have Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'

1 Answer 1

1

+ only does array addition if the first operand ($files_AppConfig in this case) is already an array. If Get-ChildItem -Path .\App.config -Recurse only returns 1 file, that won't be the case.

You can either force the first operand to be an array by wrapping it in the array subexpression operator @():

$files = @($files_AppConfig) + $files_WatcherConfig + $files_StrControles

Or, my personal preference, wrap the array subexpression operator around multiple value statements to create one big flat array:

$files = @($files_AppConfig;$files_WatcherConfig;$files_StrControles)

If you know for certain that the variables contain only scalars, you can also use the , array operator:

$files = $files_AppConfig,$files_WatcherConfig,$files_StrControles
Sign up to request clarification or add additional context in comments.

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.