PowerShell Cookbook

Search

Categories

 

On this page

Loving DasBlog 1.8
Monad Technet Webcast Pt. 2
Monad? Astroturfing?
Monad and the "First Vista Virus"
Reminder: Monad Technet Webcast
A Download Manager in MSH
Ideas to Cut Off Duplicate Questions in Forums
Another way to get Monad -- WinFX
System Administrator Appreciation Day
Command-line shortcut for repetitive operations

Archive

Blogroll

Disclaimer
I work for Microsoft.

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 220
This Year: 20
This Month: 0
This Week: 0
Comments: 533

Sign In

 Friday, August 12, 2005
Friday, August 12, 2005 8:55:26 AM (Pacific Daylight Time, UTC-07:00) ( )

Well, Scott and Omar have been busy again, and have just released DasBlog 1.8. It's added a few helpful new features, and plenty of themes. The upgrade went painlessly as far as I can tell -- be sure to let me know if you see any issues!

I've set "BlogXP" as my theme -- it sure is beautiful. My pet peeve of the old "Discreet Blog Blue" was that the sidebar would float all around, and generally mess with layout. Especially on Administration pages. BlogXP is fully functional, and bug-free so far :)

The only down-side is that I try to port the general look and feel back to the stylesheet of my regular homepage when I change the look of the blog. I try to limit this to the colours and minor design elements, though, so my main homepage looks pretty darn plain again. Oh well, the last time I updated that page was over a year ago!

That reminds me. I've been meaning to drop a line to Scott and Omar, thanking them for a wonderful blogging system. I'll probably forget that again for a long time, so let me say it now -- Thank You! You guys have really got a great system going.

Comments [2] | | # 
 Wednesday, August 10, 2005
Wednesday, August 10, 2005 4:29:04 PM (Pacific Daylight Time, UTC-07:00) ( )

Just as a reminder, the second Monad technet webcast is happening right now (9:30am PST to 11:00 am PST): http://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?EventID=1032277852&EventCategory=4&culture=en-US&CountryCode=US.

I posted more about the Technet Webcasts here.

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Comments [1] | | # 
 Sunday, August 07, 2005
Monday, August 08, 2005 5:23:00 AM (Pacific Daylight Time, UTC-07:00) ( )

If you've read Adam's post ("Preparing for Slashdot",) you probably read how we worked to inform the Slashdot crowd via early entries in the discussion. Luckily, they were moderated up fairly quickly, and people were able to continue commenting with quite a bit more basis in fact than the original article provided.

Often, this is called "Astroturfing," alluding to "fake grass" in a grass-roots movement.

Scoble brought it up, as have some internal discussions over the past few days. Scoble wrote:

Here's one place I got uncomfortable. In the past Microsoft astroturfed its way into issues. I always told myself if I saw astroturfing going on I'd point it out and oppose its use. (...) I'm not saying Adam and Lee crossed the line here (they didn't), but I wanted to point out what they did so that we can have a public discussion about it. I think what they did was OK simply because it was in the spirit of getting the facts out.

I personally don't even think this even approached astroturfing. First of all, I do consider myself a member of the Slashdot community. In fact, I first heard about Monad from Slashdot before I worked at Microsoft. I've been at the karma cap for years, and don't generally post about Microsoft articles. Is the MSRC team astroturfing by going to BlackHat?

Second, I was very careful to mention my connection with Monad exactly to prevent the perception of astroturfing. My first comment, "Comments from a Monad developer," has about as obvious a title I could have given it. Of course, another option was "MOD SELF DOWN. COMMENTS FROM A BORG MICRO$$$LOTH DRONE."

Finally, the side-bar on the blog very clearly states: "Disclaimer: I work for Microsoft."

Let the public discussion continue, but it's pretty clear to me that recent events shouldn't be part of it.

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Comments [1] | | # 
 Thursday, August 04, 2005
Thursday, August 04, 2005 9:35:35 PM (Pacific Daylight Time, UTC-07:00) ( )

