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

PowerShell CommandBuilder – a new way to look at PowerShell commands

I teach a lot of PowerShell classes. Beginners are usually overwhelmed by the number of cmldets, parameters, objects and the uncounted ways of putting them together. Especially if someone newer was exposed to a command line interface or a scripting or programming language before. I often wish I had more visual aids to explain the PowerShell vocabulary of verbs, nouns, parameters etc.

Recently I stumbled upon a complete different way of looking at the PowerShell Cmdlets:

http://www.microsoft.com/resources/TechNet/en-us/Office/media/WindowsPowerShell/WindowsPowerShellCommandBuilder.html

The Command Builder is a small Siverlight apps that allows someone to build the Cmdlet syntax by dragging the verb and the noun and typing in values for a mandatory parameter. Although its focused on SharePoint and Office 365 it would be easily extensible to any module. Right now I don’t see any benefits for an experienced PowerShell user buts its a great learning tool and a fresh new way looking at PowerShell and its Cmdlets.

Veröffentlicht unter Allgemein | Hinterlasse einen Kommentar

How many keywords does PowerShell 2 have?

An overview of all available keywords is something that is missing in the PowerShell help files (at least I haven’t discovered the topic yet). But it is very easy to make PowerShell show you such a list. This is a nice “trick” I found a couple of days ago while I was writing an introduction about the “PowerShell Scripting language”. The original web site is:

http://devcentral.f5.com/weblogs/Joe/archive/2009/01/05/powershell-abcs-k-is-for-keywords.aspx

So the command that gives you the names of all (32) keywords looks something like this:

[Type]::GetType("System.Management.Automation.KeywordTokenReader") | Foreach-Object {
$_.InvokeMember("_keywordTokens", "NonPublic,Static,GetField", $Bull, $_, @()) } | Select-Object -Exp Keys | Sort-Object

Really cool – I love metadata;)

Veröffentlicht unter Allgemein | Hinterlasse einen Kommentar

PowerShell Deep Dive Conference 2011 in Frankfurt/Germany

It was a good and real intense meeting with some brilliant people and ideas presented (I was only there for the first day because I had a PowerShell training the next day in Nürnberg   – so I was really glad that I could attend at all). The talks were mostly about using PowerShell at the edge like using the PowerShell in a “fan in” scenario with IIS running a single PowerShell process. It was also a rare opportunity to meet Bruce Payette outside of his Redmond office;).

I was also giving a talk about the “Power of the PowerShell type system” – most of the topics of my talk might have been to basic for many people in the room but at the end it got hopefully a little bit more interesting while I was talking about the PSTypeConversion class and its role as a universal type converter that makes it really easy to create “Type Accelerators” (technically they aren’t Type Accelerators – but they can be used in the same way) that turn any structured string into a typed object.

The reason that I choose that topic was that originally I proposed other a few other topics but due to a “misunderstanding” I got on the list with a “Build your own type adapter” topic which probably would have been not enough to fill 35 minutes with – maybe it’s a topic for a lightning talk next time that lasts only 5 minutes).

It was  a thrill to meet so many PowerShell VIPs in person. I won’t name any because there names are all on the agenda. So it was definitely worth attending – I wish a few more people would have found their way to the 21st floor. I hope that Quest will repeat the PowerShell deep dive next year I suppose that a lot more attendees will come (if only for the reason that it looks like that Microsoft will make the PowerShell interface mandatory with the next version of Windows Server).

Tobias Weltner is doing one of his lightning talks about Regex

Thomas Lee explains all you have to know about formatting

Thomas Lee explains all you have to know about formatting

A nice view from the top at the PowerShell Deep Dive Conference in Frankfurt

Veröffentlicht unter Allgemein | Hinterlasse einen Kommentar

Where is the ISS now?

Here is a short script that shows when the ISS (International Space Station – not IIS or IRS) flies over your house the next time. It does that just by calling a Web Service that the ISS Fan Club has setup.

<#
.Synopsis
IIS-Flyby coordinates in real time
#>

# My own location
$Lat = 48.733333
$Lon = 9.316667

