Archive for August, 2007

PowerShell Cmdlets and Environment Variables

Running Get-ChildItem or any of the variants (alias) such as dir using the following syntax on a PowerShell environment variable may fail under certain circumstances:

PS C:\> gci $env:path
Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.
At line:1 char:4
+ gci <<<< $env:path
Get-ChildItem : Cannot find path ‘C:\Program Files\Windows Resource Kits\Tools\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\ Wbem;C:\Program Files\Support Tools\;C:\WINDOWS\system32\WindowsPowerShell\v1.0;C:\Program Files\Microsoft Network Monitor 3\;C:\Program File
s\ATI Technologies\ATI Control Panel’ because it does not exist.
At line:1 char:4
+ gci <<<< $env:path

PS C:\>($env:path).length
274

The proper way to do this is to remove the preceding dollar sign whenever such variables are used directly against a cmdlet i.e. dir env:path. Together, $env: is required to qualify the scope of the variable when used by itself at the interactive PowerShell console i.e. $env:path. This is because a similarly named variable can be defined in the same scope ($path). Otherwise, an error will be thrown:

PS C:\> env:path
The term ‘env:path’ is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.
At line:1 char:8
+ env:path <<<<

This aspect of PowerShell is actually documented but may not be very obvious until you start using it as demonstrated.

Technorati tags: PowerShell

No Comments »

Learn Virtualization in Windows Server 2008

Slated to release 180 days after Windows Server 2008 goes RTM - now moved to first quarter 2008 (more here) but the planned Feb 2008 launch date remains unchanged (more here) - you now have the opportunity to learn more about Windows Virtualization* via a series of TechNet Webcast planned in Sep 2007.

Virtualization and Windows Server 2008
Wed 26 Sep 2007 2030hrs CET

Examining Microsoft’s Virtualization Portfolio and Capabilities with Windows Server 2008
Thu 27 Sep 2007 1700hrs CET

Click on the links above and register today!

* code name Viridian; green in color

Technorati tags: Longhorn, Windows Server 2008, Windows Server Core 2008

No Comments »

PowerShell Hostname to/from IP Address

Imagine an environment where you have a list containing host names. Your task is to query their static IP addresses and prepare the results into a file for import into individual cells in your favorite Excel spreadsheet.

The following should do the trick and in reverse too; just supply the source file as a list of IP addresses. Multi-homed hosts with multiple IP addresses will spill over into adjacent cells after import into Excel.

#myhosts.txt
leedesmond.com
www.swissitpro.ch
www.microsoft.ch

#get-ipAddress.ps1

$hostCur = “”
$outfilename = “out.csv”
“hostname,IP” | Out-File $outfilename -encoding ASCII

$sb {
foreach ( $obj in $input ) {
if ( $obj -ilike “*address*” ) {
$objCur = $obj.substring($obj.lastindexof(”:”)+3)
#Write-Output “$hostCur, $objCur”
“$hostCur, $objCur” | Out-File $outfilename -encoding ASCII -append
}
}
}

Get-Content ./myhosts.txt | foreach { $hostCur = $_; nslookup $hostCur | &$sb }

Technorati tags: PowerShell

4 Comments »

Microsoft TechNet Flash 17/07 (Swiss Edition)

A Step Beyond Exchange Management Shell Primer (engl.)
Lesen Sie diesen englischen Artikel von Desmond Lee von der Swiss IT Pro User Group und lernen Sie anhand von praktischen Beispielen die Grundlagen der Exchange Server Verwaltung kennen.

Für die freundliche Unterstützung von Microsoft Schweiz bedanke ich mich ganz herzlich.

Bi-weekly edition dated Wed, 29 Aug 2007 - Microsoft TechNet Flash 17/07 (Swiss Edition)

Technorati tags: Exchange Server, PowerShell

No Comments »

E-Learning Office Communication Server 2007

Check out this exciting e-learning opportunity for Office Communications Server 2007 while it is free as in gratis, no cost or payment. It is a 2-hour clinic entitled Collection 5125: Introducing Microsoft Office Communications Server 2007 and comprises of 5126, 5127, 5128 and 5129.

Technorati tags: LCS/OCS , Unified Communications

No Comments »

Know Your Source for Sort-Object

Import-Csv faithfully takes in the contents of a comma-delimited text file (csv) as-is. That is, it treats each distinct column as text, more appropriately a string object (PSCustomObject), even if it is meant to be numeric. Piping this to the Sort-Object cmdlet will sort them in sequential order i.e. 1s followed by 2s then 3s, etc. Take for example the following:

PS C:\> cat num.csv
num
2.361328125
1256.454102
1.944335938
100.090820313
2.155273438
1620.882813
10.944335938
1.508789063
2.6953125
21.4375
2.418945313
1.893554688
1601.767578
1.893554688
1.864257813
2.546875
1493.231445
1.553710938
200.3046875
1202.905273

PS C:\> $n = Import-Csv num.csv

PS C:\> $n | Sort-Object num

num

