The Scripting Games 2012 are over an I won…

well, I didn’t win exactly – the real winners of the Scripting Games 2012 are Lido Paglia in the Beginner category and Rohn Edwards in the Advanced Category. Congratulations! This was not so easily to accomplish since a few challenges were a little tough and  it was a fierce competion which several hundred submissions for each challenge  I finished somewhere in the upper third part with a lot of room for improvement for next years competition. The best advice I can give to anyone who is not happy with his results or wants to take part next year for the first time: Read the challenge carefully, ask questions and keep your scripts simple and well structured. Simple solutions are always the best ones. And don’t forget the comments.

But why was I thinking that I won? Simple, since the Scripting Games follow the “Olympic idea” everyone who submitted 10 scripts is a winner. She or he will probably have learnt a lot about PowerShell and it was fun and also challenging. Writing scripts day in and day out is one thing but having your own scripts jugded by the best PowerShell experts in the world is a big thrill because it shows how good you really are.

I would like to thank everyone who made this event possible (I know that a lot of people put a lot of work, time and passion into making this probably the “biggest scripting event in history) and I am really looking forward to Scripting Games 2013.

Veröffentlicht unter Allgemein | Hinterlasse einen Kommentar

PowerShell Remoting eBook von Don Jones und Tobias Weltner

Auch wenn PowerShell-Remoting in der Praxis einfach zu benutzen ist und auch das Thema “Troubleshooting” in der Hilfe gut beschrieben wird, so manches Mal hat man das Gefühl, dass einem Microsoft nicht alle Details verraten hat. Dieses Gefühl tritt spätestens dann ein, wenn ein PS-EnableRemoting wider Erwarten zu einem Fehler führt, der dessen Fehlermeldung wenig bis gar keine Rückschlüsse auf die Ursache zulässt (sowie wie es in meinen PowerShell-Kursen, die in wechselnden Umgebungen stattfinden, hin und wieder passiert).

Seit kurzem haben die beiden “PowerShell-Superstars” Don Jones und Tobias Weltner ihr Wissen zum Thema PowerShell-Remoting in einem eBook zusammengefasst, das es unter der folgenden Adresse zum Download gibt:

http://www.lulu.com/shop/don-jones-and-tobias-weltner/secrets-of-powershell-remoting/ebook/product-20087080.html

Auch wenn das PDF kostenlos ist, muss man sich zuvor bei Lulu.com registrieren und es “kaufen”. Und auch wenn der Inhalt nicht unbedingt “Geheimnisse” sind, ist es ein absolut lesenswertes Dokument, das jeder, der mit der PowerShell ein solides Remoting auf die Beine stellen möchte, das über ein Invoke-Command { Get-Process } auf einem Remote-Rechner hinausgeht lesen sollte. Danke an die beiden Autoren, dass sich die Zeit genommen haben das Dokument zusammenzustellen.

Veröffentlicht unter Allgemein | Hinterlasse einen Kommentar

I am done with the Scripting Games 2012

Yesterday I submitted my last PowerShell script for the 2012 Scripting Games. So I have completed every single of the advanced events (which took me several hours in total) and now its up to the jury to decide. The last challenge #10 was not such a big challenge but not a no brainer either (or may be the solution I came up with was just too complicated). The toughest challenge was Event # 6 where you had to collect the uptime of number of servers with the resction that if a server had been booted after 8am his uptime had to be taken into account the next day.

It was definitely fun, I learned at lot and I appreciate the effort the organizers put into the whole event. I don’t think that I will win or come even close. Its not becuase I wasn’t able to solve the challenges but in once occasion I didn’t read the description properly and forgot that the script should check if it runs with admin privileges (although I wasn’t aware that you had to to this just to check remotely if a services runs or not) and in another case I accidently submited a script with an error. And some of solutions might have been to complicated.

But whoever wins is a real PowerShell professional. In about two weeks the winner will be announced.

Veröffentlicht unter Allgemein | Hinterlasse einen Kommentar

Große Dateien kopieren

Zugegeben, kein besonders spannendes Thema, aber ein Wichtiges. Die PowerShell bietet als Windows-Shell zwangsläufig gleich eine ganze Palette an Möglichkeiten, um große Dateien (z.B. Images) von A nach B zu kopieren:

