Powershell - Example of using hashmaps as dynamic objects

Post date: Nov 11, 2011 9:36:51 AM

Will also serialize it to xml

$peter will be the new "dynamic" object. Which is really a hash table where you can access the Value by using the .Key syntax

It will infer the datatype of the different properties from the assigned value.

$con = New-Object System.Data.SqlClient.SqlConnection("Data Source=Localhost;Initial Catalog=CustomerDb;Integrated Security=SSPI;")function ExecuteSingleRow([string]$sql){ $cmd = New-Object System.Data.SqlClient.SqlCommand($sql, $con) $cmd.Connection.Open() $adapter = New-Object System.Data.SqlClient.SqlDataAdapter($cmd) $ds = New-Object System.Data.DataSet $null = $adapter.Fill($ds) $row = $ds.Tables[0].Rows[0] $cmd.Connection.Close() $row}$nbp = ExecuteSingleRow "Select * from Customer where CustomerId = 5"$peter = @{}$peter.CustomerId = $nbp.CustomerId$peter.JoinDate = $nbp.JoinDate

($peter| ConvertTo-Xml).save("c:\temp\peter.txt")