Posts tagged ‘Powershell’

Creating Powerpoint Decks with Powershell

A couple days ago I would have been happy to have an automated way of creating Powerpoint slides (Powerpoint was not my choice anyway) now I found one with the help of the Scripting Guy and StackOverflow. The following code just opens Powerpoint and adds a couple slides, each with a different layout. I am not sure (resp. too lazy to check) how many layouts there are but it must be more than 30.

Clear-Host
Add-type -AssemblyName office
$application = New-Object -ComObject powerpoint.application
$application.visible = [Microsoft.Office.Core.MsoTriState]::msoTrue
$presentation = $application.Presentations.add()
 
$slide = $Presentation.Slides.Add($presentation.Slides.Count + 1, 1) # title slide
$slide = $Presentation.Slides.Add($presentation.Slides.Count + 1, 2) # slide title and text
$slide = $Presentation.Slides.Add($presentation.Slides.Count + 1, 3)
$slide = $Presentation.Slides.Add($presentation.Slides.Count + 1, 4)
$slide = $Presentation.Slides.Add($presentation.Slides.Count + 1, 5)
$slide = $Presentation.Slides.Add($presentation.Slides.Count + 1, 6)
$slide = $Presentation.Slides.Add($presentation.Slides.Count + 1, 7)
$slide = $Presentation.Slides.Add($presentation.Slides.Count + 1, 8)
$slide = $Presentation.Slides.Add($presentation.Slides.Count + 1, 9)
$slide = $Presentation.Slides.Add($presentation.Slides.Count + 1, 10)
$slide = $Presentation.Slides.Add($presentation.Slides.Count + 1, 11) # just slide title
$slide.Shapes.title.TextFrame.TextRange.Text = "This is the slide title"
$slide = $Presentation.Slides.Add($presentation.Slides.Count + 1, 12) # blank

Uwe

Uwe Ziegenhagen has been working with LaTeX for more than a decade. Besides TeX/LaTeX he likes to work with Python, Rasberry/Arduino and his digital camera.

More Posts - Website

Finding truly empty folders with Powershell

The easiest approach to find empty folders with Powershell could be:

(gci C:\Scripts -r | ? {$_.PSIsContainer -eq $True}) | ? {$_.GetFiles().Count -eq 0} | select FullName

however this just finds the folders which have no files in them. To find truly empty folders – the one which also have no subfolders – use the following:

(gci C:\Scripts -r | ? {$_.PSIsContainer -eq $True}) | ?{$_.GetFileSystemInfos().Count -eq 0} | select FullName

Source: http://superuser.com/questions/321231/how-to-find-empty-directories-in-windows-using-a-powershell-script

Uwe

Uwe Ziegenhagen has been working with LaTeX for more than a decade. Besides TeX/LaTeX he likes to work with Python, Rasberry/Arduino and his digital camera.

More Posts - Website

Using Powershell to create MD5 files

This entry is part 2 of 4 in the series MD 5 Calculation

Following up on an article a few days ago here’s a script that creates a MD5 file.

# Uwe Ziegenhagen, 12.11.2014
# Reference: http://onlinemd5.com/
 
### Step A: Get the name of the file
# http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/01/hey-scripting-guy-september-1.aspx
Function Get-FileName($initialDirectory)
{  
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
} #end function Get-FileName
 
# *** Entry Point to Script ***
$file =  Get-FileName -initialDirectory "C:\"
# write-host "file: " $file
 
### Step B: Create a file handle from this string and calculate the MD5 sum
$filehandle = Get-ChildItem $file
 
write-host "Filename: " $filehandle.Name
 
# http://stackoverflow.com/questions/10521061/how-to-get-an-md5-checksum-in-powershell
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($filehandle))).ToLower().Replace("-","")
 
write-host "Calculated Hashsum: "  $hash
 
### Step C: Write the MD5 sum from the MD5 sum file matching the other filename
$hash | out-file -FilePath $($filehandle.DirectoryName + "\" + $filehandle.BaseName + ".md5")

Uwe

Uwe Ziegenhagen has been working with LaTeX for more than a decade. Besides TeX/LaTeX he likes to work with Python, Rasberry/Arduino and his digital camera.

More Posts - Website

Using Powershell to calculate/compare MD5 hashes

This entry is part 1 of 4 in the series MD 5 Calculation

Here’s some code to a) calculate a MD5 hash for a given file and b) to compare the hash with a MD5 provided in an external file. The scripts expects the MD5 file to have the same name, but MD5 extension. Credit goes to the friendly souls providing the original code, I just copied and pasted everything together.

The comparison is just visual, both MD5 hashes — the calculated and the provided one — are printed on the screen, it’s easily to extend it for an automated comparison.

# Reference: http://onlinemd5.com/
 
### Step A: Get the name of the file
# http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/01/hey-scripting-guy-september-1.aspx
Function Get-FileName($initialDirectory)
{  
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null
 
 
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
} #end function Get-FileName
 
# *** Entry Point to Script ***
$file =  Get-FileName -initialDirectory "H:\MD5"
 
### Step B: Create a file handle from this string and calculate the MD5 sum
 
$filehandle = Get-ChildItem $file
 
