cougar77
Goto Top

Powershell Formatierung Ausgabedatei

Guten Morgen,

ich habe grade was, das ich nicht verstehe...

Folgende Anweisung:
Get-ChildItem -Path x:\pfad -Recurse -Force 
| Where-Object {$_.creationTime -lt (Get-Date).adddays(-30) -and $_.LastWriteTime -lt (Get-Date).adddays(-30)} 
| format-table @{N='Dateiname';E={$_.DirectoryName + "\" + $_.name}; width=90} ,@{N='Erstellungsdatum';E={(get-date $_.CreationTime -uformat %c)}; width=25},   
@{N='Letzte Änderung';E={(get-date $_.LastWriteTime -uformat %c)}; width=25}, @{N='Löschzeitpunkt';E={(get-date -uformat %c)}; width=25}   
>> $Log

tut bei Ausführung in der PS ISE genau das, was es soll - Dateiname, Erstellungsdatum, Änderungsdatum und aktuelles Datum - formatiert in eine Datei schreiben, sieht auch schick aus.
Lasse ich das gleich Skript per Job laufen, wird nur der Dateiname geschrieben, die anderen Attribute werden weggelassen.

Der User der das Skript direkt ausführt und der, der den Job ausführt sind der gleiche.

Wie kann das sein?

Content-ID: 308663

Url: https://administrator.de/contentid/308663

Ausgedruckt am: 22.11.2024 um 06:11 Uhr

129813
Lösung 129813 01.07.2016 aktualisiert um 08:38:52 Uhr
Goto Top
Hi,
use out-string in the pipeline before you write it to the file. The Format-Operators output objects not strings.
And your three lines should be in one. And also better would be the use of out-file or set-content / add-content.
">>" is old fashioned and leads sometimes to errors.

Regards
Deepsys
Deepsys 01.07.2016 um 09:25:39 Uhr
Goto Top
Hi,

Zitat von @129813:

Hi,
use out-string in the pipeline before you write it to the file. The Format-Operators output objects not strings.
ich hätte da mal eine Frage an dich:
Warum schreibst du in einem deutschsprachigen Forum auf englisch, wenn du die deutschen Fragen anscheinend gut verstehst?

Bitte nicht als Vorwurf sehen, sonder ist nur Neugier face-smile

VG,
Deepsys
Cougar77
Cougar77 01.07.2016 um 09:40:16 Uhr
Goto Top
Hi,

thanks for your comment.

I just broke the text for better readability here - it is in one line in my script.

It reads now, following your advice:

Get-ChildItem -Path x:\path -Recurse -Force | Where-Object {$_.creationTime -lt (Get-Date).adddays(-30) -and $_.LastWriteTime -lt (Get-Date).adddays(-30)} | format-table @{N='Dateiname';E={$_.DirectoryName + "\" + $_.name}; width=90} ,@{N='Erstellungsdatum';E={(get-date $_.CreationTime -uformat %c)}; width=25}, @{N='Letzte Änderung';E={(get-date $_.LastWriteTime -uformat %c)}; width=25}, @{N='Löschzeitpunkt';E={(get-date -uformat %c)}; width=25} | out-string |add-content $Log  

But it still does the same weird thing - right when started from ISE, wrong when started by Job.
129813
129813 01.07.2016 aktualisiert um 09:50:59 Uhr
Goto Top
Zitat von @Deepsys:
Warum schreibst du in einem deutschsprachigen Forum auf englisch, wenn du die deutschen Fragen anscheinend gut verstehst?
Hi, sorry for that, my understanding of the german language is good, but my writing isn't, and i am still improving it. So i don't want you precise germans complaining about my writing face-smile, this is the reason why I am writing in english.
129813
129813 01.07.2016 aktualisiert um 09:56:52 Uhr
Goto Top
Zitat von @Cougar77:
wrong when started by Job.
That means started by task scheduler?
Here it works without problems using a task.
Cougar77
Cougar77 01.07.2016 um 09:57:08 Uhr
Goto Top
Yes, I want it to run via Task Scheduler.

I cant get the difference.
129813
129813 01.07.2016 aktualisiert um 09:59:49 Uhr
Goto Top
I would strongly recommend export as CSV. Give it a try and export-csv the content.
Cougar77
Cougar77 01.07.2016 um 10:22:55 Uhr
Goto Top
Well, I could Export it as a csv - but I dont have what i want then - it is still an unformatted bunch of text, if not opened in Excel.

Im one step farther now - it seems not to ignore the fields in format-Table, as i have shortened the first field, then the second appeared.
It seems that when started as scheduled Task there is a restriction for the Overall width of the File - is that possible?
129813
Lösung 129813 01.07.2016 aktualisiert um 10:35:48 Uhr
Goto Top
That's what I said format-table outputs objects not strings so use format-table with Parameters -Autosize -Wrap to fit in the console and define a larger output of the console with out-string -width XXX.

Because format-table cuts of too long lines and so other columns behind are missing because they are lying outside the console size. Especially when the paths of your files are long.
Cougar77
Cougar77 01.07.2016 um 10:35:23 Uhr
Goto Top
You brougth me on the right way - Format-String takes a width, when i set that explicitely to a certain width, then it works automated as well.

Still dont understand why it is working different though.
129813
129813 01.07.2016 aktualisiert um 10:39:35 Uhr
Goto Top
Zitat von @Cougar77:
Still dont understand why it is working different though.
Because powershell consoles invoked from cmd have smaller width and console settings then the normal console of the ISE.