1.508789063
1.553710938
1.864257813
1.893554688
1.893554688
1.944335938
10.944335938
100.090820313
1202.905273
1256.454102
1493.231445
1601.767578
1620.882813
2.155273438
2.361328125
2.418945313
2.546875
2.6953125
200.3046875
21.4375

To workaround this, explicitly cast the object to transform it into the appropriate type. One way you can do this is illustrated below:

PS C:\> $n | Sort-Object @{expression={[double]$_.’num’}}

num

1.508789063
1.553710938
1.864257813
1.893554688
1.893554688
1.944335938
2.155273438
2.361328125
2.418945313
2.546875
2.6953125
10.944335938
21.4375
100.090820313
200.3046875
1202.905273
1256.454102
1493.231445
1601.767578
1620.882813

The same technique applies if the file was imported using the Get-Content cmdlet.

Technorati tags: PowerShell

No Comments »

Exchange Server 2007 Update Rollup Pack 4

With all the excitement surrounding the upcoming SP1 for Exchange Server 2007, currently at Beta 2 (release note), Microsoft has just released Update Rollup 4 for Exchange Server 2007 (KB940006). As the name implies, this is an accumulative update and replaces the previous updates 1 to 3.

You can download Exchange Server 2007 Update Rollup Pack 4 here.

Technorati tags: Exchange Server

No Comments »

Working with DMVs @ Swiss IT Pro User Group (Zurich)

Join the Swiss IT Pro User Group in the next monthly event happening on Tue 4 Sep 2007 in Zurich. The technical topic to be discussed is MS SQL Server 2005: Working with Dynamic Management Views (DMVs).

Registration and more information on this free IT Pro community event here.

No Comments »

How-to Initialize a Dynamic PowerShell Array

A user asked about creating and assigning values to an array variable in PowerShell over at microsoft.public.windows.powershell. He started off with the following construct and received the errors shown:

PS C:\> $arr = @()
PS C:\> for ( $i = 0 ; $i -lt 3; $i++ ) { $arr[$i] = $i }
Array assignment failed because index ‘0′ was out of range.
At line:1 char:40
+ for ( $i = 0 ; $i -lt 3; $i++ ) { $arr[$ <<<< i] = $i }
Array assignment failed because index ‘1′ was out of range.
At line:1 char:40
+ for ( $i = 0 ; $i -lt 3; $i++ ) { $arr[$ <<<< i] = $i }
Array assignment failed because index ‘2′ was out of range.
At line:1 char:40
+ for ( $i = 0 ; $i -lt 3; $i++ ) { $arr[$ <<<< i] = $i }

Since $arr began life as an empty array, you cannot simply change “nothing” until the individual index items have first been created. You can verify by using $arr.length and the answer will be zero.

To fix this, do the following; PowerShell will automatically create and dynamically expand $arr (”redim” in Visual Basic) to the required size:

PS C:\> for ( $i = 0 ; $i -lt 3; $i++ ) { $arr += @($i) }
PS C:\> $arr.length
3
PS C:\> $arr.gettype().fullname
System.Object[]
PS C:\> $arr
0
1
2
PS C:\> for ( $i = 0 ; $i -lt 3; $i++ ) { $arr[$i] }
0
1
2

Now you can modify the contents of the array accordingly:

PS C:\> $j = 10; for ( $i = 0 ; $i -lt $arr.length; $i++ ) { $arr[$i] = $j*$i }
PS C:\> $arr
0
10
20

By far the simplest way to create an array, assuming you already know the required dimension and type, is demonstrated below:

PS C:\> $arr = 1, 3, 5, 7, 9   #$arr = ,1    #creates an array with a single element
PS C:\> $arr.length
5
PS C:\> $arr.gettype().fullname
System.Object[]
PS C:\> $arr
1
3
5
7
9
PS C:\> for ( $i = 0; $i -lt $arr.length; $i++ ) { $arr[$i] }
1
3
5
7
9

You can also use the New-Object cmdlet to create and initialize the array at the same time. In this case, Int32 objects will be initialized to zero automatically:

PS C:\> $arr = New-Object int[] 3
PS C:\> $arr
0
0
0
PS C:\> for ( $i = 0; $i -lt $arr.length; $i++ ) { $arr[$i] = $i }
PS C:\> $arr
0
1
2

Technorati tags: PowerShell

No Comments »

Post Event Highlights: Windows Server 2008 @ techiesonly (Dubai)

Highlights of last Wednesday’s (15 Aug 2007) UAE IT Pro User Group monthly event are now posted on-line at techiesonly. Attendees got the chance to learn about Microsoft Small Business Server 2003 R2 which generated lots of interests, especially for those new to the product. The interactive participation and enthusiasm of the crowd continued with my presentation cum demo session on “Technical Overview of Windows Server 2008″. Overall, the attendees gave very high marks for the whole event - from logistics, organizational to speakers to the great technical contents - and expressed keen interests to see more such programs in the future.

This IT Pro user group community event would not be possible without the support of Microsoft Gulf (Dubai) and sponsors like SAPIEN Technologies. Most importantly, all of us wish to thank YOU for taking time off your busy work schedule to be part of this exciting event.

No Comments »

Next »