Archive for the ‘Powershell’ Category.

Reports erzeugen mit Powershell

Heute bekam ich von einer Leserin meines Blogs folgendes Powershell Skript zugesandt, das alle Bilder größer 1MB sucht in in einer HTML Datei auflistet, die dann verschickt wird.

Vielen Dank an Andrea!

"
$a = $a + "BODY{background-color:#ffffff;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:grey}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#ffffff}"
$a = $a + ""

gci "C:\Users\Uwe\" -recurse -include *.jpg,*.bmp,*.png | Where-Object {$_.Length -gt 1000KB} |
? { $_.PSisContainer -eq $false} |
Sort-Object length -descending | Select-Object Name, @{Name="Kbytes";Expression={ "{0:N0}" -f ($_.Length / 1Kb) }}, DirectoryName, CreationTime | ConvertTo-HTML -head $a -body "

Grosse Bilder

" | out-file "C:\report.htm" #Send Email $msg = $null $smtpServer = "0.0.0.0" $workpath = “\\Server\Share\Verzeichnis” $file = “$workpath\report.htm” $msg = new-object Net.Mail.MailMessage $att = new-object Net.Mail.Attachment($file) $smtp = new-object Net.Mail.SmtpClient($smtpServer) $msg.From = "sender@domain.com" $msg.To.Add("empfaenger@domain.com") #$msg.To.Add("empfaenger2@domain.com") $msg.Subject = "Bilder Report" $msg.IsBodyHTML = $true $html = "Report der Bilder" $msg.Body = $html $msg.Attachments.Add($att) $smtp.Send($msg)

Uwe

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

Regexp Matching mit Powershell

Hier ein kleines Beispiel, wie man mit Powershell reguläre Ausdrücke prüft. Gesucht werden alle Ordner, deren Name aus dem vierstelligen Jahresdatum besteht. Die Zeile, eingebettet in ein Skript hat heute in der Firma einer Kollegin stundenlange monotone Arbeit erspart.

$jahre = gci | Where-Object {$_.name -match '^[0-9]{4}$'}  | ? {$_.PSisContainer -eq $true}

Uwe

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

Dateien suchen per Powershell

Unter http://www.networknet.nl/apps/wp/published/powershell-delete-files-older-than-x-days habe ich ein Skript gefunden, das Dateien anhand des letzten Änderungsdatums findet. Ohne die Kommentare und angepasst auf meine Bedürfnisse wird das:

$Now = Get-Date
$Days = 1
$TargetFolder = "C:/Users/Uwe"
$Extension = "*.pdf"
 
$LastWrite = $Now.AddDays(-$Days)
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}
foreach ($File in $Files)
{
$file.fullname
}

Uwe

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

Mit Powershell alle Word-Dateien eines Verzeichnisses drucken

Hier ein paar einfache Zeilen, wie man mit Powershell alle Word-Dateien eines Verzeichnisse öffnen und drucken kann.

#Source: http://www.vistax64.com/powershell/23311-words-printout-method.html 
$wd = new-object -com word.application
$wd.visible = 1
 
$files = gci f:\abc\*.docx
 
foreach($i in $files){
    $doc = $wd.documents.open($i.fullname)
    $doc.printout()
    $doc.close()
}

Uwe

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

Dateien umbenennen per Powershell

Hier ein kurzes Beispiel, wie man mit Powershell Dateien umbenennen kann. Die Ausgangslage war ein Verzeichnis mit PDF Dateien, die alle das Namensmuster „Dokument-“ hatten.

number;filename
1;mueller
2;meier
3;schulze
 
$files = Import-Csv -Delimiter ";" D:\files.csv
cd M:\
 
foreach ($i in $files){
    Rename-Item ("Testdokument-" + $i.number + ".pdf") ($i.filename +".pdf")     
}

Uwe

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

Neue Ordner anlegen per Powershell und CSV-Datei

Hier ein kurzes Beispiel, wie man mit Powershell Ordner anlegen kann, deren Namen in einer CSV Datei hinterlegt sind. In der originalen CSV Datei gab es zwei Spalten, per Semikolon getrennt: die Zeilennummer und den Namen. In der ersten Zeile der Datei erwartet Powershell den Spaltennamen, die Datei könnte also so aussehen:

number;foldername
1;AAA
2;BBB
3;CCC
 
