Outlook Kontakte importieren ins OTRS als Kunde

Hilfe zu Znuny Problemen aller Art
Locked
michel
Znuny newbie
Posts: 12
Joined: 24 Sep 2009, 01:39
Znuny Version: 2.4

Outlook Kontakte importieren ins OTRS als Kunde

Post by michel »

Hallo liebe Gemeinde.

Ich wollte fragen ob es irgendwie möglich ist Outlook Kontakte ins OTRS zu importieren.
Die Kontakte sollten als Kunde ins OTRS eingespeichert werden.

Ist dies möglich ?

Ich danke im vorraus
OTRS 2.4
Windows Server 2003
garwen
Znuny expert
Posts: 260
Joined: 03 Feb 2009, 16:51
Znuny Version: 2.4.7

Outlook Kontakte importieren ins OTRS als Kunde

Post by garwen »

Nun, in Outlook kannst Du die Kontakte exportieren und in verschiedenen Formen abspreichern. (z.B. kommagetrenntes TXT).
Ich kenne zwar keinen Weg, dieses File dann mit OTRS selber ein zu lesen, aber es gibt sicher SQL Befehle um solche Dateien direkt in die Datenbank zu importieren.
Dafür müsste dir aber jemand anders weiterhelfen, da ich mich mit den nötigen SQL Syntax nicht auskenne.
MfG
~Fredy

Produktiv: OTRS 3.0.11/ openSUSE 11.4 / Apache2, MySQL5 / Exchange 2010
Joyner
Znuny expert
Posts: 185
Joined: 08 Feb 2008, 14:49
Znuny Version: 2.2.8
Location: Berlin

Outlook Kontakte importieren ins OTRS als Kunde

Post by Joyner »

Hallo,

im scripts-Verz. gibt es eine sync-csv2db.pl

Outlook-Kontakte im CSV exportieren und dann über dieses Script im OTRS importieren.

mfg
Sebastian
Produktiv: OTRS 2.4.7, CiCS 3.1.5, Eigenentwicklungen
β-Testing/Mirror: OTRS 2.4.7, ITSM 2.0.2, CiCS(::ITSM) 3.1.5 (2.1.6), Eigenentwicklungen OS: Win2003, XAMPP, Apache2.2/MySQL 5, Trac/SVN
michel
Znuny newbie
Posts: 12
Joined: 24 Sep 2009, 01:39
Znuny Version: 2.4

Outlook Kontakte importieren ins OTRS als Kunde

Post by michel »

Vielen Dank für die schnellen Antworten.

Ich werde es mal gleich ausprobieren.
michel
Znuny newbie
Posts: 12
Joined: 24 Sep 2009, 01:39
Znuny Version: 2.4

Outlook Kontakte importieren ins OTRS als Kunde

Post by michel »

puh ... blöde frage .. wie kann ich dieses script ausführen..

localhost/otrs/sync-csv2db.pls

funktioniert indem fall nicht..

wie muss ich den pfad angeben ?

Ich danke im vorraus
OTRS 2.4
Windows Server 2003
garwen
Znuny expert
Posts: 260
Joined: 03 Feb 2009, 16:51
Znuny Version: 2.4.7

Outlook Kontakte importieren ins OTRS als Kunde

Post by garwen »

Was hast für einen Server ?
MfG
~Fredy

Produktiv: OTRS 3.0.11/ openSUSE 11.4 / Apache2, MySQL5 / Exchange 2010
michel
Znuny newbie
Posts: 12
Joined: 24 Sep 2009, 01:39
Znuny Version: 2.4

Outlook Kontakte importieren ins OTRS als Kunde

Post by michel »

Ich habs zum testen bei mir am Laptop lokal laufen...

Betriebssystem Windows XP unter apache..

Muss ich die syncuser_csv2otrs.pl über die Perl Konsole ausführen ?
OTRS 2.4
Windows Server 2003
garwen
Znuny expert
Posts: 260
Joined: 03 Feb 2009, 16:51
Znuny Version: 2.4.7

Outlook Kontakte importieren ins OTRS als Kunde

Post by garwen »

Ja. Oder rufst es einfach in der Eingabeaufforderung über die Perl.exe auf.

z.B.:

Code: Select all