>Copy-Item

>XCopy bzw. RoboCopy

>Der BITS-Dienst

Ich persönlich bevorzuge RoboCopy (Robust Copy), da es schnell und einfach in der Handhabung ist. Aus einem PowerShell-Skript heraus bieten die Cmdlets aus dem BitsTransfer-Modul den meisten Komfort, vor allem, da ein Transfer auch dann fortgesetzt wird, wenn der Rechner  zwischenzeitlich heruntergefahren wurde (das wollte ich immer schon einmal testen;). Interessanterweise gibt es bezüglich der Geschwindigkeit offenbar keine allzu großen Unterschiede.

Das habe ich jedenfalls mit einem PowerShell-Skript herausgefunden, dass eine relativ große ISO-Datei von A nach B kopiert. Damit es funktioniert, müssen natürlich die Pfade für die Quell- und die Zieldatei angepasst werden.

Das Skript kopiert ein und diesselbe Datei vier Mal auf verschiedene Art und Weise und misst dabei die Ausführungzeit per Measure-Command. Damit das Ergebnis am Ende gut darstellbar ist, werden die Ergebnis in eine Hashtable eingefügt, die am Ende ausgegeben wird. Eine interessante Technik vollkommen unabhängig vom Thema Dateien kopieren.

<#
.Synopsis
Copy-Vergleich
.Description
Geschwindigkeitsvergleich Copy-Item, XCopy, RoboCopy, BITS
#>
 
$QuellDatei = "C:\Users\Administrator\Downloads\de_office_professional_plus_2010_x64_517145.exe"
$ZielPfad = "\\192.168.2.138\ISOS"
$ZielDatei = "T:\de_office_professional_plus_2010_x64_517145.exe"
 
# $Cred = Get-Credential Admin
 
# Kopieren per Copy-Item
 
$ZeitCopy = @{}
 
$ZeitCopy += @{"Copy"=&{Measure-Command -Expression {
Copy-Item -Path $QuellDatei -Destination T: }}}
 
Write-Warning "Copy-Item-Beispiel ausgeführt."
 
if (Test-Path $QuellDatei)
{
if (Test-Path $ZielDatei)
{
Remove-Item -Path $ZielDatei
}
}
 
# Kopieren per XCopy
 
$ZeitCopy += @{"XCopy"=&{Measure-Command -Expression {
XCopy $QuellDatei T: }}}
 
Write-Warning "XCopy-Item-Beispiel ausgeführt."
 
if (Test-Path $QuellDatei)
{
if (Test-Path $ZielDatei)
{
Remove-Item -Path $ZielDatei
}
}
 
# Kopieren per RoboCopy
 
$ZeitCopy += @{"RoboCopy"=&{Measure-Command -Expression {
RoboCopy $QuellDatei T: }}}
 
Write-Warning "RoboCopy-Item-Beispiel ausgeführt."
 
if (Test-Path $QuellDatei)
{
if (Test-Path $ZielDatei)
{
Remove-Item -Path $ZielDatei
}
}
 
# Kopieren per BITS
 
Import-Module -Name Bitstransfer
 
