Benutzer mit den meisten Antworten
expressions vs format

Frage
-
moin,
ich habe hier zwei Beispiel für Formatierungen gefunden
Get-ChildItem c:\docs | ForEach-Object {'Filename: {0} Created: {1}' -f $_.fullname,$_.creationtime}
und hier eines mit Expressions
$a = @{Expression={$_.Name};Label="Process Name";width=25}, `
@{Expression={$_.ID};Label="Process ID";width=15}, `
@{Expression={$_.MainWindowTitle};Label="Window Title";width=40}
Get-Process | Format-Table $a
und nehme an man könnte auch den -f Parameter anstelle Expression verwenden. Wäre etwas leserlicher. Oder wo liegt hier der Unterschiedget-process|ft-f{$_.name};Label="überschrift" ==> geht nicht
Habe jetzt noch etwas gegoogelt.
ich dachte expressions nutzt man nur für Objekte. Diese Beispiele sind aber auf FT
Get-SmbShare | FT Name, @{ Label=”Folder”; Expression={$_.Path} }, Description –AutoSize
Expression am Anfang oder Ende scheint egal zu sein.
Chris
- Bearbeitet -- Chris -- Samstag, 19. November 2016 09:55
Antworten
-
Moin,
Format-Table erwartet als Wert für den Parameter "Property" folgendes:
-Property [<Object[]>] Specifies the object properties that appear in the display and the order in which they appear. Type one or more property names (separated by commas), or use a hash table to display a calculated property. Wildcards are permitted. If you omit this parameter, the properties that appear in the display depend on the object being displayed. The parameter name ("Property") is optional. You cannot use the Property and View parameters in the same command. The value of the Property parameter can be a new calculated property. To create a calculated, property, use a hash table. Valid keys are: -- Name (or Label) <string> -- Expression <string> or <script block> -- FormatString <string> -- Width <int32> -- Alignment (value can be "Left", "Center", or "Right")
Somit kann als Expression ein beliebiges Konstrukt übergeben werden, welches dann als Spalte mit dem Namen erscheint, den Du als Label übergibst. Das ist aber im konkreten Cmdlet so definiert. Bei Select-Object (ich vermute, es ist das, was Du mit "ich dachte, Expressions nutzt man nur für Objekte" meinst) ist es anders definiert:
-Property [<Object[]>] Specifies the properties to select. Wildcards are permitted. The value of the Property parameter can be a new calculated property. To create a calculated, property, use a hash table. Valid keys are: -- Name (or Label) <string> -- Expression <string> or <script block>
Anfang oder Ende ist tatsächlich egal, denn eine Hashtable ist ein Key-Value-Pair-Konstrukt.Evgenij Smirnov
I work @ msg services ag, Berlin -> http://www.msg-services.de
I blog (in German) @ http://it-pro-berlin.de
my stuff in PSGallery --> https://www.powershellgallery.com/profiles/it-pro-berlin.de/
Exchange User Group, Berlin -> http://exusg.de
Windows Server User Group, Berlin -> http://www.winsvr-berlin.de
Mark Minasi Technical Forum, reloaded -> http://newforum.minasi.com- Als Antwort markiert -- Chris -- Samstag, 19. November 2016 12:25
-
Hmmm vielleicht als genereller Tipp zum Thema Formatieren, wenn Du die ObjeKte, die Du der Pipeline entlang weiterreichen möchtest, formatieren willst, solltest Du das erst ganz am Ende machen - sonst unterbrichst Du die Pipeline. Es ist also z.B. ein gute Idee, wenn Du eigene Funktionen/Cmdlets erstellst, deren Output so 'roh und unformatiert' zu halten wie es geht. Dann kannst Du jederzeit situativ mit einem nachgestellten Format-Cmdlet entscheiden, wie der Output dieses Mal aussehen soll. Das macht die Funktionen universeller benutzbar.
Grüße - Best regards
PS:> (79,108,97,102|%{[char]$_})-join''- Als Antwort markiert -- Chris -- Samstag, 19. November 2016 18:46
Alle Antworten
-
Moin,
Format-Table erwartet als Wert für den Parameter "Property" folgendes:
-Property [<Object[]>] Specifies the object properties that appear in the display and the order in which they appear. Type one or more property names (separated by commas), or use a hash table to display a calculated property. Wildcards are permitted. If you omit this parameter, the properties that appear in the display depend on the object being displayed. The parameter name ("Property") is optional. You cannot use the Property and View parameters in the same command. The value of the Property parameter can be a new calculated property. To create a calculated, property, use a hash table. Valid keys are: -- Name (or Label) <string> -- Expression <string> or <script block> -- FormatString <string> -- Width <int32> -- Alignment (value can be "Left", "Center", or "Right")
Somit kann als Expression ein beliebiges Konstrukt übergeben werden, welches dann als Spalte mit dem Namen erscheint, den Du als Label übergibst. Das ist aber im konkreten Cmdlet so definiert. Bei Select-Object (ich vermute, es ist das, was Du mit "ich dachte, Expressions nutzt man nur für Objekte" meinst) ist es anders definiert:
-Property [<Object[]>] Specifies the properties to select. Wildcards are permitted. The value of the Property parameter can be a new calculated property. To create a calculated, property, use a hash table. Valid keys are: -- Name (or Label) <string> -- Expression <string> or <script block>
Anfang oder Ende ist tatsächlich egal, denn eine Hashtable ist ein Key-Value-Pair-Konstrukt.Evgenij Smirnov
I work @ msg services ag, Berlin -> http://www.msg-services.de
I blog (in German) @ http://it-pro-berlin.de
my stuff in PSGallery --> https://www.powershellgallery.com/profiles/it-pro-berlin.de/
Exchange User Group, Berlin -> http://exusg.de
Windows Server User Group, Berlin -> http://www.winsvr-berlin.de
Mark Minasi Technical Forum, reloaded -> http://newforum.minasi.com- Als Antwort markiert -- Chris -- Samstag, 19. November 2016 12:25
-
Hmmm vielleicht als genereller Tipp zum Thema Formatieren, wenn Du die ObjeKte, die Du der Pipeline entlang weiterreichen möchtest, formatieren willst, solltest Du das erst ganz am Ende machen - sonst unterbrichst Du die Pipeline. Es ist also z.B. ein gute Idee, wenn Du eigene Funktionen/Cmdlets erstellst, deren Output so 'roh und unformatiert' zu halten wie es geht. Dann kannst Du jederzeit situativ mit einem nachgestellten Format-Cmdlet entscheiden, wie der Output dieses Mal aussehen soll. Das macht die Funktionen universeller benutzbar.
Grüße - Best regards
PS:> (79,108,97,102|%{[char]$_})-join''- Als Antwort markiert -- Chris -- Samstag, 19. November 2016 18:46