Name Export via Powershell not working as expected

ntr2def
ntr2def used Ask the Experts™
on
I am trying to dump application data from Azure into their own file, however, when i reference the DisplayName attribute for the application to save as the file name I get a weird naming convention. The code below imports the csv with the DisplayName and Object ID. Attached is sample file as well

import-csv .\AzureApplications.csv | ForEach {Get-AzureADApplication -objectID $_.ObjectID |export-csv .\$_.DisplayName.csv}

Open in new window

The output file name is as follows:
@{DisplayName=Test Application; ObjectId=10f3cd35-bf7e-468d-bdbb-298fa456b3c5; AppId=440ba5b1-0372-4b62-9379-edf7096c8cb5}.DisplayName.csv
Any help would be appreciated
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018
Commented:
The problem is that you're using "$_.Var" inside a string and is not being evaluating, So you're getting the whole Object

"@{Object}".Displayname.csv

Your code:
Import-csv .\AzureApplications.csv | ForEach {Get-AzureADApplication -objectID $_.ObjectID |export-csv .\$_.DisplayName.csv}

Open in new window


My code, you do need to evaluate "$_.property" before sending it into the "string"
Import-csv .\AzureApplications.csv | ForEach {Get-AzureADApplication -objectID $_.ObjectID |export-csv .\$($_.DisplayName).csv}

Open in new window


And you do this like :  with $()  $_.DisplayName  ... so $($_.DisplayName)

Basically if you have an object X, defined as  $X=@{A="valueA";B="valueB"} (2 properties A and B).
if you're gonna get the value of the property A, if it's a string all alone:  The name of a file for example, would be
 $X.A  + ".csv"

Open in new window

(like in C# variable + " string")

if you want to do it "inline", then you use $( expression ), to evaluate 'expression':
"$($X.A).csv"

Open in new window


In this case the line is evaluated 1st the value inside the brakets ... ($X.A). and the result is dumped into the string
"{exp}.csv"
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Jose is correct. But as here, it is not obvious that a parameter is a string, because PowerShell tries to do its best in "helping" you to use parameters and types:
If the filename expression starts with a bracket or dollar, it would first get evaluated as expression and return a string.
In other cases the "expression" is seen as a string, with inline string evaluation only - that is what you see.
In fact. it is much more complicated ;-). But in general you can assume that file name parameters are (implicit) strings. Get-Content, Import-*, Export-* are examples for that behaviour.

Author

Commented:
Thank you Jose for the re-education. It has been over a year since I have done programmatic administration and as you were explaining my immediate reaction was "Duh!"
Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018

Commented:
Yeah? haha, so much fun! :)
Read the @Qlemo comment and as well as @odba I respect them much, especially on the Powershell area, they're gods of it :)
And I'm glad to help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial