Wednesday, May 4, 2011

Strange occurrence when returning Datatable from a function

I was trying to retrieve some data from a SQL Server database using Powershel using the following function:


function GetLocationData($Name)
{
$SqlConnection = New-Object system.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=ServerName;Database=MyDB;User Id=dbuser;Password=pwd;"

$SqlCommandText = "SELECT Field1, Field2 FROM TableName WHERE [Name]='" + $Name + "'"
$SqlConnection.Open()

$SqlCmd = New-Object Data.SqlClient.SqlCommand($SqlCommandText, $SqlConnection)
$Reader = $SqlCmd.ExecuteReader()

$Datatable = New-Object System.Data.DataTable
$DataTable.Load($Reader)

# Close the database connection
$sqlConnection.Close()

return $Datatable
}


When I would call it from my script


$dt = New-Object System.Data.DataTable
$dt=GetLocationData $item.name
Write-Host "row count is " + $dt.Rows.Count


It would not return anything and if I tried accessing a field


Write-Host $dt.Rows[0].Field1


I would get an error "Cannot index into null array"


The issue is that function would be returning a child collection of the table object instead of the table itself. What helped me is using the comma operator to wrap the rows collection in an array so that when the array is unrolled leaving me with the original rows collection.

So all I had to do to fix this issue is put a comma in the return statement right before datatable object:


function GetLocationData($Name)
{
$SqlConnection = New-Object system.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=ServerName;Database=MyDB;User Id=dbuser;Password=pwd;"


$SqlCommandText = "SELECT Field1, Field2 FROM TableName WHERE [Name]='" + $Name + "'"
$SqlConnection.Open()

$SqlCmd = New-Object Data.SqlClient.SqlCommand($SqlCommandText, $SqlConnection)
$Reader = $SqlCmd.ExecuteReader()

$Datatable = New-Object System.Data.DataTable
$DataTable.Load($Reader)

# Close the database connection
$sqlConnection.Close()

return ,$Datatable
}

Tuesday, May 3, 2011

Windows Live Essentials 2011 uninstall from Windows 7

I have been struggling with creating an uninstall package for Windows Live Essentials 2011 on Windows 7 platform.

Finally I found a list of uninstallers that need to be run in order to clear out all of Windows Live Essentials:


Live Essentials {FE044230-9CA5-43F7-9B58-5AC5A28A1F33}
Microsoft SQL Server 2005 Compact Edition {F0B430D1-B6AA-473D-9B06-AA3DD01FD0B8}
D3DX10 Microsoft {E09C4DB7-630C-4F06-A631-8EA7239923AF}
Live Remote Service {E02A6548-6FDE-40E2-8ED9-119D7D7E641F}
Live Remote Client {DF6D988A-EEA0-4277-AAB8-158E086E439B}
Live Writer Resources {DDC8BDEE-DCAC-404D-8257-3E8D4B782467}
Live MIME IFilter {DA54F80E-261C-41A2-A855-549A144F2F59}
Live Communications Platform {D45240D3-B6B3-4FF9-B243-54ECE3E10066}
Live Photo Common {D436F577-1695-4D2F-8B44-AC76C99E0002}
MSVCRT_amd64 Microsoft {D0B44725-3666-492D-BEF6-587A14BD9BD9}
Live UX Platform {CE95A79E-E4FC-4FFF-8A75-29F04B942FF2}
Live Mail {C66824E4-CBB3-4851-BB3F-E8CFD6350923}
Live Writer {AAAFC670-569B-4A2F-82B4-42945E0DE3EF}
Live Mesh {A0C91188-C88F-4E86-93E6-CD7C9A266649}
Live Movie Maker {92EA4134-10D1-418A-91E1-5A0453131A38}
MSVCRT Microsoft {8DD46C6A-0056-4FEC-B70A-28BB16A1F11F}
Mesh Runtime {8C6D6116-B724-4810-8F2D-D047E6B7D68E}
Microsoft Silverlight {89F4137D-6C26-4A84-BDB8-2E5A4BB71E00}
Live Remote Client Resources {847B0532-55E3-4AAF-8D7B-E3A1A7CD17E5}
Live Messenger {80956555-A512-4190-9CAD-B000C36D6B6B}
Live Messenger Companion Core {78A96B4C-A643-4D0F-98C2-A8E16A6669F9}
Live UX Platform Language Pack {6A05FEDF-662E-46BF-8A25-010E3F1C9C69}
Live SOXE {682B3E4F-696A-42DE-A41C-4C07EA1678B4}
Bing Bar Platform {65C0025A-2CDE-43C5-82D0-C7A56EF0DB39}
Live Remote Service Resources2 {656DEEDE-F6AC-47CA-A568-A1B4E34B5760}
Live Language Selector {5EB6F3CB-46F4-451F-A028-7F6D8D35D7D0}
Messenger Companion {50816F92-1652-4A7C-B9BC-48F682742C4B}
Live PIMT Platform {4CBABDFD-49F8-47FD-BE7D-ECDE7270525A}
Live Family Safety {46A5FBE9-ADB3-4493-A1CC-B4CFFD24D26A}
Live Photo Gallery {34F4D9A4-42C2-4348-BEF4-E553C84549E7}
Live Mesh ActiveX Control for Remote Connections {2902F983-B4C1-44BA-B85D-5C6D52E2C441}
Live SOXE Definitions {200FEC62-3C34-4D60-9CE8-EC372E01C08F}
Junk Mail filter update {1F6AB0E7-8CDD-4B93-8A23-AA9EB2FEFCE4}
Live ID Sign-in Assistant {1B8ABA62-74F0-47ED-B18C-A43128E591B8}
Live Installer {0B0F231F-CE6A-483D-AA23-77B364F75917}
Bing Bar {08234a0d-cf39-4dca-99f0-0c5cb496da81}



I also needed to package separately Windows Live Photo gallery and Movie Maker and create an uninstall just for those two:


Live Essentials {FE044230-9CA5-43F7-9B58-5AC5A28A1F33}
Microsoft SQL Server 2005 Compact Edition {F0B430D1-B6AA-473D-9B06-AA3DD01FD0B8}
D3DX10 Microsoft {E09C4DB7-630C-4F06-A631-8EA7239923AF}
Live Communications Platform {D45240D3-B6B3-4FF9-B243-54ECE3E10066}
Live Photo Common {D436F577-1695-4D2F-8B44-AC76C99E0002}
MSVCRT_amd64 Microsoft {D0B44725-3666-492D-BEF6-587A14BD9BD9}
Live UX Platform {CE95A79E-E4FC-4FFF-8A75-29F04B942FF2}
Live Movie Maker {92EA4134-10D1-418A-91E1-5A0453131A38}
MSVCRT Microsoft {8DD46C6A-0056-4FEC-B70A-28BB16A1F11F}
Live UX Platform Language Pack {6A05FEDF-662E-46BF-8A25-010E3F1C9C69}
Live SOXE {682B3E4F-696A-42DE-A41C-4C07EA1678B4}
Live PIMT Platform {4CBABDFD-49F8-47FD-BE7D-ECDE7270525A}
Live Photo Gallery {34F4D9A4-42C2-4348-BEF4-E553C84549E7}
Live SOXE Definitions {200FEC62-3C34-4D60-9CE8-EC372E01C08F}
Live ID Sign-in Assistant {1B8ABA62-74F0-47ED-B18C-A43128E591B8}
Live Installer {0B0F231F-CE6A-483D-AA23-77B364F75917}


As it turned out the above was not enough to uninstall Photo Gallery, I needed two more Product GUIDs, one for PhotGallery and one for Photo Common, so in total the were 2 GUIDs for Photo Gallery and 2 GUIDs for Photo Common. Below are the extra GUIDs:


Live Photo Gallery {3336F667-9049-4D46-98B6-4C743EEBC5B1}
Live Photo Common {A9BDCA6B-3653-467B-AC83-94367DA3BFE3}