F-Secure has reported on some recent work by Second Part To Hell on a Monad scripting virus ("First Vista Virus Found"). It's a misleading title, as it's an issue that affects any vehicle for any executable code on any operating system. There's an excellent treatment of shell script viruses on Virus Bulletin that covers this issue, but predates it by 2 years: Unix Shell Scripting Malware.

The fact that MSH is used as the execution vehicle is really a side-note, as it does not exploit any vulnerabilities in Monad. The guidance on shell script viruses is the same as the guidance on all viruses and malware: protect yourself against the point of entry, and limit the amount of damage that the malicious code can do.

To protect yourself against the point of entry, follow the guidance suggested by Microsoft's Malware FAQ:

The best way to stop viruses is to use common sense. If an executable computer program is attached to your e-mail and you are unsure of the source, then it should be deleted immediately. Do not download any applications or executable files from unknown sources, and be careful when trading files with other users.

To limit the amount of damage that the malicious code can do, try to limit the amount of time you run as Administrator / root.  Aaron Margosis has an excellent blog on how to run as non-Administrator.

Now, this isn't meant to be dismissive of the very real threat of scripting viruses. In the real world, it's very hard to protect yourself against the point of entry.

To combat this, Monad has three features to help: not installing a shell association by default, configurable execution policies (along with digitally signing scripts,) and not running scripts from the current directory.

In the past, many viruses have injected themselves into a user's system when they double-click on the file. This is especially true in the case of email attachments. Windows then looks for the program that understands the file, and tells the program to run it. This is known as a shell association. Double-clicking on a .txt file opens Notepad. Double clicking on a .html page opens your browser of choice. Our installer doesn't tell Windows that it understands .msh scripts, so double-clicking on a .msh file does nothing.

We also support three execution policies to help you run scripts only from publishers that you trust.

The first execution policy, "AllSigned," checks all scripts for a digital signature. Monad asks you if you trust that publisher to run scripts on your system. If you do, Monad will run the script. If you don't, it won't. If the file doesn't have a digital signature, Monad won't run the file. Monad contains functionality to let you digitally sign your own scripts to help you run in this mode. This will be our default execution policy past beta.

The second execution policy, "RemoteSigned," checks scripts origintating from the Internet for a digital signature. If a script originates from the Internet, Monad goes through the same process that it does in the "AllSigned" mode. If the script does not originate from the Internet, it runs the script. This is the mode that our betas are configured for.

The final execution policy, "Unrestricted," does not check the digital signatures on scripts. However, if a script originates from the internet, it will warn (and prompt you) before it runs it.

As for not running scripts in the current directory, Monad follows a policy similar to that of Unix shells: we do not run them, unless you explicitly ask us to. This prevents malicious scripts (with names such as dir.msh, or get-childitem.msh) from intercepting your otherwise innocent attempt to list the files in that directory.

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Comments [5] | | # 
 Wednesday, August 03, 2005
Wednesday, August 03, 2005 4:17:40 PM (Pacific Daylight Time, UTC-07:00) ( )

Just as a reminder, the first Monad technet webcast is happening today (9:30am PST to 11:00 am PST): http://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?EventID=1032277850&EventCategory=4&culture=en-US&CountryCode=US.

The next one is next week, I'll post a reminder then, as well.

I posted more about the Technet Webcasts here.

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Comments [0] | | # 
 Tuesday, August 02, 2005
Wednesday, August 03, 2005 4:13:31 AM (Pacific Daylight Time, UTC-07:00) ( )

I recently stumbled upon this blog entry that expanded on a piece I wrote a few days ago: Command Line Shortcut for Repetitive Operations. (Hankatsu?)'s entry is in Japanese, so I don't know what it says. In fact, for all I know, he or she could be making fun of me. In any case, the code included with the blog entry shows a quck way to download sequentially numbered files from the internet -- such as File001.jpg, File002.jpg, etc. That's a great use of the technique, and we can improve it even further with a useful script that acts as a download manager.

This was one of the first Monad scripts I wrote (about 2.5 years ago,) and I've faithfully ported it through every one of the many breaking changes that have happened since then :) It originally relied heavily on the Windows port of wget, but I was able to finally remove that a few weeks ago when I noticed that the .Net framework now supports the WebClient.DownloadFile() method.

