1

I am trying to something pretty simple in powershell, I have instance of ProjectItem and I would like read the value of the FileNames property (documented as a string array). However its proving to be quite difficult

ForEach ($item in $scripts.ProjectItems) {
    Write-Host $item.FileNames
    Write-Host $item.FileNames[0]
    Write-Host $item.FileNames(0)
}

So my goal here is to move some files around a project. The output of these 3 Write-Host lines is

string FileNames (short) {get} 
string FileNames (short) {get} 
string FileNames (short) {get} 

Is there something special I have to do to read this as an array?

Sam
  • 1,725
  • 1
  • 17
  • 28

1 Answers1

4

When you evaluate a .NET method without calling it i.e. providing parens, PowerShell will emit the method's signature(s). Try:

Write-Host $item.get_FileNames(0)

Update: according to the OP the following does work:

Write-Host $item.FileNames(0)
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • Actually: Write-Host $item.FileNames(0) works. I must have missed it somehow in the debug output. It wont work with a ForEach loop though, but that's ok. I'll marks yours as answer as it does explain the "weird output" (the emitted signature) I get. – Sam Jan 17 '13 at 22:51
  • none of the 4 options (OP and answer) appears to be working for me =( `"The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))"` and `CategoryInfo : NotSpecified: (:) [], GetValueInvocationException` and also `FullyQualifiedErrorId : CatchFromBaseAdapterParameterizedPropertyGetValueTI` when using ` write-host $item.FileNames(0) "104 pass!"` – Maslow May 27 '14 at 21:03
  • well, the Write-Host `$item.FileNames(0)` is working. The types I was passing to it must have changed I guess. – Maslow May 28 '14 at 15:20