$Url = "http://www.issfanclub.com/modules/iss_tracking/get-pass.php?lat=$Lat&lon=$Lon&visibleonly=n&start=0"

$WC = New-Object -Typename System.Net.WebClient
$ISSDaten = 1$WC.DownloadString($Url)
$ISSDaten.tracking.row | Where-Object { $_.Latitude -gt $Lat - 5 -and $_.Latitude -lt $Lat + 5} | Select-Object Time, Latitude, Longitude
Veröffentlicht unter Allgemein | Hinterlasse einen Kommentar

PowerShell V3 (first sightings)

PowerShell V3 has finally arrived (Yippie ya ya;). A first preview version is part of the Windows 8 Developer Preview and of course the Windows 8 Server preview (available on MSDN and the Windows 8 preview is available here).

Yesterday (a week after the first release) the PowerShell Team finally blogged about it:

http://blogs.msdn.com/b/powershell/archive/2011/09/20/windows-management-framework-3-0-community-technology-preview-ctp-1-available-for-download.aspx

The direction the PowerShell team is taking with PowerShell is really amazing. Who thought about workflows in the admin world a couple of years ago? (I wonder if they have anything to do with the Dynamic System Initiative?).

And good, old WMI is getting more and more attractive (I never felt that I wanted to write my own WMI provider but having the ability to do so is probably a good thing).

I wonder what role the new release will play at the PowerShell Deep Dive next month in Frankfurt/Germany (which I am really looking forward too).

PowerShell V3 - the first sighting (its really version 3.0 and even WsMan had been enhanced)

Update: A few installation hints

>As stated in the readme-file: The preview can’t be installed on a non-english Windows 7 SP1

>.NET Framework 4.0 runtime must be installed first – if its not installed, there won’t be any error message – the installation of the preview runs through but it won’t get installed

>After a sucessful install the preview version replaces the existing PS 2.0 – the year of the copyright is a good indicator to check wether the preview had been installed or not

Veröffentlicht unter Allgemein | Hinterlasse einen Kommentar

List local admins with a custom WMI provider

Roger Zander developed a small WMI provider that lists local admins on the local or on a network computer.

The download address of this tiny Codeplex-Project is:

http://localadminwmiprov.codeplex.com/

After the installation of the msi file a query to list the local admins with there name and SID looks like this:

gwmi -namespace root\cimv2 -class Win32_LocalAdmins | Select Member, SID

The author used the programming language C# and Visual Studio to develop his WMI provider. It looks like that with the next version of PowerShell it will become much easier to create your own WMI provider.

Veröffentlicht unter Allgemein | Hinterlasse einen Kommentar

PowerShell at BUILD – create WMI providers and workflows with the next version of PowerShell

Although the recent BUILD conference was 90% about Windows 8 and how to develop so called “Metro style apps” (in nearly any language) there were also two (?) PowerShell sessions by Jeffrey Snover (Inventor of PowerShell) and Refaat Issa from the PowerShell team:

>Make your product more manageable

>Manage a highly-efficient environment at scale using the Windows Management Framework (WMF)

The session “Make your product more manageable” title might sound a little boring at first but the session contains some dynamite for PowerShell aficionados – like an easy way to write your own WMI provider or the possibility to include a workflow in a PowerShell script with the new workflow keyword that is used instead of a function for long running activities on multiple machines (like Refaat put it “a script with a bookmark”).

You can watch or download the session here:

http://channel9.msdn.com/Events/BUILD/BUILD2011/SAC-644T

I have watched this session to its end and can only say “whow, I am overwhelmed” (I didn’t expect a “PowerShell 3″ to appear anytime soon because that was V2 has to offer right now is more than what 80% of all admin users might ever need – as far as I can tell from my own experience and my countless PowerShell classes I have given over the last years). I can’t wait to download the Windows 8 Server preview and try out the new stuff myself.

PS: Somewhere during the first half of the presentation Jeffrey mentions the fact that Windows 8 contains about 2.300+ Cmdlets (!)

Veröffentlicht unter Allgemein | Hinterlasse einen Kommentar