It's one of my most heavily used scripts -- it's not very complex, but sure is useful.

## download-queue.msh
## Acts as a download manager, to download batches of files.
##
## 1) Create a directory, and place "download-queue.msh" in it.
## 2) Create a subdirectory, called "Queue"
## 3) Inside the "Queue" directory, place .txt files that contain only URLs in them.
##
## Download-queue.msh will use the name of the text file to create a new subdirectory.
## It will place the downloaded files inside that subdirectory.

## Ensure the System.Net and System.Web DLLs are loaded
[void] [Reflection.Assembly]::LoadWithPartialName("System.Net")
[void] [Reflection.Assembly]::LoadWithPartialName("System.Web")

## Keep on processing the queue directory, while there are batches
## remaining
while($(get-childitem Queue\*.txt).Length -gt 0)
{
 ## Get all of the .txt files in the queue directory
 foreach($file in $(get-childitem Queue\*.txt))
 {
  write-host "Processing: $file"

  ## Create a directory, based on the filename (minus extension)
  ## of the text file
  $name = $file.Name.Replace(".txt", "")
  $null = new-item -name $name -type Directory
  set-location $name

  ## Download each item in the file
  foreach($url in (get-content $file))
  {
   ## Strip the filename out of the URL
   if($url -match ".*/(?<file>.*)")
   {
    $filename = $matches["file"]
    $filename = combine-path "$(get-location)" "$([System.Web.HttpUtility]::URlDecode($filename))"

    write-host " Downloading: $url"
    $webClient = new-object System.Net.WebClient
    $webClient.DownloadFile($url, $filename)
   }
   else
   {
    write-host "$url is not a valid URI."
   }
  }

  ## Move the file list into the directory, also
  move-item (combine-path "..\Queue" ($file.Name)) .

  set-location ..
 }
}

For now, you're on your own for generating the queue files. Right-clicking "Copy Shortcut" in your browser is a great way to get URLs. Batching them this way is many times faster than downloading each file individually.

Here it is in action:

MSH:297 C:\Temp >md Queue


    Directory: FileSystem::C:\Temp


Mode    LastWriteTime            Length Name
----    -------------            ------ ----
d----   Aug 02 21:10                    Queue


MSH:299 C:\Temp >echo "http://www.leeholmes.com/blog/images/rssButton.gif" > Queue\LeeHolmes.com.txt
MSH:300 C:\Temp >echo "
http://www.leeholmes.com/blog/images/xmlCoffeeMug.gif" >> Queue\LeeHolmes.com.txt
MSH:301 C:\Temp >download-queue
Processing: C:\Temp\Queue\LeeHolmes.com.txt
 Downloading:
http://www.leeholmes.com/blog/images/rssButton.gif
 Downloading: http://www.leeholmes.com/blog/images/xmlCoffeeMug.gif
MSH:302 C:\Temp >dir LeeHolmes.com


    Directory: FileSystem::C:\Temp\LeeHolmes.com


Mode    LastWriteTime            Length Name
----    -------------            ------ ----
-a---   Aug 02 21:12                107 LeeHolmes.com.txt
-a---   Aug 02 21:12               1025 rssButton.gif
-a---   Aug 02 21:12               1486 xmlCoffeeMug.gif

Stay tuned -- in the near future, I'll write a post that shows how to parse all of the URLs out of a web page.