C:\OTRS\StrawberryPerl\perl\bin\perl.exe C:\OTRS\OTRS\scripts\syncuser_csv2otrs.pl -s C:\Temp\test.csv
Auf einem Win2k3 Server geht das. Pfad anpassen natürlich, hab einfach irgendwas hin geschrieben.
MfG
~Fredy

Produktiv: OTRS 3.0.11/ openSUSE 11.4 / Apache2, MySQL5 / Exchange 2010
michel
Znuny newbie
Posts: 12
Joined: 24 Sep 2009, 01:39
Znuny Version: 2.4

Outlook Kontakte importieren ins OTRS als Kunde

Post by michel »

Vielen Dank für deine Hilfe.
Hat mir sehr geholfen bzw. es funktioniert :)

Aber die Kontakte werden als Agents gespeichert und nicht als Kunde ... Was muss ich einstellen damit die Kontakte als Customer gespeichert werden?

Ich danke im vorraus
OTRS 2.4
Windows Server 2003
Joyner
Znuny expert
Posts: 185
Joined: 08 Feb 2008, 14:49
Znuny Version: 2.2.8
Location: Berlin

Outlook Kontakte importieren ins OTRS als Kunde

Post by Joyner »

Vielleicht funktioniert es, wenn du aus aus jedem "User..." ein "CustomerUser..." machst...aber ohne Gewähr.

mfg
Sebastian
Produktiv: OTRS 2.4.7, CiCS 3.1.5, Eigenentwicklungen
β-Testing/Mirror: OTRS 2.4.7, ITSM 2.0.2, CiCS(::ITSM) 3.1.5 (2.1.6), Eigenentwicklungen OS: Win2003, XAMPP, Apache2.2/MySQL 5, Trac/SVN
michel
Znuny newbie
Posts: 12
Joined: 24 Sep 2009, 01:39
Znuny Version: 2.4

Outlook Kontakte importieren ins OTRS als Kunde

Post by michel »

Wie ist das gemeint mit "Aus jedem User einen CustomerUser machen" ?

Mein Problem ist , dass das Script in die falsche Tabelle schreibt. Sie schreibt nämlich in die system_user rein und ich will dass sie aber in die customer_user rein schreibt.
OTRS 2.4
Windows Server 2003
Joyner
Znuny expert
Posts: 185
Joined: 08 Feb 2008, 14:49
Znuny Version: 2.2.8
Location: Berlin

Outlook Kontakte importieren ins OTRS als Kunde

Post by Joyner »

aus z.B. UserObject wird CustomerUserObject ...
aus Kernel::System::User wird Kernel::System::CustomerUser
usw.

und aus z.b."update user" wird "update customer_user"
Produktiv: OTRS 2.4.7, CiCS 3.1.5, Eigenentwicklungen
β-Testing/Mirror: OTRS 2.4.7, ITSM 2.0.2, CiCS(::ITSM) 3.1.5 (2.1.6), Eigenentwicklungen OS: Win2003, XAMPP, Apache2.2/MySQL 5, Trac/SVN
michel
Znuny newbie
Posts: 12
Joined: 24 Sep 2009, 01:39
Znuny Version: 2.4

Outlook Kontakte importieren ins OTRS als Kunde

Post by michel »

Danke für deine Antwort... Aber hat leider nicht funktioniert.

Bin aber selber fündig geworden..

Hier der Code wenn man eine csv in die customer_user tabelle importieren will:

Code: Select all

#!C:/PROGRA~1/OTRS/StrawberryPerl/perl/bin/perl.exe -w
# --
# synccustomer_csv2otrs.pl - sync csv user list or otrs
# Copyright (C) 2001-2008 xxx, http://otrs.org/
# --
# $Id: synccustomer_csv2otrs.pl,v 1.10 2008/06/09 14:52:11 tr Exp $
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# --

# config options / csv file - column 0-...
my %Fields = ();
$Fields{login} = 0;
$Fields{email} = 1;
$Fields{customer_id} = 2;
$Fields{pw} = 3;
$Fields{salutation} = 4;
$Fields{firstname} = 5;
$Fields{lastname} = 6;
$Fields{comment} = 7;
$Fields{loginnew} = 8;
$Fields{gueltig} = 9;

# use ../ as lib location
use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin) . "/Kernel/cpan-lib";
use strict;
use warnings;

