Monthly Archives: June 2009

out-gridview

The version of PowerShell that ships with Windows 7 RC has fixed out-gridview so that we can display more than 10 columns.

try

get-process | select * | Out-GridView

to test it

Technorati Tags: ,

AD PowerShell on Windows 2008/2003 – supposedly

As James pointed out recently the AD Web Service from Windows 2008 R2 has been released for Windows 2008 and Windows 2003 -  see http://blogs.msdn.com/adpowershell/archive/2009/06/23/use-active-directory-powershell-to-manage-windows-2003-2008-dcs.aspx for details.

Having installed it there doesn’t appear to be any sign of the AD cmdlets or Provider.  On this basis it is useless.  If anyone knows different please let me know because I’ve looked in all the logical (and illogical) and I can’t find those cmdlets.  The Windows 2008 R2 RSAT won’t install so that source doesn’t work.

Microsoft Learning Manager

I went on to the Microsoft site looking for books on SQL Server 2008.  What you get is an incomprehensible jumble of stuff.  Don’t tell me about everything there is – let me get to the information I want.

I used to really like the Microsoft site as there was masses of information that usually logically arranged.  Now we get some many layers between us and the information that I’m likely to expire of old age (OK I know its not that long to go) before finding it. 

Another example – I went looking for the information on the SQL Server exams – the logical route through the site leaves you with the impression there isn’t an upgrade from SQL 2005 MCITP to the 2008 version.  Wrong! But oh how you have to search.

I’m coming to the conclusion that the only way to access the Microsoft site is to search from outside and hope you can get to what you need.

A triumph of style over substance at the moment.

Technorati Tags:

SQL Server autoclose

Buck Woody blogged about SQL Server autoclose and how it should be set to being off  - http://blogs.msdn.com/buckwoody/archive/2009/06/24/sql-server-best-practices-autoclose-should-be-off.aspx 

I’m doing a lot with SQL Server and PowerShell at the moment and it started me thinking that I could easily set this property in PowerShell.

001
002
003
004
005
006
007
008
009
010
$Server = New-Object Microsoft.SqlServer.Management.Smo.Server("SQL08")
$Server.databases | select Name, Autoclose
foreach ($db in $Server.Databases){
    if (!$db.IsSystemObject){
        $db.Autoclose = $false
        $db.Alter()
    }
}
$Server = New-Object Microsoft.SqlServer.Management.Smo.Server("SQL08")
$Server.databases | select Name, Autoclose

 

Create a server object and display the databases, selecting name and the autoclose property.  Its boolean so we get true or false back.

We can iterate through the database collection. Test if the database is a system database and set autoclose to false (we don’t want autoclose to happen).  A call to Alter() saves the change.

Finally we can recreate the server object and  redisplay the autoclose property.

Technorati Tags: ,

PowerShell survey

The PowerShell team want to know how you find PowerShell related material – scripts, training material etc etc

http://blogs.msdn.com/powershell/archive/2009/06/24/new-powershell-survey-on-connect.aspx

Technorati Tags:

foreach usage

I’ve noticed the way foreach is being used and it falls into two groups. I am using foreach-object to keep the discussion straight – like everyone else I use the foreach alias – yes I know I rant about aliases but the aliases for the *-object cmdlets make sense.

001
002
003
004
005
006
007
008
$things = Get-Content some_file.txt
foreach ($thing in $things){
    ## do stuff here
}

Get-Content some_file.txt | foreach-object{
    ## do stuff here
}

 

The first one reads the file and then iterates through the objects while second one reads the file and puts the objects on the pipeline.

So whats the difference and is it a big deal?  Instinctively I try to use the second but is that best?

using foreach we have to read the file and hold all the objects in memory while the foreach-object works with them one at a time as they come down the pipeline – for a big file that makes a lot of difference in resources.  However, if we need to access a file multiple times using foreach may be the better option as we only have to read the file once.  foreach can be faster which is a trade off we may need to think about

inside the loops we can use $thing or $_ to access the object respectively.  One point to think about is that if you do further work inside the loop that generates a different $_ eg a switch then reading the code may get confusing especially if you come back to it after a while

So is there a best option – probably not as an all time categorical “this is the best so always use it”

I tend to prefer the foreach-object as I can get on with processing objects straightaway though we do have to be careful with subsequent order of events sometimes.  The other advantage is that foreach-object gives me a –begin, –process and –end script block which makes things even more flexible

So no hard and fast answer but as a final thought the foreach-object usage looks more “powershelly” as someone once put it

Technorati Tags: ,

AD Recovery

I have always been impressed by by Quest’s AD Recovery Manager and now its even better – yep it has PowerShell cmdlets see http://wiki.powergui.org/index.php/Quest_Recovery_Manager_for_Active_Directory_cmdlets_reference