$ZeitCopy += @{"BITS"=&{Measure-Command -Expression {
Start-BitsTransfer -Source $QuellDatei -Description "Copy-Test" `
-Destination $ZielPfad -DisplayName "Copy-Test" }}}
 
if (Test-Path $QuellDatei)
{
if (Test-Path $ZielDatei)
{
Remove-Item -Path $ZielDatei
}
}
Write-Warning "BITS-Beispiel ausgeführt."
 
if (Test-Path $QuellDatei)
{
if (Test-Path $ZielDatei)
{
Remove-Item -Path $ZielDatei
}
}
 
$ZeitCopy.Keys | Select-Object @{Name="Copy-Variante";Expression={$_}},@{Name="Wert";Expression={$ZeitCopy.$_.ToString()}} | Sort-Object -Property Wert
Veröffentlicht unter Allgemein | Hinterlasse einen Kommentar

Die PowerShell Scripting Games 2012 – ein Aufruf zum Mitmachen

Ein nicht ganz unwichtiger Hinweis gleich vorweg: Die Scripting Games 2012 haben bereits am 3. April begonnen und enden am 13. April. Für eine Teilnahme ist es zwar nicht zu spät, aber da der Abgabetermin für die ersten “Wettbewerbe” schon vorbei ist, kann man sicher nicht mehr die Punktzahl erzielen, die einen in die Nähe des Gesamtsiegs bringen könnte (ich werde 2013 etwas eher auf die Scripting Games hinweisen).

Eine allgemeine Übersicht gibt es hier:

http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/04/the-2012-windows-powershell-scripting-games-all-links-on-one-page.aspx

Doch was sind die Scripting Games überhaupt und warum sollte man sich unbedingt beteiligen? Hier das Wichtigste in Kurzform:

>Die Scripting Games sind eine Art “PowerShell-Wettbewerb”, bei der es darum geht gestellte Aufgaben möglichst “gut” mit den Mitteln der PowerShell 2.0 zu lösen.
>Die Scripting Games werden von den “Scripting Guys” bei Microsoft in Redmond organisiert (dahinter steht in erster Linie ein gewisser Ed Wilson, der zahlreiche Bücher zum Thema Windows Scripting – alle erschienen bei Microsoft Press) geschrieben hat, und der von seiner Ehefrau (aka “Scripting Wife”) unterstützt wird.
>Die Idee der Scripting Games ist, dass alle, die beruflich oder privat (oder beides) mit der PowerShell zu tun haben, anhand von Übungsaufgaben ihr Wissen testen können.
>Die Organisatoren stellen jeden Tag eine Aufgabe (jeweils in der Kategorie “Beginers” und “Advanced”), die innerhalb einer Frist gelöst werden muss. Alle eingereichten Lösungen werden nach verschiedenen Kriterien von einer Jury bewertet. Am Ende gewinnt, wer die meisten Punkte erreicht hat. Die Gewinner erhalten verschiedene Sachpreise (u.a die Teilnahme an der TechEd US).

Bei der Teilnahme geht es nicht nur darum, möglicherweise etwas gewinnen zu können, sondern in erster Linie darum, durch das Lösen der Aufgaben mehr über die PowerShell zu lernen und dabei Spaß zu haben.

Insgesamt sind die Scripting Games eine tolle Sache. Ich kann jedem nur empfehlen mitzumachen. Ich bin selber natürlich (das erste Mal) dabei und werde natürlich darüber berichten wie erfolgreich ich am Ende war (zwei Aufgaben habe ich bereits umgesetzt – ich war nicht nur mit dieser Ankündigung etwas spät dran;)

Veröffentlicht unter Allgemein | Hinterlasse einen Kommentar

The 2012 Scripting Games have started

I am little late but may be this blog entry will give the PowerShell 2012 Scripting Games a tiny amount of extra exposure. Here is the link

http://blogs.technet.com/b/heyscriptingguy/archive/2012/01/30/scripting-guys-announce-the-2012-powershell-scripting-games.aspx

I can really recommend to take the challenge – its a fun way of proving how good you know PowerShell already.

If you are not sure if you are already good enough for taking part you might should try this nice little quiz that “The Scripting Guys” had put together:

http://quizapp.cloudapp.net/default.aspx?quiz=powershell

Veröffentlicht unter Allgemein | Hinterlasse einen Kommentar

Beware of MUI (or how to get a stranged Enable-PSRemoting error fixed)

I am German, I live in Germany, so I am using a Windows Server with a German Language Pack. Make sense, right? But from time to time I come accross a Windows Server with a en-US installation where someone installed a German MUI (Multlingual User Interface). Thats when some problems starts.

Recently I got a “strange” error when executing Enanble-PSRemoting. The command ran through until it hits the part where the Firewall rules are added. The result was a kind of “Unable to check the status of the firewall” error message.

It turns out that after changing the language to “English” it worked.

It also turned out that someone already had reported this as an error:

http://connect.microsoft.com/PowerShell/feedback/details/625463/firewall-state-mui-problem

Veröffentlicht unter Allgemein, Tipps | Hinterlasse einen Kommentar

Get-Hotfix fixed

Get-Hotfix has a minor glitch that probably only occurs on a non US-Windows. If a date cannot be converted into a valid DateTime an exception is probably internally thrown and the InstalledOn property has no value. That’s a little bit annoying.

The fix is to access the date value from the base property and do you own conversion. The following snippet will accomplish this.

Get-Hotfix | Select-Object HotfixID, @{Name="InstallDate";Expression={
         Get-Date -Date ($_.psbase.Properties["InstalledOn"].Value `
            -replace "(\d{1,2})/(\d{1,2})/(\d{4})", '$2.$1.$3') 
}} | Sort-Object -Property InstallDate -Descending | 
Select-Object HotfixID, @{Name="InstallDate";Expression={"{0:d}" -f $_.InstallDate}}
Veröffentlicht unter Tipps | 1 Kommentar

