Mark Weir
Mark Weir

Reputation: 359

How can I change a System.Array into a System.Object?

I have a variable stored as a System.Array displayed as;

Site        : https://value.sharepoint.com/
Email       : [email protected]
DisplayName : value
UniqueId    : value
AcceptedAs  : [email protected]
WhenCreated : 24/01/2019 06:02:45
InvitedBy   : value_value.co.uk#ext#@value.onmicrosoft.com

When I try to export this variable as a file it shows in the same format. As this is not in the correct structure for a table (shown below) I am unable to use this data when I try to use it in Power BI.

Site                                               Email                ect                                                                                                                  
----                                               ----                 ----                                                                                                                 
https://value.sharepoint.com/                      [email protected]    ect

I need to get my data into the structure shown above. I have tried;

$Test = New-Object -TypeName PSObject -Property  $ExternalUsers

However this results in the following error;

New-Object : Cannot convert 'System.Object[]' to the type 'System.Collections.IDictionary' required by parameter 'Property'. Specified method is not supported.

I then tried to loop through all of the items in the array and then create an object for each item, before adding it to a "Master Object";

foreach($var in $ExternalUsers){
    $Test = New-Object -TypeName PSObject -Property  $ExternalUsers
    $Test | Add-Member -MemberType NoteProperty -Name Site -Value $var.Site
    $Test | Add-Member -MemberType NoteProperty -Name Email -Value $var.Email
    $TestObject += $Test
}

This got each item into the correct structure but when I tried to add all the items back into the one variable I got the error;

Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

Any ideas how I could get around this?

Upvotes: 0

Views: 4756

Answers (2)

Theo
Theo

Reputation: 61168

To me it looks like you have an array (System.Object[]) containing PSObjects with the properties Site, Email etc. A structure like that is ideal for exporting to CSV file, which you can then import in a spreadsheed application like Excel for instance.

For that you use the cmdlet Export-Csv like this:

$ExternalUsers | Export-Csv -Path 'PATH AND FILENAME FOR THE OUTPUT CSV FILE' -NoTypeInformation 

If the output you show is complete, it seems there is only one element in the array. You can check this by looking at $ExternalUsers.Count.

I'm not quite sure what you mean by "As this is not in the correct structure for a table", because you can quite easily display it as table using

$ExternalUsers | Format-Table -AutoSize

Output on console window:

Site                          Email             DisplayName UniqueId AcceptedAs        WhenCreated         InvitedBy                                   
----                          -----             ----------- -------- ----------        -----------         ---------                                   
https://value.sharepoint.com/ [email protected] value       value    [email protected] 24/01/2019 06:02:45 value_value.co.uk#ext#@value.onmicrosoft.com

If what you want is less properties, just loop through the array and select the properties you want to keep from the objects in it:

$Shortened = $ExternalUsers | ForEach-Object {
    $_ | Select-Object Site, Email
}

$Shortened | Format-Table -AutoSize

Will produce:

Site                          Email            
----                          -----            
https://value.sharepoint.com/ [email protected]

I'm not into Power BI, but remember that the Format-Table cmdlet is for display purposes on console ONLY.
It does NOT provide anything else but a view on the data.

Hope this helps

Upvotes: 1

CloudTech
CloudTech

Reputation: 11

use:

$TestObject = @()

and no need to specify (-Property $ExternalUsers)

$Test = New-Object -TypeName PSObject 

Upvotes: 0

Related Questions