use Getopt::Std;
use Kernel::Config;
use Kernel::System::Log;
use Kernel::System::Main;
use Kernel::System::Time;
use Kernel::System::DB;
#use Kernel::System::User;
use Kernel::System::CustomerUser;

# common objects
my %CommonObject = ();
$CommonObject{ConfigObject} = Kernel::Config->new();
$CommonObject{EncodeObject} = Kernel::System::Encode->new(%CommonObject);
$CommonObject{LogObject}    = Kernel::System::Log->new(
    LogPrefix => 'test.pl',
    %CommonObject
);
$CommonObject{MainObject} = Kernel::System::Main->new(%CommonObject);
$CommonObject{TimeObject} = Kernel::System::Time->new(%CommonObject);
$CommonObject{DBObject}   = Kernel::System::DB->new(%CommonObject);
#$CommonObject{UserObject} = Kernel::System::User->new(%CommonObject);
$CommonObject{DBObject} = Kernel::System::DB->new(%CommonObject);
$CommonObject{CustomerUserObject} = Kernel::System::CustomerUser->new(%CommonObject);

# get options
my %Opts = ();
getopt( 's', \%Opts );
my $End = "\n";
if ( !$Opts{s} ) {
    die "Need -s <CSV_FILE>\n";
}

my $Lang = $CommonObject{ConfigObject}->Get('DefaultLanguage') || 'de';

# read csv file
open (IN, "< $Opts{'s'}") || die "Can't read $Opts{'s'}: $!";
while( <IN>) {
   my $LineStrg = $_;
   $LineStrg =~ s/\"//g;
    my @Line = split(/;/, $LineStrg);

    # check if user extsis
    #my %UserData = $CommonObject{UserObject}->GetUserData( User => $Line[ $Fields{UserLogin} ] );
    my %CustomerUserData = $CommonObject{CustomerUserObject}->CustomerUserDataGet(User => $Line[$Fields{login}]);


    # if there is no pw in the csv list, gererate one
    if( !$Line[$Fields{pw}]) {
        $Line[$Fields{pw}] = $CommonObject{CustomerUserObject}->GenerateRandomPassword();
    }

    # update user
    if( %CustomerUserData) {
        print "Updating customer user '$Line[$Fields{login}]' with password '$Line[$Fields{pw}]'\n";
$CommonObject{CustomerUserObject}->CustomerUserUpdate(
         Source        => $Line[$Fields{customer_id}] || '', # CustomerUser source config
         ID            => $Line[$Fields{login}], # current user login
         UserLogin     => $Line[$Fields{loginnew}],       # new user login
         UserFirstname => $Line[$Fields{firstname}],
         UserLastname  => $Line[$Fields{lastname}],
         UserPassword  => $Line[$Fields{pw}],    # not required
         UserEmail     => $Line[$Fields{email}],
         UserCustomerID => $Line[$Fields{customer_id}] || '',
         UserSalutation => $Line[$Fields{salutation}] || '',
         UserComment => $Line[$Fields{comment}] || '',
         ValidID       => $Line[$Fields{gueltig}],
         UserID        => 123,
    );
     }

    # add user
    else {
         print "Add customer_user '$Line[$Fields{login}]' ($Line[$Fields{firstname}],$Line[$Fields{lastname}]) with password '$Line[$Fields{pw}]'\n";
        $CustomerUserData{UserID} = $CommonObject{CustomerUserObject}->CustomerUserAdd(
            UserSalutation => $Line[$Fields{salutation}] || '',
            UserFirstname => $Line[$Fields{firstname}],
            UserLastname => $Line[$Fields{lastname}],
            UserLogin => $Line[$Fields{loginnew}],
            UserPassword => $Line[$Fields{pw}],
            UserEmail => $Line[$Fields{email}],
            CustomerID => $Line[$Fields{customer_id}] || '',
            UserCustomerID => $Line[$Fields{customer_id}] || '',
            UserComment => $Line[$Fields{comment}] || '',
            ValidID => $Line[$Fields{gueltig}],
            UserID => 1,
        );
    }
}
close( IN);
OTRS 2.4
Windows Server 2003
deinmam
Znuny newbie
Posts: 19
Joined: 24 Oct 2012, 15:04
Znuny Version: 3.1.11

Re: Outlook Kontakte importieren ins OTRS als Kunde

Post by deinmam »

Hallo Menschen,

