Powershell: if Abfrage in foreach Schleife
Hallo,
ich würde mich sehr freuen wenn ihr mir etwas auf die Sprünge helfen könntet.
Ich habe hier folgendes .txt File
Anrede;Nachname;Vorname;Mitarbeiternummer;Eintritt_1;Geburtsdatum;Mandant;Abrechnungskreis;Personalnummer;Austrittsdatum;Dienstgruppe;Kostenstelle
Frau;xxx;xxx;1310100000022;01.12.1998;19.08.1973;1310;100;000022;;01;161001
Frau;xxx;xxx;1310100000101;01.01.1999;02.09.1969;1310;100;000101;;01;161004
Frau;xxx;xxx;1310100000484;15.02.1990;29.09.1954;1310;100;000484;01.02.2018;07;190200
Dieses .txt möchte automatisiert bearbeiten, das eine Spalte "Eintritt_2" hinzugefügt wird mit dem Wert "01.02.2018". Dann soll ein Prüfung erfolgen.
Wenn Eintritt_1 >= Eintritt_2;
Dann Eintritt_2 = Eintritt_1;
Bis jetzt habe ich das ganze als csv umgewandelt und dann "bearbeitet" aber ich komme nicht irgendwie nicht weiter. Folgend mein Skript:
$csv = Import-CSV 'c:\test\mal.csv' -delimiter ";"
$csv | add-member -MemberType NoteProperty -Name "Eintritt" -Value "01.02.2018" -Force
$csv | Export-CSV 'c:\test\mal_fertig.csv' -NoType -delimiter ";" -Encoding default
foreach ($temp in $csv.Eintritt)
{
if ($csv.Eintrittsdatum -ge $csv.Eintitt)
{
$csv.Eintritt = $csv.Eintrittsdatum
}
}
Dann wird diese Meldung generiert
he property 'Eintritt' cannot be found on this object. Verify that the property exists and can be set.
At C:\test\Eintrit_neu.ps1:14 char:17
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'Eintritt' cannot be found on this object. Verify that the property exists and can be set.
At C:\test\Eintrit_neu.ps1:14 char:17
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'Eintritt' cannot be found on this object. Verify that the property exists and can be set.
At C:\test\Eintrit_neu.ps1:14 char:17
+ FullyQualifiedErrorId : PropertyAssignmentException
ICH BIN FÜR JEDE HILFE DANKBAR.... ICH SEHE DEN WALD VOR LAUTER BÄUMEN NICHT MEHR
ich würde mich sehr freuen wenn ihr mir etwas auf die Sprünge helfen könntet.
Ich habe hier folgendes .txt File
Anrede;Nachname;Vorname;Mitarbeiternummer;Eintritt_1;Geburtsdatum;Mandant;Abrechnungskreis;Personalnummer;Austrittsdatum;Dienstgruppe;Kostenstelle
Frau;xxx;xxx;1310100000022;01.12.1998;19.08.1973;1310;100;000022;;01;161001
Frau;xxx;xxx;1310100000101;01.01.1999;02.09.1969;1310;100;000101;;01;161004
Frau;xxx;xxx;1310100000484;15.02.1990;29.09.1954;1310;100;000484;01.02.2018;07;190200
Dieses .txt möchte automatisiert bearbeiten, das eine Spalte "Eintritt_2" hinzugefügt wird mit dem Wert "01.02.2018". Dann soll ein Prüfung erfolgen.
Wenn Eintritt_1 >= Eintritt_2;
Dann Eintritt_2 = Eintritt_1;
Bis jetzt habe ich das ganze als csv umgewandelt und dann "bearbeitet" aber ich komme nicht irgendwie nicht weiter. Folgend mein Skript:
$csv = Import-CSV 'c:\test\mal.csv' -delimiter ";"
$csv | add-member -MemberType NoteProperty -Name "Eintritt" -Value "01.02.2018" -Force
$csv | Export-CSV 'c:\test\mal_fertig.csv' -NoType -delimiter ";" -Encoding default
foreach ($temp in $csv.Eintritt)
{
if ($csv.Eintrittsdatum -ge $csv.Eintitt)
{
$csv.Eintritt = $csv.Eintrittsdatum
}
}
Dann wird diese Meldung generiert
he property 'Eintritt' cannot be found on this object. Verify that the property exists and can be set.
At C:\test\Eintrit_neu.ps1:14 char:17
$csv.Eintritt = $csv.Eintrittsdatum
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: ( , RuntimeException+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'Eintritt' cannot be found on this object. Verify that the property exists and can be set.
At C:\test\Eintrit_neu.ps1:14 char:17
$csv.Eintritt = $csv.Eintrittsdatum
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: ( , RuntimeException+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'Eintritt' cannot be found on this object. Verify that the property exists and can be set.
At C:\test\Eintrit_neu.ps1:14 char:17
$csv.Eintritt = $csv.Eintrittsdatum
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: ( , RuntimeException+ FullyQualifiedErrorId : PropertyAssignmentException
ICH BIN FÜR JEDE HILFE DANKBAR.... ICH SEHE DEN WALD VOR LAUTER BÄUMEN NICHT MEHR
Please also mark the comments that contributed to the solution of the article
Content-Key: 364497
Url: https://administrator.de/contentid/364497
Printed on: April 19, 2024 at 13:04 o'clock
5 Comments
Latest comment
$csv = Import-CSV 'c:\test\mal.csv' -delimiter ";"
$csv | add-member -MemberType NoteProperty -Name "Eintritt_2" -Value (get-date "01.02.2018") -Force
$csv | %{
If ((get-date $_.Eintritt_1) -gt $_.Eintritt_2){
$_.Eintritt_2 = $_.Eintritt_1
}
}
$csv | Export-CSV 'c:\test\mal_fertig.csv' -NoType -delimiter ";" -Encoding default
Zitat von @LuceDeCiello:
Allerdings habe ich jetzt bei der Ausgabe in der Spalte "Eintritt_2" 00.00.00 mit drinnen, da sollte nur das Datum stehen.
Einfach das Format ändern mit -F Parameter Allerdings habe ich jetzt bei der Ausgabe in der Spalte "Eintritt_2" 00.00.00 mit drinnen, da sollte nur das Datum stehen.
Und wie bekomme ich denn die vielen Anführungszeichen weg?
Die machen nichts, bei einer CSV werden die Weg interpretiert.Ansonsten an ConvertTo-CSV pipen und dann mit -replace die Anführungszeichen killen.
Ich sehe gerade, das dort ebenfalls noch einige Fehlermeldungen generiert werden ???
Du musst prüfen ob in den Zellen wirklich Datumswerte stehen, die Fehler kommen weil die Felder bei dir leer sein können ...