[Edit: I've updated the script, to make it a little less sensitive to URLs with funky characters.]
[Edit: I've now posted my link parser script, so you don't have to generate these files manually anymore.]

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Comments [0] | | # 
 Sunday, July 31, 2005
Sunday, July 31, 2005 7:23:37 AM (Pacific Daylight Time, UTC-07:00) ( )

Josh Ledgard recently wrote an excellent post: Two Ideas to Cut off Duplicate Questions in Online Forums. It deals with a problem that clogs almost every technical community: some users treat the group as their personal support oracle. This happens in online forums, mailing lists, IRC, blogs, and more. Heck, I'd say that 70% of the questions posted to our internal Microsoft mailing lists could have been solved with a few minutes of research. Got an error message? Post something to the group. Computer looking at you funny? Post something to the group. Not sure what to have for lunch? Post something to the group.

The community researchers that answer questions on Google Answers make their money off of exactly this type of ignorance. Although some of the answers require above-average searching abilities, others turn up on the first page of search results.

Hence the exhasperated shouts of RTFM, Just Google It, and others. Those don't help, though. They show you to be a pompous jerk who likes to belittle others. The solution is to either gracefully help change the user's habits (so that they do more homework before asking a question,) or seamlessly integrate the research into their habits.

Josh's idea covers the latter. He suggests that sites (such as the MSDN Forums) integrate a little live search feed to the right of the "Post a Question" form. It's a great idea.

Now, here's a little compressed version of the Microsoft product cycle. Being a Program Manager at Microsoft, he was nice enough to even draft a rough mock-up.

Now, being a developer at Microsoft, the next step in a project is to mock up a hack-and-slash technical prototype. At this point, you're really just trying to prove to yourself (and other technical folks) that the idea is possible. These prototypes also give you a basis from which to make a development time estimate for the feature. I've posted it here, although there was really no question that this specific feature is technically possible.

Next, you might want to construct a hi-fidelity prototype to show others. The guts are still hack-and-slash, but the interface is much more functional. Usability tests, and executive demos are a common target for this class of prototypes. I've posted it here.  HTML is a great prototyping medium for UI, as is Macromedia Flash.

When you understand enough about the feature to know what you want, the ball again sits in the Program Manager's court for detailed functional specification. From there, it goes to the UI team for detailed colour and layout specification. Finally, it goes to development and testing for the full-fledged implementation.

Comments [2] | | # 
 Friday, July 29, 2005
Friday, July 29, 2005 11:24:04 PM (Pacific Daylight Time, UTC-07:00) ( )

For those of you that haven't yet downloaded Monad through BetaPlace, you now have another option -- Beta1 of the WinFX SDK!  This is not an NDA release, unlike BetaPlace.  Here's a direct link to the download page.

"The WinFX SDK contains documentation, samples and tools designed to help you develop managed applications and libraries using WinFX, which is the set of next-generation managed APIs provided by Microsoft."

If you decide to install only portions of the SDK, Monad is one of the products installed in the "Tools and Build Environment" part of the package.

Let me say one thing, though.  We take your input very seriously.  The comments and feedback that you provide to us in the BetaPlace newsgroup helps us make the product better for you, and future customers.  If you install via the WinFX SDK, please still make the effort to let us know what you think via email, and comments in our blogs.

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Comments [2] | | # 
Friday, July 29, 2005 8:16:07 PM (Pacific Daylight Time, UTC-07:00) ( )

Today is System Administrator Appreciation DayWikipedia summarizes it as:

System Administrator Appreciation Day, also known as Sysadmin Day or SAAD (as in Happy SAAD!), falls on the last Friday in July. It exists solely to show appreciation to sysadmins and people with other similar occupations.
[...]
Typical observances of this holiday are to present gifts to your Sysadmin. These gifts include chocolate, beer, wine, electronic toys, video games, and cake & ice cream.

So, reward your local SysAdmin!  Open your Monad prompt, type get-process a few times, then go buy yourself chocolate, beer, wine, electronic toys, video games, cake, and ice cream.

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Comments [0] | | # 
 Thursday, July 28, 2005
Thursday, July 28, 2005 10:25:25 PM (Pacific Daylight Time, UTC-07:00) ( )

There are times when you might want to do the same thing many times at the command line.  You normally would use a counted for loop:

MSH:19 C:\temp\monad > for($x = 0; $x -lt 5; $x++) { do-something }

But here's a neat little trick to save some typing, if you don't care which iteration of the loop you're in:

MSH:19 C:\temp\monad > 1..5 | foreach { do-something }

This is a bloated and slow way to do a for loop, though, so don't use it in scripts.

The 1..5 expression creates an array of 5 elements, using the numbers 1 through 5.  Then, we pipe it to foreach-object -- which then performs "do-something" for each element in the array.

For more neat things you can do with arrays, type 
   get-help about_Array
at your prompt.

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Comments [3] | | #