Monday, February 27, 2006

 
Sending email using MSH part 2

Thanks to MOW at microsoft.public.windows.server.scripting newsgroup. There is an easier way to send email.


$smtp = new-object Net.Mail.SmtpClient("smtp.nospam.com")
$smtp.send("someone@nospam.com","someone@nospam.com", "Email using MSH", "This is easy.")


This example works well when the computer is within the subnet of the email SMTP server. However, I cannot run this at home, because there are more restrictions. For example, we need to enable SSL and also we need to supply credential to send messages outside our network. Here is another example to enable SSL and use credential.


$smtp = new-object Net.Mail.SmtpClient
$smtp.Host = "smtp.nospam.com"
$smtp.EnableSsl = $true
$smtp.Credentials = new-object Net.NetworkCredential("username", "password")
$smtp.send("someone@nospam.com","someone@nospam.com", "Email using MSH", "This is easy.")


If you need to send attachment and you also want to BCC(or CC) other people, you'll need to use another class: System.Net.Mail.MailMessage. Here is another example.


$smtp = new-object Net.Mail.SmtpClient
$smtp.Host = "smtp.nospam.com"
$smtp.EnableSsl = $true
$smtp.Credentials = new-object Net.NetworkCredential("username", "password")

$msg = new-object Net.Mail.MailMessage
$msg.From = "someone@nospam.com"
$msg.To.Add("someone@nospam.com")
$msg.Bcc.Add("someone@gmail.com")
$msg.Subject = "Full test"
$msg.Body = "This is a full test"
$att = new-object Net.Mail.Attachment("c:\testfile.txt")
$msg.Attachments.Add($att)

$smtp.Send($msg)

Saturday, February 25, 2006

 
Monad Beta 3 Available

I haven't played with Monad for a while and found that Beta 3 is available. You can download it from here.

Friday, February 24, 2006

 
Amazon Web Service

I got some experiences with Amazon Web Service when I created my Anime Reviews site. It's pretty neat that you can write a query to look up very detailed product information. Recently, I am thinking to buy a Garmin StreetPilot i3 from Amazon. The regular price is around $308.00, but I heard that sometimes Amazon would lower the price to as low as $199.00. However, the low price is only available for a short time. I don't want to sit in front of my computer and manually check the price myself. Wouldn't it be nice if I can have a script to check the price automatically? It would be nicer is the script if I write the script using MSH!

Ok, enough rambling. Let's get started. To use Amazon Web Service, the first thing to do is to create an account and obtain your own Access Key ID. You can create a free account here. After the account has been created successfully, you can log on to the site with your new account. Move the mouse cursor on top of the button that says "Your Web Services Account". A drop down menu would appear, click "View Access Key Identifiers". There are two keys, one is called "Access Key ID", the other is called "Secret Access Key". Make a note of the Access Key ID, this is the key you used for the web service. The next step is sending the Secret Access Key to me. :-) Ok, just kidding. You should NOT share Secret Access Key with anyone, it's only used to create signature. You should keep it confidential and not reveal it to anyone.

Amazon Web Service offers several ways to find product information. ItemSearch is used to search products by using keywords. Since we already know the product that we are interested in, we can just use ItemLookup. To use ItemLookup, you need two pieces of information, AWSAccessKeyId and ItemId. You already have the Access Key ID, all you need to know is the ItemId for the product. Each product offered on Amazon has a unique ID. If you look at a product page, it's called ASIN. You use ASIN as ItemId for the query. For books query, use ISBN as ItemId in your query. This is the query string for looking up information for the Garmin i3.


$url = "http://webservices.amazon.com/onca/xml?Service=AWSECommerceService"
$url += "&AWSAccessKeyId=Your_AccessKeyID_Here"
$url += "&Operation=ItemLookup"
$url += "&ItemId=B000ACHVVE"
$url += "&ResponseGroup=Offers"


Note that this is for North America www.amazon.com. If you want to query product information for other Amazon sites. You need to change the first line accordingly. You can check here for information on how to modify the query string. Another point to note is that if you don't specify ResponseGroup in your query, the default response is "Request" and "Small" which don't have the price information. To get the current price, we use "Offers" as response group. Finally, we use .Net's Net.WebClient class to get the response from Amazon Web Service. The response from the server is an xml string. We cast it to XMLDocument for easy accessing the data. The amount returned from the query is in cents, so we divide it by 100 to get the dollar amount.


$rxml = [xml](new-object Net.WebClient).DownloadString("$url")
$price = $rxml.ItemLookupResponse.Items.Item.Offers.Offer.OfferListing.Price.Amount
$price = $price/100
echo $price


You might ask how did you come up with the offer price? You can check the documentation in Amazon Web Service web site, or you can save the response as an XML file and use your favorite XML editor/reader to examine the response. FireFox is just fine on parsing the XML file.


$sxml = (new-object Net.WebClient).DownloadString("$url")
echo $sxml > "c:\response.xml"

Tuesday, November 15, 2005

 
MSH Beta 2 for RTM

The new version of MSH is out. If you want to install Visual Studio 2005, you need to uninstall the previous version of MSH and get this one.

Note that the default execution policy has changed. There are four execution policies: Restricted, AllSigned, RemoteSigned and Unrestricted. The default execution policy has changed from RemoteSigned to Restricted. You can see the details here.
If you are like me who had old profile.sh from previous version, you might get a message like this:

set-alias : The AllScope option cannot be removed from the alias 'cat'.

It turns out the new version introduces AllScope, a new session state option. You would get the warning if you are trying to define an existing alias. To override the default alias, do "remove-item alias:cat -force" and recreate the alias per Jeff Jones of Microsoft. Anyway, I don't intend to override the default alias, I just deleted the set-alias statements from the old profile.msh files.

Thursday, November 10, 2005

 
List Installed Software

Jeff Jones of Microsoft has posted a script to list installed software. It's pretty neat.


get-property HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* -property "DisplayName" `
-ErrorAction SilentlyContinue | ft MshChildName,DisplayName -wrap

Tuesday, October 04, 2005

 
MSH Beta 2 Documentation Pack


There is a hands-on lab for MSH included in the pack. This lab, in my opinion, is way better than the quick start guide in the WinFX SDK.


http://www.microsoft.com/downloads/details.aspx?FamilyID=8a3c71d1-18e5-49d7-952a-c55d694ecee3&DisplayLang=en

Friday, September 30, 2005

 
Modifying Registry Using MSH

MSH has a registry provider built-in. You can manipulate the registry just like you do with the file system. Here is an example session on modifying registry.

# Change to the HKLM\SOFTWARE key, note the ':'
# between HKLM and \SOFTWARE.
cd HKLM:\SOFTWARE

# Create a key HKLM\SOFTWARE\mshtest.
new-item mshtest

# Set a value for the key.
set-item mshtest -value TestValue

# Change the key value to DWORD type.
set-item mshtest -value 10 -type dword

# Create a new value mshvalue under the key.
set-property mshtest -property mshvalue -value value1

# Change the value to DWORD type.
set-property mshtest -property mshvalue -value 10 -type dword

# Delete the 'mshvalue' value.
remove-property mshtest -property mshvalue

# Delete the mshtest key.
remove-item mshtest


Note that in this example, I used the relative path. If you prefer the full path use HKLM:\SOFTWARE\mshtest instead of just mshtest.

This page is powered by Blogger. Isn't yours?