ich habe aus Outlook 2010 die Kontakte exportiert. Jedoch wirft mir DOS beim ausführen des Scriptes, vom Herrn über mir, folgende Fehlermeldung aus:

Code: Select all

Traceback (316):
   Module: Kernel::System::CustomerUser::DB::CustomerUserAdd (v1.88.2.1) Line: 5
53
   Module: Kernel::System::CustomerUser::CustomerUserAdd (v1.63.2.1) Line: 376
   Module: C:\Programme\OTRS\OTRS\scripts\syncuser_csv2otrs_kunden.pl (unknown v
ersion) Line: 117

Use of uninitialized value in concatenation (.) or string at C:\Programme\OTRS\O
TRS\scripts\syncuser_csv2otrs_kunden.pl line 116, <IN> line 507.
Use of uninitialized value in concatenation (.) or string at C:\Programme\OTRS\O
TRS\scripts\syncuser_csv2otrs_kunden.pl line 116, <IN> line 507.
Add customer_user ',Gerhard,,Stein,,Kondons,,,,,,,,,,,,,,,,,,,,,,,,,,00 49 654561231,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,0.0.00,Keine Angabe,,,,0.0.00,,,,,,,,,,
,,Niedrig,,,,,,Normal
' (,) with password '848654896'
ERROR: test.pl-10 Perl: 5.12.3 OS: MSWin32 Time: Mon Nov 12 12:11:52 2012

 Message: Need UserFirstname!
Ich vermute wohl, es liegt daran, dass alle Kontaktnamen in Outlook im Feld "Nachname" eingetragen wurde. Es sind zu viele um alle noch einmal umzuschreiben.
Ich kenne mich mit der Syntax von Datenbanken nicht gut aus. Ich hätte aber die Idee, dass Script umzuschreiben, dass die Nachnamen verwendet werden, statt die Vornamen.
Könntet ihr mir dabei helfen und ist das überhaupt möglich?

Nette Grüße
Deinmam
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: Outlook Kontakte importieren ins OTRS als Kunde

Post by reneeb »

Das liegt daran, dass das Skript Semikolon-separierte Spalten erwartet, Du aber Komma-separierte Spalten hast. Entweder änderst Du den Export, oder Du änderst im Skript

Code: Select all

my @Line = split(/;/, $LineStrg);
in

Code: Select all

my @Line = split(/,/, $LineStrg);

Hinweis: Vorsicht wenn "," in den Nutzdaten vorkommen können... Der Code teil wirklich an *jedem* ","
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
deinmam
Znuny newbie
Posts: 19
Joined: 24 Oct 2012, 15:04
Znuny Version: 3.1.11

Re: Outlook Kontakte importieren ins OTRS als Kunde

Post by deinmam »

Danke für deine Hilfe :)

Ich habe die Zeile so angepasst, wie du es beschrieben hast und sie schaut nun so aus:
my @Line = split(/,/, $LineStrg);
Allerdings wirft er immernoch fehler aus:

Code: Select all

 Traceback (1684):
   Module: Kernel::System::CustomerUser::DB::CustomerUserAdd (v1.88.2.1) Line: 5
53
   Module: Kernel::System::CustomerUser::CustomerUserAdd (v1.63.2.1) Line: 376
   Module: C:\Programme\OTRS\OTRS\scripts\syncuser_csv2otrs_kunden.pl (unknown v
ersion) Line: 117

Add customer_user '' (DingDong GmbH,) with password '85939575'
ERROR: test.pl-10 Perl: 5.12.3 OS: MSWin32 Time: Mon Nov 12 13:54:46 2012

 Message: Need UserLastname!
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: Outlook Kontakte importieren ins OTRS als Kunde

Post by reneeb »

Am Anfang des Skripts gibt es ein wenig Konfiguration, welche Daten wo in der CSV-Datei stehen:

Code: Select all

my %Fields = ();
$Fields{login} = 0;
$Fields{email} = 1;
$Fields{customer_id} = 2;
$Fields{pw} = 3;
$Fields{salutation} = 4;
$Fields{firstname} = 5;
$Fields{lastname} = 6;
$Fields{comment} = 7;
$Fields{loginnew} = 8;
$Fields{gueltig} = 9;
Du musst mal schauen, ob das so mit Deiner Datei passt (wichtig: die Spaltenzählung fängt bei 0 an).
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
deinmam
Znuny newbie
Posts: 19
Joined: 24 Oct 2012, 15:04
Znuny Version: 3.1.11

