How to return and parse multiple properties of an object?
I have the following script that fetches reports from a URI and displays the datasource(s) username. However, we'd like to get the name of the reports so we know what datasource username is associated with what report for tracking purposes.
listReports()
function parses each report and returns its ID return $_.Id
The report ID is a hash like
12b1657c-dc07-5b35-afc0-b0d6c9047860
The $_
object also has other meaningful properties, such as $_.Name
, and $_.Path
Current output of full script (from the last foreach loop that is) is a list of Datasource usernames (not the report IDs):
TKCSIMPORT
payroll
si_51932_p_00h
payroll
payroll
tkcsimport
which is what we want, but i also want to display the report name/paths associated with each of those usernames, e.g. expected output:
DS Username Report Name
TKCSIMPORT Report1
payroll Report2
si_51932_p_00h Report3
payroll Report3
payroll Report4
tkcsimport Report5
How do I do this?
I would try return $_.Id, $_.Name, $_.Path
in listReports()
but then the challenge is how to map the extra properties here @(listReports("http://pbi.domain.gm.com/reports"))
Full script:
# lists all the reports on the server that are available to your user account
Function listReports($baseURL) {
$reports = Invoke-RestMethod -UseDefaultCredentials -uri "$baseURL/api/v2.0/CatalogItems"
$reports.value | Where-Object {$_.Type -eq "PowerBIReport"} | foreach {
#Write-Host ("{0} {1} {2}" -f $_.Id, $_.Name, $_.Path)
return $_.Id
}
}
Function getDataSources($baseURL, $reportID) {
$sources = Invoke-RestMethod -UseDefaultCredentials -uri "$baseURL/api/v2.0/PowerBIReports($reportID)/DataSources"
if ($sources.value -is [array]) {
return $sources.value
} else {
return @($sources.value)
}
}
$reportIDs = @(listReports("http://pbi.domain.com/reports"))
foreach($reportID in $reportIDs) {
(getDataSources "http://pbi.domain.com/reports" $reportID | % {
return $_.DataModelDataSource.Username
})
}
Warning. This is an illustrative example. It isn't solution for your specific code
[System.Environment]::GetEnvironmentVariables()
returns a HashTable, .GetEnumerator()
return an IEnumerator that iterates through a collection. Each element of Enumerator have two properties Key and Value. In this example I select Key, Value and the third property is calculated an named as Size.
The statement @{ Name="Size"; Expression = {$_.Value.Length}}
is the third custom selected property.
Name="Size"
is the name of the calculated property
Expression = {$_.Value.Length}
is the value of the calculated property. In this case is the Length of the Value property.
{$_.Value.Length}
is an script block that returns a value.
$_
is the value of the current object on the pipe.
The Code
# //Selecting all objects
$enumerator = [System.Environment]::GetEnvironmentVariables().GetEnumerator() | `
Select-Object -Property Key, Value, @{ Name="Size"; Expression = {$_.Value.ToString().ToUpper() }}
# //Alternative solution with PS Custom Objects. Thanks to https://stackoverflow.com/users/8397835/cataster
$enumerator2 = [System.Environment]::GetEnvironmentVariables().GetEnumerator() | Foreach-Object{
[PSCustomObject]@{
Key = $_.Key
Value = $_.Value
Size = $_.Value.Length
}
}
# //Printing all selected objects
$enumerator | ForEach-Object {
Write-Host "{ Key= $($_.Key), Value= $($_.Value), Size= $($_.Size) }"
}
# //Print all selected objects as list, all properties
$enumerator | Format-List
# //Print all selected objects as table, specific properties
$enumerator | Format-Table -Property Key, Size
Output Illustrative example
{ Key= TEMP, Value= C:\Users\Megam\AppData\Local\Temp, Size= 33 }
{ Key= PSModulePath, Value= C:\Users\Megam\Documents\PowerShell\Modules;C:\Program Files\PowerShell\Modules;c:\program files\powershell\7\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules;c:\Users\Megam\.vscode\extensions\ms-vscode.powershell-2021.12.0\modules, Size= 284 }
{ Key= HOMEDRIVE, Value= C:, Size= 2 }
{ Key= APPDATA, Value= C:\Users\Megam\AppData\Roaming, Size= 30 }
{ Key= LANG, Value= en_US.UTF-8, Size= 11 }
{ Key= PATHEXT, Value= .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL, Size= 58 }
{ Key= CommonProgramFiles(x86), Value= C:\Program Files (x86)\Common Files, Size= 35 }
{ Key= ProgramData, Value= C:\ProgramData, Size= 14 }
{ Key= PROCESSOR_ARCHITECTURE, Value= AMD64, Size= 5 }
{ Key= VSCODE_GIT_ASKPASS_EXTRA_ARGS, Value= --ms-enable-electron-run-as-node, Size= 32 }
{ Key= TERM_PROGRAM_VERSION, Value= 1.63.2, Size= 6 }
{ Key= ProgramFiles(x86), Value= C:\Program Files (x86), Size= 22 }
{ Key= SystemDrive, Value= C:, Size= 2 }
{ Key= USERDOMAIN, Value= DESKTOP-9EPNRSP, Size= 15 }
{ Key= COLORTERM, Value= truecolor, Size= 9 }
{ Key= ProgramW6432, Value= C:\Program Files, Size= 16 }
{ Key= HOMEPATH, Value= \Users\Megam, Size= 12 }
{ Key= TERM_PROGRAM, Value= vscode, Size= 6 }
{ Key= CommonProgramW6432, Value= C:\Program Files\Common Files, Size= 29 }
{ Key= windir, Value= C:\Windows, Size= 10 }
as List
Key : CHROME_CRASHPAD_PIPE_NAME
Value : \\.\pipe\crashpad_9536_MITAYLIDBVJOLRAM
Size : 39
Key : PUBLIC
Value : C:\Users\Public
Size : 15
Key : COLORTERM
Value : truecolor
Size : 9
Key : APPDATA
Value : C:\Users\Megam\AppData\Roaming
Size : 30
Key : ComSpec
Value : C:\Windows\system32\cmd.exe
Size : 27
Key : NUMBER_OF_PROCESSORS
Value : 8
Size : 1
Key : PSModulePath
Value : C:\Users\Megam\Documents\PowerShell\Modules;C:\Program Files\PowerShell\Modules;c:\program files\powershell\7\Modules;C:\Program Files\Windows
PowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules;c:\Users\Megam\.vscode\extensions\ms-vscode.powershell-2021.12.0\modules
Size : 284
Key : COMPUTERNAME
Value : DESKTOP-9EPNRSP
Size : 15
Key : ProgramFiles(x86)
Value : C:\Program Files (x86)
Size : 22
Key : SESSIONNAME
Value : Console
Size : 7
asTable
Key Size
--- ----
PROCESSOR_LEVEL 1
ALLUSERSPROFILE 14
HOMEDRIVE 2
InsaneAppName 59
PSExecutionPolicyPreference 12
GIT_ASKPASS 100
AppName 49
VSCODE_GIT_ASKPASS_NODE 64
TMP 33
PROCESSOR_REVISION 4
OneDriveConsumer 23
XXX__YYY 32
SystemRoot 10
VSCODE_GIT_IPC_HANDLE 35
SystemDrive 2
PROCESSOR_ARCHITECTURE 5
IntelliJ IDEA 54
CommonProgramW6432 29
ProgramFiles 16
windir 10
HOMEPATH 12
USERNAME 5
TERM_PROGRAM 6
USERDOMAIN_ROAMINGPROFILE 15
VSCODE_GIT_ASKPASS_EXTRA_ARGS 32
POWERSHELL_DISTRIBUTION_CHANNEL 4
LOGONSERVER 17
USERPROFILE 14
Path 635
TEMP 33
USERDOMAIN 15
CommonProgramFiles 29
PATHEXT 58
CommonProgramFiles(x86) 35
ProgramData 14
ORIGINAL_XDG_CURRENT_DESKTOP 9
OS 10
OneDrive 23
LANG 11
TERM_PROGRAM_VERSION 6
ProgramW6432 16
DriverData 38
VSCODE_GIT_ASKPASS_MAIN 105
LOCALAPPDATA 28
PROCESSOR_IDENTIFIER 50
CHROME_CRASHPAD_PIPE_NAME 39
PUBLIC 15
COLORTERM 9
APPDATA 30
ComSpec 27
NUMBER_OF_PROCESSORS 1
PSModulePath 284
COMPUTERNAME 15
ProgramFiles(x86) 22
SESSIONNAME 7
References
Select-Object
Custom expressions in common pipeline commands
Example 11: Create calculated properties for each InputObject
Format-List
Format-Table
██████ UPDATE for your CODE You can use this
# //Select Id, Name, Path, UpperName(custom), PathSize(custom) properties.
$reports.value | Where-Object {$_.Type -eq "PowerBIReport"} | Select-Object -Property Id, Name, Path, @{ Name="UpperName"; Expression={$_.Name.ToString().ToUpper()}}, @{ Name="PathSize"; Expression={$_.Path.Length}}