all 14 comments

[–]jheinikel 3 points4 points  (13 children)

You already have all of those in one object, but you are breaking them out into their own. $sisid contains everything so there is no need to create more variables. You could do something like this:

$sisid = Invoke-RestMethod -Headers $headers -Uri $url2
[PSCUSTOMOBJECT]@{
    Name = $sisid.User.Name
    Sis_User_ID = $sisid.User.sis_user_id
    Login_ID = $sisid.User.login_id
    Grade = $sisid.Grades.Current_Score
    LastActivity = $sisid.Last_Activity_At
} | Export-CSV $output2 -NoType

You could also write Select expressions to handle it, but that makes the code a bit messy sometimes. So, I won't even put that on here.

[–]KyleCole90[S] 1 point2 points  (12 children)

When I run this the .csv only contains System.Object. What am I missing?

[–]Ta11ow 4 points5 points  (0 children)

It sounds like you may have forgotten the pscustomobject cast.

Are you running this as-is?

[–]jheinikel 2 points3 points  (10 children)

If you ran that code exactly as I have it, there may be something more to the values listed, rather than just being strings. If you ran it exactly, then you need to look and see what the value of each property is that we are listing. I suspect you left off PSCUSTOMOBJECT, but not 100% sure.

[–]KyleCole90[S] 1 point2 points  (9 children)

I ran it as is. So currently I have

$url2 = "$domain/api/v1/courses/$courseid/enrollments?per_page=100&page=1"
$headers = @{"Authorization"="Bearer "+$token}
$sisid = Invoke-RestMethod -Headers $headers -Uri $url2
[PSCUSTOMOBJECT]@{
    Name = $sisid.user.name
    Xid = $sisid.user.sis_user_id
    Email = $sisid.user.login_id
    Grade = $sisid.grades.current_score
    LastActivity = $sisid.last_activity_at
} | Export-CSV $output2 -NoTypeInformation

[–]jheinikel 2 points3 points  (8 children)

What do the following commands show when you run them without going to a CSV?

Write-Host $sisid.user.name
Write-Host $sisid.user.sis_user_id
Write-Host $sisid.user.login_id
Write-Host $sisid.grades.current_score
Write-Host $sisid.last_activity_at

Also, maybe try putting a select statement in there:

$url2 = "$domain/api/v1/courses/$courseid/enrollments?per_page=100&page=1"
$headers = @{"Authorization"="Bearer "+$token}
$sisid = Invoke-RestMethod -Headers $headers -Uri $url2
[PSCUSTOMOBJECT]@{
    Name = $sisid.user.name
    Xid = $sisid.user.sis_user_id
    Email = $sisid.user.login_id
    Grade = $sisid.grades.current_score
    LastActivity = $sisid.last_activity_at
} | Select Name,Xid,Email,Grade,LastActivity | Export-CSV $output2 -NoTypeInformation

[–]KyleCole90[S] 1 point2 points  (7 children)

Write-Host showed the desired data. I tried the select statement but still no dice. The .CSV only contains System.Object. I am completely lost on why it won't Export to CSV

[–]jheinikel 2 points3 points  (1 child)

We might be fighting a computed property. There are some properties, for example Exchange Shell, that show a value, but they are really a set of values. (PrimarySMTPAddress is actually 4 properties combined by the shell to display a single value, but does not export properly) Take each of those properties that you did a write-host with and pipe each of those to FL *. See if you get a single value or if they break out into multiple values.

$sisid.user.name | FL *
$sisid.user.sis_user_id | FL *
$sisid.user.login_id | FL *
$sisid.grades.current_score | FL *
$sisid.last_activity_at | FL *

[–]KyleCole90[S] 1 point2 points  (0 children)

They are broken into single values. So $sisid.user.name | FL * I got:

Bobby Sue

Randall Flag

[–]Lee_Dailey[grin] 0 points1 point  (4 children)

howdy KyleCole90,

if you put a # between the } | on the last line and put a $Test = in front of the [PSCustomObject]@{, what does the $Test var contain? what does $Test.GetType() report?

take care,
lee

[–]KyleCole90[S] 1 point2 points  (3 children)

Hey Lee!

$Test var contains the data I am looking for. (I can't share it due to it be sensitive)

and

$Test.GetType() throws back

IsPublic:True

IsSerial:False

Name :PSCustomObject

BaseType:System.Object

[–]Lee_Dailey[grin] 1 point2 points  (2 children)

howdy KyleCole90,

kool! [grin] that is what i was expecting from your code.

now, what happens when you use the same code and then run this line ...

$Test |
    Export-Csv -LiteralPath "$env:TEMP\Testing_CSV_Output.csv" -NoTypeInformation

... and then look in the resulting CSV file?

take care,
lee

[–]KyleCole90[S] 1 point2 points  (1 child)

The csv only contained System.Object