$folders = Import-Csv -Delimiter ";" D:\folders.csv
cd M:\
 
foreach ($i in $folders){
     New-Item -type directory $i.foldername
}

Uwe

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

Mit Powershell TeX Verzeichnisse aufräumen

Dank des kleinen Powershell-Skripts zur Bestimmung des Skript-Pfades (http://uweziegenhagen.de/?p=2066) können wir uns recht einfach ein Skript schreiben, das alle LaTeX Hilfsdateien löscht.

function Get-ScriptDirectory{
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    Split-Path $Invocation.MyCommand.Path
}
 
$path = (Get-ScriptDirectory)
 
cd $path
 
 
remove-item  *.log 
 
remove-item  *.lot 
 
remove-item  *.lof 
 
remove-item *.toc 
 
remove-item *.gz 
 
remove-item *.aux 
 
remove-item *.nav 
 
remove-item *.out 
 
remove-item *.atfi 
 
remove-item *.vrb 
 
remove-item *.snm 
 
remove-item *.tmp

Uwe

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

Den Pfad des Powershell-Skripts ermitteln

Ich möchte per Mausklick auf eine Powershell-Datei alle LaTeX Dateien in dem entsprechenden Verzeichnis kompilieren. Dazu muss das Skript das Verzeichnis kennen, in dem es liegt. Auf http://leftlobed.wordpress.com/2008/06/04/getting-the-current-script-directory-in-powershell/ habe ich eben eine Lösung gefunden, die mir diesen Pfad ausgibt, zusammen mit dem Messagebox Code ist dies der erste Schritt zum „TeXe alles“ Skript.

function Get-ScriptDirectory{
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value
    Split-Path $Invocation.MyCommand.Path
}
 
$wshshell = new-object -comobject wscript.shell
$Answer = $wshshell.popup((Get-ScriptDirectory),0,"Aktueller Pfad",4)

Uwe

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

Mit Powershell Ordner überwachen

Vor kurzem wurde ich gebeten, bei der Überprüfung von Ordnern im Dateisystem zu helfen. Ziel war die Ermittlung, wann in bestimmten Unterverzeichnissen einer Dateistruktur Dateien hinzugefügt oder gelöscht werden.

Zur Erläuterung: In C:\abc liegen viele gleichartige Ordner, die alle einen „A-def“ Unterordner haben, der wiederum einen Unterordner „2012“ hat. Für alle in diesen „2012“ Ordnern gefundenen Dateien werden der Dateiname und Zeitstempel in eine CSV Datei geschrieben. Diese kann dann einfach und sehr übersichtlich mit einem Excel-Pivot ausgewertet werden.

# Resette die Ausgabe
Clear-History
Clear-Host
# Ausgabepfad der Datei
$outputpath = "c:\FoundFiles.csv"
 
# hier liegen die Ordner
cd c:\abc\
 
# warte x Sekunden bis zum ersten Lauf bzw. zwischen den Läufen.
$waitseconds = 1
# wie oft soll gecheckt werden
$runs = 1
# zähle die Läufe mit
$counter = 0
 
while($counter -le $runs){
 start-sleep -s $waitseconds
 $counter++
 # Zeitstempel
 $timestamp= get-date -uformat "%Y%m%d-%H:%M"
 
 #hole die Liste der Ordner
 $folders = gci
 
 foreach ($i in $folders){
   # prüfe in den "A-def" Ordnern, ob "2012" vorhanden ist 
   cd "$i/A-def"
        if (!(Test-Path -path "2012")){
            # no 2012 folder found
        } else {
            # lese alle Dateien ein
            $files = gci "2012"
            foreach ($j in $files){
                # Schreibe Dateiname und Zeitstempel in die Datei, überschreibe nicht.
                $i.name + "`t" + $timestamp + "`t" + $j.fullname  | out-file $outputpath -append -Width 300
            }
    }
cd c:\abc\
}
}

Uwe

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

Powershell: Ersetzungen in Dateinamen durchführen

Hier ein kurzer Schnipsel, wie man Zeichen in Dateinamen ersetzen kann. Für die tatsächliche Umbenennung ist das „-whatif“ zu entfernen.

$files = gci -Recurse | ? {$_.PSisContainer -eq $false} |  ? {$_.name -match "~"}
 
foreach ($i in $files){
            rni -WhatIf $i.fullname $i.Name.Replace("~","_")
}

Uwe

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