Re: Outlook Kontakte importieren ins OTRS als Kunde

Post by deinmam »

So fängt die Exportierte.csv an, diese Spalten habe ich abgezählt:

Code: Select all

"Anrede","Vorname","Weitere Vornamen","Nachname","Suffix","Firma","Abteilung","Position","Straße geschäftlich","Straße geschäftlich 2","Straße geschäftlich 3","Ort geschäftlich","Region geschäftlich","Postleitzahl geschäftlich","Land/Region geschäftlich","Straße privat","Straße privat 2","Straße privat 3","Ort privat","Bundesland/Kanton privat","Postleitzahl privat","Land/Region privat","Weitere Straße","Weitere Straße 2","Weitere Straße 3","Weiterer Ort","Weiteres/r Bundesland/Kanton","Weitere Postleitzahl","Weiteres/e Land/Region","Telefon Assistent","Fax geschäftlich","Telefon geschäftlich","Telefon geschäftlich 2","Rückmeldung","Autotelefon","Telefon Firma","Fax privat","Telefon privat","Telefon privat 2","ISDN","Mobiltelefon","Weiteres Fax","Weiteres Telefon","Pager","Haupttelefon","Mobiltelefon 2","Telefon für Hörbehinderte","Telex","Abrechnungsinformation","Benutzer 1","Benutzer 2","Benutzer 3","Benutzer 4","Beruf","Büro","E-Mail-Adresse","E-Mail-Typ","E-Mail: Angezeigter Name","E-Mail 2: Adresse","E-Mail 2: Typ","E-Mail 2: Angezeigter Name","E-Mail 3: Adresse","E-Mail 3: Typ","E-Mail 3: Angezeigter Name","Empfohlen von","Geburtstag","Geschlecht","Hobby","Initialen","Internet Frei/Gebucht","Jahrestag","Kategorien","Kinder","Konto","Name Assistent","Name des/r Vorgesetzten","Notizen","Organisationsnr.","Ort","Partner","Postfach geschäftlich","Postfach privat","Priorität","Privat","Regierungsnr.","Reisekilometer","Sprache","Stichwörter","Vertraulichkeit","Verzeichnisserver","Webseite","Weiteres Postfach"
Danach habe ich die Spaltennummern im Script wie folgt eingetragen:
$Fields{login} = 55;
$Fields{email} = 56;
$Fields{customer_id} = 5;
$Fields{pw} = 54;
$Fields{salutation} = 0;
$Fields{firstname} = 1;
$Fields{lastname} = 3;
$Fields{comment} = 7;
$Fields{loginnew} = 8;
$Fields{gueltig} = 9;
Dennoch wirft er immernoch die gleiche Fehlermeldung aus, nur das zwischendurch auch mal "Need UserEmail!" hinzugekommen ist. :shock:
deinmam
Znuny newbie
Posts: 19
Joined: 24 Oct 2012, 15:04
Znuny Version: 3.1.11

Re: Outlook Kontakte importieren ins OTRS als Kunde

Post by deinmam »

[PUSH]
jojo
Znuny guru
Posts: 15020
Joined: 26 Jan 2007, 14:50
Znuny Version: Git Master
Contact:

Re: Outlook Kontakte importieren ins OTRS als Kunde

Post by jojo »

Alle konfigurierten Felder müssen gefüllt sein.

Deine Ungeduld zeigt mir, das Du kommerziellen Support ins Auge fassen solltest. Ein Push ist hier nicht gerne gesehen!
"Production": OTRS™ 8, OTRS™ 7, STORM powered by OTRS
"Testing": ((OTRS Community Edition)) and git Master

Never change Defaults.pm! :: Blog
Professional Services:: http://www.otrs.com :: enjoy@otrs.com
deinmam
Znuny newbie
Posts: 19
Joined: 24 Oct 2012, 15:04
Znuny Version: 3.1.11

Re: Outlook Kontakte importieren ins OTRS als Kunde

Post by deinmam »

Das Wort "Geduld" ist relativ. Ich kann meine sehnsüchte zeitweise zurückstellen, doch wer definiert "Zeit"? Wie lange ist "kurz" oder wie kurz ist "lang"? Trozdem danke.