Changing ASCII characters in an xml file

Recently I stumbled upon a small peculiarity that Word exposes when you try to save an XML file as text (actually its a regular word document with xml tags that I saved as a text file). After saving the file I was not able to load it as xml. After a “few hours” (minutes that seems like hours) I found out with the help of a hex editor that Word uses a special ASCII character 147 instead of 44 for the quotation mark “.

To make the story really short, I used PowerShell (of course) to change all ASCII codes 147 to 44:

(Get-Content Test.xml -Encoding byte) -replace "147", "34" | foreach { [Byte]
$_ } | Set-Content Test.xml -Encoding Byte

The “trick” is not only to use the Encoding parameter with Get-Content but also to convert each character back to [Byte] so that it can be saved again as byte with Set-Content.

Veröffentlicht unter Tipps | 1 Kommentar

Getting the time from an Internet Time Server

Like probably many others I accepted the generous offer by n/Software right before Christmas that included a free license of their NetCmdlets V3. I really like their Cmdlets – especially copying files via Ftp and checking mails from a POP3 server it couldn’t be simpler.The current version even includes a couple of cmdlets for transfering files between the local file system and Amazons Simple Storage Service (S3).

Their help needs some work, especially when it comes to examples (I guess they aren’t any), more parameters should offer parameter pinding and I think they should have choosen a different naming scheme (names like Get-Pop oder Send-EMail are too generic and should be reserved for Microsoft).

What I couldn’t get to work is the Get-Time cmdlet (this name is too generic too). For some reasons it always delivered a DateTime that was completely wrong. Although getting the current time over the Internet it not the most important thing to do, I was currious to learn a little more about the inner working which turned out to be quite simple. All there is to do is sending a TCP request over port 13 (or 17) to an Internet Time Server.

Here is a small PowerShell script that gets the current Internet time by querying a list of Time servers.

<#
 .Synopsis
 Internet-Time Server direkt abfragen
#>
 
$TimeServer = "128.138.140.44","64.90.182.55","206.246.118.250","207.200.81.113","128.138.188.172","64.113.32.5","64.147.116.229","64.125.78.85","128.138.188.172"
$OldVerbosePref = $VerbosePreference
$VerbosePreference = "Continue"
 
$Port = 13
foreach($Server in $TimeServer)
{
 Write-Verbose "Checking $Server..."
 try
 {
 $Sockets = New-Object -TypeName System.Net.Sockets.TcpClient $Server, $Port
 $SocketStream = $Sockets.GetStream()
 $Reader = New-Object -TypeName System.IO.StreamReader $SocketStream
 $ServerResponse = $Reader.ReadToEnd()
 Write-Verbose "Server-Reponse: $ServerResponse"
 $Res = $ServerResponse -match "\d+\s+(?<Year>\d{2})-(?<Day>\d{2})-(?<Month>\d{2})\s+(?<Hour>\d{2}):(?<Minute>\d{2}):(?<Second>\d{2})"
 $Year = "20$($Matches.Year)"
 $Month = $Matches.Month
 $Day = $Matches.Day
 $Hour = $Matches.Hour
 $Minute = $Matches.Minute
 $Second = $Matches.Second
 $InternetTime = Get-Date -Year $Year -Month $Month -Day $Day -Hour $Hour -Minute $Minute -Second $Second
 "The current Internet time by $Server`: $InternetTime"
 break
 }
 catch
 { "No connection with $Server possible - lets try the next one." }
}
 
$VerbosePreference = $OldVerbosePref
Veröffentlicht unter Tipps | Hinterlasse einen Kommentar