# write-host "file: " $file
# write-host "DirectoryName: " $filehandle.DirectoryName
write-host "Name: " $filehandle.Name
# write-host "pure Name: " $filehandle.BaseName
 
# http://stackoverflow.com/questions/10521061/how-to-get-an-md5-checksum-in-powershell
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($filehandle))).ToLower().Replace("-","")
 
write-host "Calculated Hashsum: "  $hash
 
### Step C: Get the MD5 sum from the MD5 sum file matching the other filename
 
$md5handle =  Get-ChildItem $($filehandle.DirectoryName + "\" + $filehandle.BaseName + ".md5")
$md5sum = [IO.File]::ReadAllText($md5handle)
 
write-host "Provided MD5        " $md5sum

Uwe

Uwe Ziegenhagen has been working with LaTeX for more than a decade. Besides TeX/LaTeX he likes to work with Python, Rasberry/Arduino and his digital camera.

More Posts - Website

Dateien verschieben mit Powershell

Ausgangssituation: Diverse Unterverzeichnisse mit Dateien, die alle ins darüberliegende Verzeichnis verschoben werden sollen. Mit dem Windows Explorer dauert es Stunden, selbst mit Total Commander dauert es zu lange. Gut, dass ein paar Zeilen Powershell das erledigen:

$path =  "D:\files\"
cd $path
 
$files = gci | ? {$_.PSisContainer -eq $true} 
 
foreach ($i in $files){
	Move-Item $i\*.* $path
}

Uwe

Uwe Ziegenhagen has been working with LaTeX for more than a decade. Besides TeX/LaTeX he likes to work with Python, Rasberry/Arduino and his digital camera.

More Posts - Website

Mit Powershell das Active Directory abfragen

Die heutige Aufgabe in Sachen Powershell war, diverse Informationen zu ACLs (Access Controll Lists) aus dem AD auszulesen. Diverse Personen wie die Zertifizierer der Gruppe sind in Feldern wie „Mail“ und „Info“ abgelegt, diese sollen in eine Excel-Liste exportiert werden.

Die folgenden Artikel waren hilfreich, um den Code zusammenzubauen:

Nachtrag vom 09.01.2013: Der unten stehende Code hat nur 1000 Zeilen abgefragt, durch das Einfügen von $searcher.pagesize=1000 ist diese Grenze aufgehoben.

# Uwe Ziegenhagen, 21.12.2012
# Path for the CSV file to be written to
 
$outputpath = "d\groups.csv"
 
# some AD stuff
$root = [ADSI]''
# initialize the AD searcher
$searcher = new-object System.DirectoryServices.DirectorySearcher($root)
# which groups shall be returned
$searcher.filter = "(&(objectClass=group) (CN=some-group-name*))"
$searcher.pagesize=1000
 
# need to add the variables that shall appear in the result
$searcher.PropertiesToLoad.Add("cn");
 
$searcher.PropertiesToLoad.Add("description");
$searcher.PropertiesToLoad.Add("mail");
$searcher.PropertiesToLoad.Add("info");
 
# call the finder
$adfind = $searcher.findall()
 
# first line of the output file
"Group`tDescription`trole1`trole21`trole22`trole23"  | out-file $outputpath -Width 300
 
foreach ($i in $adfind ) {
   # get the properties from the search result
   $name = "" + $i.properties.item("cn")
   $description = "" + $i.properties.item("description")
   $role1 = "" + $i.properties.item("info")
 
   # some entries include garbage like 'contact:' instead of the pure name
   # to get rid of whitespace I then trim everything
   $role1= $role1.replace("contact:","").trim()
 
   $role2 = "" + $i.properties.item("mail")
   # there are up to 3 role2s, split this entry into different columns
   $role2 = $role2.replace(",","`t")
 
   # create line
   $line = $name + "`t" + $description + "`t" + $role1  + "`t" + $role2 
   # replace carriage-returns
   $line.replace("`n","");
   # write line to file
   $line | out-file -append $outputpath -Width 300
}

Uwe

Uwe Ziegenhagen has been working with LaTeX for more than a decade. Besides TeX/LaTeX he likes to work with Python, Rasberry/Arduino and his digital camera.

More Posts - Website

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 has been working with LaTeX for more than a decade. Besides TeX/LaTeX he likes to work with Python, Rasberry/Arduino and his digital camera.

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 has been working with LaTeX for more than a decade. Besides TeX/LaTeX he likes to work with Python, Rasberry/Arduino and his digital camera.

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 has been working with LaTeX for more than a decade. Besides TeX/LaTeX he likes to work with Python, Rasberry/Arduino and his digital camera.

More Posts - Website

Powershell Ressourcen

Bei der täglichen Arbeit hilft mir die Microsoft Powershell sehr. Hier ein paar Links zu den wichtigsten Büchern & Webseiten:

Bücher

  • Schnelleinstieg in die Windows Powershell von Andy Oakley
  • Windows PowerShell Crashkurs von Peter Monadjemi
  • Windows PowerShell für Administratoren und Power-User von Helma Spona
  • Windows PowerShell von Holger Schwichtenberg

Online (Microsoft)

Online

Uwe

Uwe Ziegenhagen has been working with LaTeX for more than a decade. Besides TeX/LaTeX he likes to work with Python, Rasberry/Arduino and his digital camera.

More Posts - Website