Meinst du damit, dass alle Felder im Script ausgefüllt sein müssen (von 0-9) ?
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: Outlook Kontakte importieren ins OTRS als Kunde

Post by reneeb »

Kannst Du mal eine kleine Beispiel-CSV zur Verfügung stellen (bitte Dummy-Daten)?
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
deinmam
Znuny newbie
Posts: 19
Joined: 24 Oct 2012, 15:04
Znuny Version: 3.1.11

Re: Outlook Kontakte importieren ins OTRS als Kunde

Post by deinmam »

reneeb wrote:Kannst Du mal eine kleine Beispiel-CSV zur Verfügung stellen (bitte Dummy-Daten)?
Gerne doch. Ich habe sie natürlich bearbeitet und einen Eintrag übrig gelassen und die Daten abgeändert. Ich hoffe das hilft. Danke schon mal reneeb!

kontakte.CSV
reneeb
Znuny guru
Posts: 5018
Joined: 13 Mar 2011, 09:54
Znuny Version: 6.0.x
Real Name: Renée Bäcker
Company: Perl-Services.de
Contact:

Re: Outlook Kontakte importieren ins OTRS als Kunde

Post by reneeb »

Das Problem ist hier, dass Zeilenumbrüche in den Nutzdaten vorkommt und das Skript damit nicht umgehen kann. CSV-Daten zu parsen ist nicht ganz trivial. Ich habe das Skript mal angepasst, so dass es das Modul Text::CSV verwendet.

Code: Select all

#/usrl/bin/perl.exe -w
# --
# synccustomer_csv2otrs.pl - sync csv user list or otrs
# Copyright (C) 2012 Perl-Services.de, http://perl-services.de
# --
# $Id: synccustomer_csv2otrs.pl,v 1.10 2008/06/09 14:52:11 tr Exp $
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# --

# config options / csv file - column 0-...
my %Fields = ();
$Fields{login} = 0;
$Fields{email} = 1;
$Fields{customer_id} = 2;
$Fields{pw} = 3;
$Fields{salutation} = 4;
$Fields{firstname} = 5;
$Fields{lastname} = 6;
$Fields{comment} = 7;
$Fields{loginnew} = 8;
$Fields{gueltig} = 9;

# set to "1" if the firstline contains the column headers
my $HasHeader = 1;

# use ../ as lib location
use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin) . "/Kernel/cpan-lib";
use strict;
use warnings;

use Getopt::Std;
use Text::CSV;

use Kernel::Config;
use Kernel::System::Log;
use Kernel::System::Main;
use Kernel::System::Time;
use Kernel::System::DB;
#use Kernel::System::User;
use Kernel::System::CustomerUser;

# common objects
my %CommonObject = ();
$CommonObject{ConfigObject} = Kernel::Config->new();
$CommonObject{EncodeObject} = Kernel::System::Encode->new(%CommonObject);
$CommonObject{LogObject}    = Kernel::System::Log->new(
    LogPrefix => 'test.pl',
    %CommonObject
);
$CommonObject{MainObject} = Kernel::System::Main->new(%CommonObject);
$CommonObject{TimeObject} = Kernel::System::Time->new(%CommonObject);
$CommonObject{DBObject}   = Kernel::System::DB->new(%CommonObject);
#$CommonObject{UserObject} = Kernel::System::User->new(%CommonObject);
$CommonObject{DBObject} = Kernel::System::DB->new(%CommonObject);
$CommonObject{CustomerUserObject} = Kernel::System::CustomerUser->new(%CommonObject);

# get options
my %Opts = ();
getopt( 's', \%Opts );
my $End = "\n";
if ( !$Opts{s} ) {
    die "Need -s <CSV_FILE>\n";
}

my $Lang = $CommonObject{ConfigObject}->Get('DefaultLanguage') || 'de';

my $csv = Text::CSV->new({
    sep_char => ',', 
    eol => "\n",
    binary => 1,
}) or die "Cannot use CSV: ".Text::CSV->error_diag ();

# read csv file
open (my $fh, "< $Opts{'s'}") || die "Can't read $Opts{'s'}: $!";

