2010-02-05, 09:06
Nachdem das Erstellen der DLL und der Test erfolgreich waren, können wir jetzt die DLL auch aus Powershell heraus aufrufen:
[Reflection.Assembly]::LoadFile("C:\simpledll.dll")
[de.uweziegenhagen.TextCompare]::levenshtein("Andrea", "Andria") |
[Reflection.Assembly]::LoadFile("C:\simpledll.dll")
[de.uweziegenhagen.TextCompare]::levenshtein("Andrea", "Andria")
Die Ausgabe bringt zuerst ein paar allgemeine Informationen zur geladenen DLL, dann die Ausgabe der Funktion.
GAC Version Location
--- ------- --------
False v2.0.50727 C:\Users\Uwe\Desktop\csharp-dll\simpledll\simpledll\bin\Re...
1
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
2010-02-05, 08:54
Als nächstes schreibe ich eine kleine Konsolenanwendung, mit deren Hilfe ich die DLL testen kann. Als Projekttyp nutze ich daher „Konsolenanwendung“. Im Projektmappenexplorer wird unter Verweisen ein neuer Verweis auf die DLL angelegt, dann folgt die Eingabe des Quellcodes:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using de.uweziegenhagen;
namespace Levenshtein_Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(TextCompare.levenshtein("uwe", "andreas"));
Console.ReadLine();
}
}
} |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using de.uweziegenhagen;
namespace Levenshtein_Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(TextCompare.levenshtein("uwe", "andreas"));
Console.ReadLine();
}
}
}
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
2010-02-05, 08:33
Hier eine kurze Anleitung, wie man mit C# eigene DLLs erstellt und diese von Powershell aus benutzt. Ich nutze die kostenlose Version von Visual Studio, Visual C# Express 2008. Als umzusetzender Algorithmus kommt Levenshtein zum Einsatz, über den ich öfter schon geschrieben habe, die Funktion stammt aus der englischen Wikipedia.
In VS lege ich ein neues Projekt vom Typ Klassenbibliothek an, als Namespace wähle ich „de.uweziegenhagen“. Das Projekt habe ich unter dem Namen „simpledll“ abgespeichert, die „Class1“ Datei in TextCompare.cs umbenannt. Hier der Quelltext für die statische Klasse, wichtig ist, dass die Levenshtein Funktion „public static“ ist, nicht „private“ wie in der Wikiedia.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace de.uweziegenhagen
{
public static class TextCompare
{
public static Int32 levenshtein(String a, String b)
{ // http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#C.23
if (string.IsNullOrEmpty(a))
{
if (!string.IsNullOrEmpty(b))
{
return b.Length;
}
return 0;
}
if (string.IsNullOrEmpty(b))
{
if (!string.IsNullOrEmpty(a))
{
return a.Length;
}
return 0;
}
Int32 cost;
Int32[,] d = new int[a.Length + 1, b.Length + 1];
Int32 min1;
Int32 min2;
Int32 min3;
for (Int32 i = 0; i <= d.GetUpperBound(0); i += 1)
{
d[i, 0] = i;
}
for (Int32 i = 0; i <= d.GetUpperBound(1); i += 1)
{
d[0, i] = i;
}
for (Int32 i = 1; i <= d.GetUpperBound(0); i += 1)
{
for (Int32 j = 1; j <= d.GetUpperBound(1); j += 1)
{
cost = Convert.ToInt32(!(a[i - 1] == b[j - 1]));
min1 = d[i - 1, j] + 1;
min2 = d[i, j - 1] + 1;
min3 = d[i - 1, j - 1] + cost;
d[i, j] = Math.Min(Math.Min(min1, min2), min3);
}
}
return d[d.GetUpperBound(0), d.GetUpperBound(1)];
}
}
} |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace de.uweziegenhagen
{
public static class TextCompare
{
public static Int32 levenshtein(String a, String b)
{ // http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#C.23
if (string.IsNullOrEmpty(a))
{
if (!string.IsNullOrEmpty(b))
{
return b.Length;
}
return 0;
}
if (string.IsNullOrEmpty(b))
{
if (!string.IsNullOrEmpty(a))
{
return a.Length;
}
return 0;
}
Int32 cost;
Int32[,] d = new int[a.Length + 1, b.Length + 1];
Int32 min1;
Int32 min2;
Int32 min3;
for (Int32 i = 0; i <= d.GetUpperBound(0); i += 1)
{
d[i, 0] = i;
}
for (Int32 i = 0; i <= d.GetUpperBound(1); i += 1)
{
d[0, i] = i;
}
for (Int32 i = 1; i <= d.GetUpperBound(0); i += 1)
{
for (Int32 j = 1; j <= d.GetUpperBound(1); j += 1)
{
cost = Convert.ToInt32(!(a[i - 1] == b[j - 1]));
min1 = d[i - 1, j] + 1;
min2 = d[i, j - 1] + 1;
min3 = d[i - 1, j - 1] + cost;
d[i, j] = Math.Min(Math.Min(min1, min2), min3);
}
}
return d[d.GetUpperBound(0), d.GetUpperBound(1)];
}
}
}
Das Übersetzen der DLL sollte problemlos funktionieren, die fertige DLL sollte im PRojektverzeichnis unter /bin/release liegen.
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
2010-01-31, 07:45
In der aktuellen c’t wird in einem Artikel beschrieben, wie man mittels Powershell die Rechnungen von O2 abholen kann. Hier der Link zum Skript: https://www.heise.de/ct/projekte/machmit/webautomatisieren/wiki/SkriptUebersicht.
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
Schlagwörter:
Powershell Category:
Powershell |
Kommentare deaktiviert für O2 Rechnungen mit Powershell herunterladen
2010-01-25, 22:02
Auf http://www.powershell.nu/2009/09/04/scripting-mp3-metadata-through-powershell/ habe ich heute einen interessanten Artikel gefunden, der das Auslesen von MP3-Tags mit Powershell beschreibt.
Edit: Hier findet man die fertige DLL: http://download.banshee.fm/taglib-sharp/.
$TagLib = "C:\taglib\Libraries\taglib-sharp.dll"
[System.Reflection.Assembly]::LoadFile($TagLib)
$Media = [TagLib.File]::Create("C:/nemo.mp3")
$Media
$Media.Tag |
$TagLib = "C:\taglib\Libraries\taglib-sharp.dll"
[System.Reflection.Assembly]::LoadFile($TagLib)
$Media = [TagLib.File]::Create("C:/nemo.mp3")
$Media
$Media.Tag
GAC Version Location
--- ------- --------
False v2.0.50727 C:\taglib\Libraries\taglib-sharp.dll
Tag : TagLib.NonContainer.Tag
Properties : TagLib.Properties
TagTypesOnDisk : Id3v1, Id3v2
TagTypes : Id3v1, Id3v2
Name : C:/nemo.mp3
MimeType : taglib/mp3
Tell : 0
Length : 0
InvariantStartPosition : 644
InvariantEndPosition : 6508416
Mode : Closed
GAC Version Location
--- ------- --------
False v2.0.50727 C:\taglib\Libraries\taglib-sharp.dll
Tag : TagLib.NonContainer.Tag
Properties : TagLib.Properties
TagTypesOnDisk : Id3v1, Id3v2
TagTypes : Id3v1, Id3v2
Name : C:/nemo.mp3
MimeType : taglib/mp3
Tell : 0
Length : 0
InvariantStartPosition : 644
InvariantEndPosition : 6508416
Mode : Closed
StartTag : TagLib.NonContainer.StartTag
EndTag : TagLib.NonContainer.EndTag
TagTypes : Id3v1, Id3v2
Tags : {, }
Title : Nemo
Performers : {Nightwish}
PerformersSort : {}
AlbumArtistsSort : {}
AlbumArtists : {}
Composers : {}
ComposersSort : {}
TitleSort :
AlbumSort :
Album : Once
Comment :
Genres : {Other}
Year : 2004
Track : 0
TrackCount : 0
Disc : 0
DiscCount : 0
Lyrics :
Grouping :
BeatsPerMinute : 0
Conductor :
Copyright :
MusicBrainzArtistId :
MusicBrainzReleaseId :
MusicBrainzReleaseArtistId :
MusicBrainzTrackId :
MusicBrainzDiscId :
MusicIpId :
AmazonId :
MusicBrainzReleaseStatus :
MusicBrainzReleaseType :
MusicBrainzReleaseCountry :
Pictures : {}
IsEmpty : False
Artists : {Nightwish}
FirstArtist : Nightwish
FirstAlbumArtist :
FirstAlbumArtistSort :
FirstPerformer : Nightwish
FirstPerformerSort :
FirstComposerSort :
FirstComposer :
FirstGenre : Other
JoinedArtists : Nightwish
JoinedAlbumArtists :
JoinedPerformers : Nightwish
JoinedPerformersSort :
JoinedComposers :
JoinedGenres : Other
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
2010-01-09, 15:59
Kürzlich musste ich eine größere Menge Ordner erstellen, die einen bestimmten Aufbau hatten. Zuerst habe ich einen Musterordner erstellt, den dann per Powershell dupliziert. Die Namen der Ordner habe ich aus einer CSV Datei genommen.
Get-Content foldernames.csv | Foreach-Object {
xcopy c:\template F:\target\$_ /E /Y /I
} |
Get-Content foldernames.csv | Foreach-Object {
xcopy c:\template F:\target\$_ /E /Y /I
}
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
2010-01-09, 15:42
Momentan bin ich dabei, Skripte zu schreiben, die mir ein Auslesen der Rechte einer Fileserver-Struktur ermöglichen. Neben den exzellenten Quest AD-Commandlets (siehe http://www.computerperformance.co.uk/powershell/powershell_quest_qad.htm ist es vor allem der intensive Gebrauch von get-acl.
Auf http://www.terminal23.net/2007/04/powershell_auditing_permissions.html habe ich ein Skript gefunden, das nicht nur das ansatzweise macht, was ich brauche, sondern auch so, dass ich verstehe was passiert. 🙂
Hier noch ein paar Links zu dem Thema:
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
Schlagwörter:
Powershell Category:
Powershell |
Kommentare deaktiviert für Mit Powershell Verzeichnisrechte verwalten
2010-01-02, 16:21
Die folgenden Parameter lassen sich nutzen, um die zu matchenden Zeichen besser einzugrenzen:
[ABC] |
nur die Zeichen A, B und C |
[A-F] |
nur die Zeichen A bis F |
[^ABC] |
alle Zeichen außer A, B und C |
. |
jedes Zeichen |
\w |
Buchstaben |
\W |
Nicht-Buchstaben |
\d |
Ziffern |
\D |
Nicht-Ziffern |
\s |
Whitespace Zeichen wie Leerzeichen oder Tabulator |
\S |
Non-Whitespace Zeichen |
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
Schlagwörter:
Powershell Category:
Powershell |
Kommentare deaktiviert für Reguläre Ausdrücke mit Powershell V: Zeichenklassen
2010-01-02, 10:14
In Version 2 enthält die Powershell ein Commandlet zum einfachen Versenden von E-Mails. Hier ein Beispiel:
send-mailmessage -From admin@localhost.xyz
-To user@someotherdomain.com
-Subject "Hallo Welt"
-Body "Testmail von Uwe"
-Smtpserver smtpserver.somedomain.com |
send-mailmessage -From admin@localhost.xyz
-To user@someotherdomain.com
-Subject "Hallo Welt"
-Body "Testmail von Uwe"
-Smtpserver smtpserver.somedomain.com
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
Schlagwörter:
Powershell Category:
Powershell |
Kommentare deaktiviert für E-Mails versenden mit Powershell 2
2010-01-01, 18:19
Wenn man nicht das gesamte Muster ‚hart‘ ersetzen möchte, sondern in Abhängigkeit des gematchten Strings, dann bieten sich Gruppen an.
In folgendem Beispiel suche ich alle <h1>-Überschriften und ersetze diese durch <h2>-Überschriften. Dabei wird nicht die Powershell-eigene
replace
-Funktion benutzt, sondern die entsprechende .Net Klasse. Das, was zwischen den beiden Tags steht, ist über $1
zugänglich.
$string = "<h1>Hallo</h1>"
$pattern = "<h1>(\w+)</h1>"
[regex]::Replace($string,$pattern,'<h2>$1</h2>') |
$string = "<h1>Hallo</h1>"
$pattern = "<h1>(\w+)</h1>"
[regex]::Replace($string,$pattern,'<h2>$1</h2>')
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
Schlagwörter:
Powershell Category:
Powershell |
Kommentare deaktiviert für Reguläre Ausdrücke mit Powershell: Untergruppen matchen