Scripting Games – Event 6 and 7

Event 6

Beginners – find the errors in the script

Advanced – read a text file of a network trace and find the delay

can’t be bothered. 

Beginners was done last year plus I really hate scripts that have $objxxx    - so VBScript 

Advanced is yet another variation on reading a file.

Event 7

Advanced – write a script to play 21 or blackjack or pontoon or whatever you want to call it.  Did that last year see http://richardsiddaway.spaces.live.com/blog/cns!43CFA46A74CF3E96!1161.entry

Might have a look at beginners 7

Technorati Tags:

PowerShell in Practice Chapter 13

Chapter 13 – IIS – is now available on MEAP  www.manning.com/siddaway

Technorati Tags:

Games: Advanced 4

Read a file and create a graph of the results.  The quickest way to graph the data is to take the height and display a number of “*” depending on the value.

001
002
003
004
005
006
007
008
cls
Get-content "High Jump Stats_Adv4.txt" | foreach {
    $data = $_ -split ","
    Write-Host "Jumper: $($data[1]) $($data[0]) last $($data.count - 3) jumps"
    for ($i=2;$i -le $($data.count - 1); $i++) {
        "*" * $data[$i]
    }
}

 

-split is a PowerShell 2 operator -  use $_.Split(“,”) in v1

BTW – there are 43 jumps per person not the 30 as stated in the event information.

This answers the letter of the event but probably not the spirit. PowerGadgets would be an interesting option for this.  PowerGadgets installs straight into Windows 7. I had tested it with PowerShell v2 in the past but nice that it installs on Windows 7.

If we want to create individual graphs using PowerGadgets

001
002
003
004
005
cls
Get-content "High Jump Stats_Adv4.txt" | foreach {
    $data = $_ -split ",",3
    $data[2] -split "," | out-chart -gallery lines -Title "$($data[1]) $($data[0])"   
}

 

All we do is pipe numeric data (notice the double split – the first gives the names plus the numbers as one string.  We can then split the numbers separately as required) into out-chart using the first two elements from the first split (jumpers name) as the title.

Finally lets look at charting in Excel – we have to use the COM object to work with Excel (wouldn’t be nice if we got proper PowerShell support in next version of Office – please -  )

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
cls
$sfile = "C:\Scripts\Scripting Games 2009\Competitors pack\ad4.xlsx"
if(Test-Path $sfile){Remove-Item $sfile}


## create a spreadsheet
$xl = New-Object -comobject "Excel.Application"
$xl.visible = $true
$xlbooks =$xl.workbooks
## remember non-US culture
## add work book
$newci = [System.Globalization.CultureInfo]"en-US"
$wkbk = $xlbooks.PSBase.GetType().InvokeMember("Add", `
     [Reflection.BindingFlags]::InvokeMethod, $null, $xlbooks, $null, $newci)

$sheet = $wkbk.WorkSheets.Item(1)
$sheet.Name = "High Jump"

## read the file
$row = $col = 1
Get-content "High Jump Stats_Adv4.txt" | foreach {
    $data = $_ -split ",",3
    $numbers = $data[2] -split ","
    ## name
    $sheet.Cells.Item($row, $col) = $data[1]
    $sheet.Cells.Item($row, $col+1) = $data[0]
    ## jumps
    for ($i=0;$i -le $numbers.length-1;$i++){
        $sheet.Cells.Item($row, $col+2+$i) = $numbers[$i]
    }
    $row++
}

## get the cell range
$xl.Charts.Add() | out-null
$xl.ActiveChart.chartType = 4  ## line chart

$xl.ActiveChart.HasTitle = $true
$xl.ActiveChart.ChartTitle.Text = "High Jump Results"

## (x axis =1, y axis = 2)
$xl.ActiveChart.Axes(1, 1).HasTitle = $true
$xl.ActiveChart.Axes(1, 1).AxisTitle.Text = "Attempt"
$xl.ActiveChart.Axes(2, 1).HasTitle = $true
$xl.ActiveChart.Axes(2, 1).AxisTitle.Text = "Height"

[void]$wkbk.PSBase.GetType().InvokeMember("SaveAs", `
   [Reflection.BindingFlags]::InvokeMethod, $null, $wkbk, $sfile, $newci)

[void]$wkbk.PSBase.GetType().InvokeMember("Close", `
   [Reflection.BindingFlags]::InvokeMethod, $null, $wkbk, 0, $newci)

$xl.Quit()

 

We create a spreadsheet and add a work book .  Have to use this long winded method as I am using a non-US culture on my machine (Excel bug).

We can add the work sheet, add the data and finally add the chart.  Chart looks ugly but it is a chart.  Finally save the spreadsheet using the same horrible construction. 

didn’t like this event - tedious