my $Counter = 0;
while(my $Row = $csv->getline( $fh ) ) {

    next if $Counter++ == 0 and $HasHeader;

    my @Line = @{$Row};

    # check if user extsis
    #my %UserData = $CommonObject{UserObject}->GetUserData( User => $Line[ $Fields{UserLogin} ] );
    my %CustomerUserData = $CommonObject{CustomerUserObject}->CustomerUserDataGet(User => $Line[$Fields{login}]);


    # if there is no pw in the csv list, gererate one
    if( !$Line[$Fields{pw}]) {
        $Line[$Fields{pw}] = $CommonObject{CustomerUserObject}->GenerateRandomPassword();
    }

    # update user
    if( %CustomerUserData) {
        print "Updating customer user '$Line[$Fields{login}]' with password '$Line[$Fields{pw}]'\n";
$CommonObject{CustomerUserObject}->CustomerUserUpdate(
         Source        => $Line[$Fields{customer_id}] || '', # CustomerUser source config
         ID            => $Line[$Fields{login}], # current user login
         UserLogin     => $Line[$Fields{loginnew}],       # new user login
         UserFirstname => $Line[$Fields{firstname}],
         UserLastname  => $Line[$Fields{lastname}],
         UserPassword  => $Line[$Fields{pw}],    # not required
         UserEmail     => $Line[$Fields{email}],
         UserCustomerID => $Line[$Fields{customer_id}] || '',
         UserSalutation => $Line[$Fields{salutation}] || '',
         UserComment => $Line[$Fields{comment}] || '',
         ValidID       => $Line[$Fields{gueltig}],
         UserID        => 123,
    );
     }

    # add user
    else {
         print "Add customer_user '$Line[$Fields{login}]' ($Line[$Fields{firstname}],$Line[$Fields{lastname}]) with password '$Line[$Fields{pw}]'\n";
        $CustomerUserData{UserID} = $CommonObject{CustomerUserObject}->CustomerUserAdd(
            UserSalutation => $Line[$Fields{salutation}] || '',
            UserFirstname => $Line[$Fields{firstname}],
            UserLastname => $Line[$Fields{lastname}],
            UserLogin => $Line[$Fields{loginnew}],
            UserPassword => $Line[$Fields{pw}],
            UserEmail => $Line[$Fields{email}],
            CustomerID => $Line[$Fields{customer_id}] || '',
            UserCustomerID => $Line[$Fields{customer_id}] || '',
            UserComment => $Line[$Fields{comment}] || '',
            ValidID => $Line[$Fields{gueltig}],
            UserID => 1,
        );
    }
}

$csv->status or die "error: ".$csv->error_diag . "(" . $csv->error_input . ")";
Perl / Znuny development: http://perl-services.de
Free Znuny add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de
deinmam
Znuny newbie
Posts: 19
Joined: 24 Oct 2012, 15:04
Znuny Version: 3.1.11

Re: Outlook Kontakte importieren ins OTRS als Kunde

Post by deinmam »

Danke für deine Hilfe reneeb. Leider besteht das Problem immernoch, nur dass das Script nicht so lange braucht, bis es durchgearbeitet ist. :(
reneeb wrote:Das Problem ist hier, dass Zeilenumbrüche in den Nutzdaten vorkommt und das Skript damit nicht umgehen kann. CSV-Daten zu parsen ist nicht ganz trivial. Ich habe das Skript mal angepasst, so dass es das Modul Text::CSV verwendet.

Code: Select all

#/usrl/bin/perl.exe -w
# --
# synccustomer_csv2otrs.pl - sync csv user list or otrs
# Copyright (C) 2012 Perl-Services.de, http://perl-services.de
# --
# $Id: synccustomer_csv2otrs.pl,v 1.10 2008/06/09 14:52:11 tr Exp $
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# --

# config options / csv file - column 0-...
my %Fields = ();
$Fields{login} = 0;
$Fields{email} = 1;
$Fields{customer_id} = 2;
$Fields{pw} = 3;
$Fields{salutation} = 4;
$Fields{firstname} = 5;
$Fields{lastname} = 6;
$Fields{comment} = 7;
$Fields{loginnew} = 8;
$Fields{gueltig} = 9;

# set to "1" if the firstline contains the column headers
my $HasHeader = 1;

# use ../ as lib location
use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin) . "/Kernel/cpan-lib";
use strict;
use warnings;

use Getopt::Std;
use Text::CSV;

