 Wednesday, April 30, 2008
Using Miado with Powershell
One of the nice things about Miado is that it wraps a lot of tedious, repetitive ADO.Net code and therefore greatly reduces the amount of code you actually do need to write. Here's a quick example. One of the tasks I had to do at my last last gig was upload a set of prices from a CSV file every month. I wrote a PowerShell script to use Miado to do the DB inserts:
- function BuildSql()
- {
-
- $sb = New-Object System.Text.StringBuilder
- $sb.Append("insert into VendorPrice(VendorID, FixedPrice, VariablePrice, ");
- $sb.Append("CreationDate, LastUpdatedDate) ");
- $sb.Append("values(@VendorID, @FixedPrice, @VariablePrice, ");
- $sb.Append("current_timestamp, current_timestamp)");
-
- return $sb.ToString();
- }
-
- [Reflection.Assembly]::LoadFile("C:\dev\VS2008\Miado\src\Miado\bin\Release\Miado.dll")
-
-
- $connString = "Data Source=.\SQLEXPRESS;Initial Catalog=MyDatabase;User Id=user;Password=passw0rd"
- $dbProvider = System.Data.SqlClient.SqlClientFactory::Instance;
- $repository = Miado.MiadoRepositoryFactory::CreateMiadoRepository($dbProvider, $connString);
-
- $sql = BuildSql;
-
- foreach ( $row in Import-Csv prices.csv | Select VendorID, FixedPrice, VariablePrice )
- {
- $repository.ExecuteNonQuery($sql.ToString(), $row.VendorID, $row.FixedPrice, $row.VariablePrice);
- }
As you can see, the script iterates over each row in the CSV file and inserts a record for each row. There's no need to write all the code necessary to create the DbConnection and DbCommand objects, nor do you have to dynamically create DbParameter objects for each row. Just pass the SQL and the variables (in order of replacement) into the IMiadoRepository.ExecuteNonQuery() call and Miado takes care of all the ugly details. One line for each insert - that's it! Edit: This post was updated to reflect the changes in the 0.7.10530.1
Wednesday, April 30, 2008 11:38:38 AM (Eastern Standard Time, UTC-05:00) .Net | Miado | Powershell
 Tuesday, April 22, 2008
Getting the SysInternals Suite, Ruby style
John Robbins at Wintellect put out a nice blog post about using a PowerShell script to download and install the excellent suite of tools provided by Sysinternals. I absolutely love the *concept* of PowerShell and the ability to harness the power of .Net at the command prompt. The one downside to it is that I find the syntax to be a bit too verbose than what I normally expect out of a scripting language. YMMV.
Anyway, in an effort to stay on top of my (limited) Ruby skills, I have ported his implementation to Ruby: #!/usr/bin/env ruby
require 'net/http' require 'zip/zip'
def usage puts "" puts "Downloads and extracts all the tools from Sysinternals" puts "" puts "Usage: ruby download_sysinternals.rb <extract-directory> [<save-directory>]" puts "Required Parameter :" puts " <extract-directory> : The directory where the SysinternalsSuite.zip" puts " tools are extracted." puts "Optional Parameters :" puts " <save-directory> : Saves SysinternalsSuite.zip to the specified" puts " directory. If not specified, the .ZIP file " puts " is not saved." puts " -? : Display this usage information" puts "" puts "" Kernel#exit end
def create_directory_if_needed(dir) File.makedirs(dir) unless File.exist?(dir) end
def download_sysinternals_zip_file(download_dir) puts "Downloading SysinternalsSuite.zip file" url = URI.parse('http://download.sysinternals.com/Files/SysinternalsSuite.zip') req = Net::HTTP::Get.new(url.path) resp = Net::HTTP.start(url.host, url.port) { |http| http.request(req) } puts "Saving SysinternalsSuite.zip to: #{download_dir}" file_name = "#{download_dir}/SysinternalsSuite.zip" delete_file_if_exists(file_name) open(file_name, "wb") { |file| file.write(resp.body) } puts "Finished downloading" end
def extract_zip_file(zip_file, extract_dir) puts "Extracting zip file to: #{extract_dir}" Zip::ZipFile.open(zip_file) do |zip| zip.each do |entry| file_name = "#{extract_dir}/#{entry.to_s}" puts "Extracting #{file_name}" # you will need to run this as an admin if # you have sysinternals stuff in "Program Files" delete_file_if_exists(file_name) entry.extract(file_name) end end
puts "Finished extracting zip file" end
def delete_file_if_exists(file_name) File.delete(file_name) if File.exist?(file_name) end
# here we go!!! usage if ( ARGV.empty? || ARGV[0] == "-?" )
extract_dir = ARGV[0] save_dir = ARGV.length > 1 ? ARGV[1] : ENV["TEMP"]
# download the file create_directory_if_needed(save_dir) download_sysinternals_zip_file(save_dir)
# extract the zip file create_directory_if_needed(extract_dir) zip_file = "#{save_dir}/SysinternalsSuite.zip" extract_zip_file(zip_file, extract_dir)
# clean up after ourselves delete_file_if_exists(zip_file) if ARGV.length < 1
Tuesday, April 22, 2008 6:59:08 AM (Eastern Standard Time, UTC-05:00) Powershell | Ruby | Sysinternals
|

Subscribe to this feed
 Email Me
 Follow Me On Twitter
| | Sun | Mon | Tue | Wed | Thu | Fri | Sat |
|---|
| 30 | 1 | 2 | 3 | 4 | 5 | 6 | | 7 | 8 | 9 | 10 | 11 | 12 | 13 | | 14 | 15 | 16 | 17 | 18 | 19 | 20 | | 21 | 22 | 23 | 24 | 25 | 26 | 27 | | 28 | 29 | 30 | 31 | 1 | 2 | 3 | | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
Search
Navigation
Tag Cloud
Archive
Blogroll
|