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-Key: 308663

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

Printed on: April 26, 2024 at 05:04 o'clock

Mitglied: 129813
Solution 129813 Jul 01, 2016 updated at 06:38:52 (UTC)
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
Member: Deepsys
Deepsys Jul 01, 2016 at 07:25:39 (UTC)
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
Member: Cougar77
Cougar77 Jul 01, 2016 at 07:40:16 (UTC)
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.
Mitglied: 129813
129813 Jul 01, 2016 updated at 07:50:59 (UTC)
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.
Mitglied: 129813
129813 Jul 01, 2016 updated at 07:56:52 (UTC)
Goto Top
Zitat von @Cougar77:
wrong when started by Job.
That means started by task scheduler?
Here it works without problems using a task.
Member: Cougar77
Cougar77 Jul 01, 2016 at 07:57:08 (UTC)
Goto Top
Yes, I want it to run via Task Scheduler.

I cant get the difference.
Mitglied: 129813
129813 Jul 01, 2016 updated at 07:59:49 (UTC)
Goto Top
I would strongly recommend export as CSV. Give it a try and export-csv the content.
Member: Cougar77
Cougar77 Jul 01, 2016 at 08:22:55 (UTC)
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?
Mitglied: 129813
Solution 129813 Jul 01, 2016 updated at 08:35:48 (UTC)
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.
Member: Cougar77
Cougar77 Jul 01, 2016 at 08:35:23 (UTC)
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.
Mitglied: 129813
129813 Jul 01, 2016 updated at 08:39:35 (UTC)
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.