lucedeciello
Goto Top

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

back-to-top$csv.Eintritt = $csv.Eintrittsdatum

back-to-top~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidOperation: (face-smile , 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

back-to-top$csv.Eintritt = $csv.Eintrittsdatum

back-to-top~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidOperation: (face-smile , 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

back-to-top$csv.Eintritt = $csv.Eintrittsdatum

back-to-top~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : InvalidOperation: (face-smile , RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException


ICH BIN FÜR JEDE HILFE DANKBAR.... ICH SEHE DEN WALD VOR LAUTER BÄUMEN NICHT MEHR

Content-Key: 364497

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

Printed on: April 19, 2024 at 13:04 o'clock

Mitglied: 135333
135333 Feb 12, 2018 at 13:19:28 (UTC)
Goto Top
$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  
Member: LuceDeCiello
LuceDeCiello Feb 12, 2018 at 13:38:36 (UTC)
Goto Top
Hey super.... das funktioniert bereits viel besser !!!

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?

Der fertige Datensatz sollte so aussehen:

Anrede;Nachname;Vorname;Mitarbeiternummer;Eintrittsdatum;Geburtsdatum;Mandant;Abrechnungskreis;Personalnummer;Austrittsdatum;Dienstgruppe;Kostenstelle;Eintritt_2
Frau;XXX;XXX;1310100000022;01.03.2018;19.08.1973;1310;100;000022;;01;161001;01.02.2018

aktuell sieht es aber so aus:

"Anrede";"Nachname";"Vorname";"Mitarbeiternummer";"Eintrittsdatum";"Geburtsdatum";"Mandant";"Abrechnungskreis";"Personalnummer";"Austrittsdatum";"Dienstgruppe";"Kostenstelle";"Eintritt_2"
"Frau";"XXX";"XXX";"1310100000022";"01.03.2018";"19.08.1973";"1310";"100";"000022";"";"01";"161001";"01.02.2018 00:00:00"
"Frau";"XXX";"XXX";"1310100000101";"01.01.1999";"02.09.1969";"1310";"100";"000101";"";"01";"161004";"01.02.2018 00:00:00"
"Frau";"XXX";"XXX";"1310100000484";"15.02.1990";"29.09.1954";"1310";"100";"000484";"01.02.2018";"07";"190200";"01.02.2018 00:00:00"
Member: LuceDeCiello
LuceDeCiello Feb 12, 2018 at 13:55:13 (UTC)
Goto Top
Ich sehe gerade, das dort ebenfalls noch einige Fehlermeldungen generiert werden ???

Get-Date : Cannot bind parameter 'Date' to the target. Exception setting "Date": "Object reference not set to an instance of an object."
At C:\test\Eintrit_neu.ps1:9 char:19

back-to-topIf ((get-date $_.Eintritt_1) -gt $_.Eintritt_2){

back-to-top~~~~~~~~~~~~~

+ CategoryInfo : WriteError: (face-smile [Get-Date], ParameterBindingException
+ FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.GetDateCommand

Get-Date : Cannot bind parameter 'Date' to the target. Exception setting "Date": "Object reference not set to an instance of an object."
At C:\test\Eintrit_neu.ps1:9 char:19

back-to-topIf ((get-date $_.Eintritt_1) -gt $_.Eintritt_2){

back-to-top~~~~~~~~~~~~~

+ CategoryInfo : WriteError: (face-smile [Get-Date], ParameterBindingException
+ FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.GetDateCommand

Get-Date : Cannot bind parameter 'Date' to the target. Exception setting "Date": "Object reference not set to an instance of an object."
At C:\test\Eintrit_neu.ps1:9 char:19

back-to-topIf ((get-date $_.Eintritt_1) -gt $_.Eintritt_2){

back-to-top~~~~~~~~~~~~~

+ CategoryInfo : WriteError: (face-smile [Get-Date], ParameterBindingException
+ FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.GetDateCommand
Mitglied: 135333
135333 Feb 12, 2018 updated at 14:52:22 (UTC)
Goto Top
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 face-wink
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 ...
Member: LuceDeCiello
LuceDeCiello Feb 12, 2018 at 15:11:57 (UTC)
Goto Top
Danke .... hat super geklappt !!!!