use Kernel::Config;
use Kernel::System::Log;
use Kernel::System::Main;
use Kernel::System::Time;
use Kernel::System::DB;
#use Kernel::System::User;
use Kernel::System::CustomerUser;

# common objects
my %CommonObject = ();
$CommonObject{ConfigObject} = Kernel::Config->new();
$CommonObject{EncodeObject} = Kernel::System::Encode->new(%CommonObject);
$CommonObject{LogObject}    = Kernel::System::Log->new(
    LogPrefix => 'test.pl',
    %CommonObject
);
$CommonObject{MainObject} = Kernel::System::Main->new(%CommonObject);
$CommonObject{TimeObject} = Kernel::System::Time->new(%CommonObject);
$CommonObject{DBObject}   = Kernel::System::DB->new(%CommonObject);
#$CommonObject{UserObject} = Kernel::System::User->new(%CommonObject);
$CommonObject{DBObject} = Kernel::System::DB->new(%CommonObject);
$CommonObject{CustomerUserObject} = Kernel::System::CustomerUser->new(%CommonObject);

# get options
my %Opts = ();
getopt( 's', \%Opts );
my $End = "\n";
if ( !$Opts{s} ) {
    die "Need -s <CSV_FILE>\n";
}

my $Lang = $CommonObject{ConfigObject}->Get('DefaultLanguage') || 'de';

my $csv = Text::CSV->new({
    sep_char => ',', 
    eol => "\n",
    binary => 1,
}) or die "Cannot use CSV: ".Text::CSV->error_diag ();

# read csv file
open (my $fh, "< $Opts{'s'}") || die "Can't read $Opts{'s'}: $!";

my $Counter = 0;
while(my $Row = $csv->getline( $fh ) ) {

    next if $Counter++ == 0 and $HasHeader;

    my @Line = @{$Row};

    # check if user extsis
    #my %UserData = $CommonObject{UserObject}->GetUserData( User => $Line[ $Fields{UserLogin} ] );
    my %CustomerUserData = $CommonObject{CustomerUserObject}->CustomerUserDataGet(User => $Line[$Fields{login}]);


    # if there is no pw in the csv list, gererate one
    if( !$Line[$Fields{pw}]) {
        $Line[$Fields{pw}] = $CommonObject{CustomerUserObject}->GenerateRandomPassword();
    }

    # update user
    if( %CustomerUserData) {
        print "Updating customer user '$Line[$Fields{login}]' with password '$Line[$Fields{pw}]'\n";
$CommonObject{CustomerUserObject}->CustomerUserUpdate(
         Source        => $Line[$Fields{customer_id}] || '', # CustomerUser source config
         ID            => $Line[$Fields{login}], # current user login
         UserLogin     => $Line[$Fields{loginnew}],       # new user login
         UserFirstname => $Line[$Fields{firstname}],
         UserLastname  => $Line[$Fields{lastname}],
         UserPassword  => $Line[$Fields{pw}],    # not required
         UserEmail     => $Line[$Fields{email}],
         UserCustomerID => $Line[$Fields{customer_id}] || '',
         UserSalutation => $Line[$Fields{salutation}] || '',
         UserComment => $Line[$Fields{comment}] || '',
         ValidID       => $Line[$Fields{gueltig}],
         UserID        => 123,
    );
     }

    # add user
    else {
         print "Add customer_user '$Line[$Fields{login}]' ($Line[$Fields{firstname}],$Line[$Fields{lastname}]) with password '$Line[$Fields{pw}]'\n";
        $CustomerUserData{UserID} = $CommonObject{CustomerUserObject}->CustomerUserAdd(
            UserSalutation => $Line[$Fields{salutation}] || '',
            UserFirstname => $Line[$Fields{firstname}],
            UserLastname => $Line[$Fields{lastname}],
            UserLogin => $Line[$Fields{loginnew}],
            UserPassword => $Line[$Fields{pw}],
            UserEmail => $Line[$Fields{email}],
            CustomerID => $Line[$Fields{customer_id}] || '',
            UserCustomerID => $Line[$Fields{customer_id}] || '',
            UserComment => $Line[$Fields{comment}] || '',
            ValidID => $Line[$Fields{gueltig}],
            UserID => 1,
        );
    }
}

$csv->status or die "error: ".$csv->error_diag . "(" . $csv->error_input . ")";
Locked