Adding values from function into new PSobject array

Welcome Forums General PowerShell Q&A Adding values from function into new PSobject array

Viewing 1 reply thread
  • Author
    Posts
    • #251732
      Participant
      Topics: 10
      Replies: 31
      Points: 82
      Rank: Member

      Hi folks. i’m working on pulling some data from Sharepoint on-prem, using Powershell and currently it outputs perfectly when i use “Write-Host”. But i need to add the values to an open array and order it so i can Export to CSV file. Can you tell me why the $Hashtable array is empty and does not store any of the filename and fileURL values? Write-Host shows them, but $hashtable is empty. Thank you

      • This topic was modified 4 weeks, 1 day ago by Mahin.
      • This topic was modified 4 weeks, 1 day ago by Mahin.
      • This topic was modified 4 weeks, 1 day ago by kvprasoon. Reason: code formatting
    • #251756
      Participant
      Topics: 0
      Replies: 77
      Points: 343
      Helping Hand
      Rank: Contributor

      The reason $hashTable contains no data after your function call is because of how scoping works. When a function is called, it runs in its own scope. Variables updated within a function are not reflected outside of the function. There are exceptions of course if you use scope modifiers. To achieve the results you want, you can just output custom objects from within your foreach loop. Then save your function output into a list, which will contain an Add() method for adding elements. In order for your output to be a single dimensional array, you will need to iterate your GetFiles() output with your current function design.

      • This reply was modified 4 weeks, 1 day ago by kvprasoon. Reason: Code formatting
Viewing 1 reply thread
  • You must be logged in to reply to this topic.