Posts
49
Comments
83
Trackbacks
0
Wednesday, December 03, 2008
Running scripts that only work under 32bit cleanly in 64bit PowerShell

The scenario is as following: you have some script that can only work in 32bit powershell (the COM objects it calles only work in 32 bit for example) but you are used to running 64bit powershell. So what to do you do? Easy, just write your script to check if its being called from a 32bit process and if not, spawn a copy of 32bit powershell to run itself! Tha way you never have to think about calling 32bit powershell explicitly.

Here is the snippet:

   1: if ($env:Processor_Architecture -ne "x86")
   2: {
   3:         write-warning "Running x86 PowerShell..."
   4:         &"$env:windir\syswow64\windowspowershell\v1.0\powershell.exe" -noninteractive 
   5:              -noprofile -file $myinvocation.Mycommand.path -executionpolicy bypass
   6:         exit
   7: }
   8:  
   9: # rest of your code

 

Turns out I use this trick all the time as I frequently work with COM objects such as Office Charting Components which are only 32bit.

Technorati Tags:
posted @ Wednesday, December 03, 2008 12:08 AM | Feedback (1)
Monday, November 24, 2008
PowerShell version (sort of) of Unix’s du command

PS CTPv2 is finally performant enough with enumerating directories and files that its worthwhile to write deep file system traversal functions. Unix’s du was always incredibly useful for me when managing my files. Here’s the PS equivalent.

   1:  function du ($path = '.\', $unit="MB", $round=0) 
   2:  { 
   3:     get-childitem $path -force | ? { 
   4:       $_.Attributes -like '*Directory*' } | %{ 
   5:          dir $_.FullName -rec -force | 
   6:             measure-object -sum -prop Length | 
   7:                 add-member -name Path -value $_.Fullname -member NoteProperty -pass | 
   8:                     select Path,Count,@{ expr={[math]::Round($_.Sum/"1$unit",$round)}; Name="Size($unit)"} 
   9:          } 
  10:  }

Sample:

[dronagiri] F:\Vivek\>du .\ –unit MB 

Path                                       Count            Size(MB)
----                                       -----            --------
F:\Vivek\Custom UI                           275                  18
F:\Vivek\Desktop                            1151                 114
F:\Vivek\Documents                         20338                3521

 

Hope this is useful for your file management.

posted @ Monday, November 24, 2008 8:33 PM | Feedback (0)
Tuesday, August 19, 2008
Emacs powershell-mode.el updated to v0.5

Here is the updated file. The old v0.4 version can still be downloaded as well. Many thanks to Richard Bielawski for taking the time to update some things and fix bugs. Change list from Richard's diff:

  • Indenting fixed on <tab>
  • Elseif keyword added
  • Support standard comment func
  • Bug fixed when visiting files in major mode

I didn't do a thorough test, simply used it in the following scenarios: visiting file, auto-load, colorization, indenting via <tab> and editing.

Apologies for the dastardly delay--but if you have improvements and suggestions / snippets, I'd be happy to include them and give you due credit.

Thanks again Richard!

 

del.icio.us Tags: ,

Technorati Tags: ,
posted @ Tuesday, August 19, 2008 7:52 PM | Feedback (8)
Sunday, August 17, 2008
Quick Post: live.com is actually kind of nice...
Technorati Tags: ,

Just upgraded the blog so apologies for any downtime. Anyway, quick post to exclaim: live.com is really quite useable! I know, I know, I work for Microsoft so that's why I'm saying live.com is worth visiting... right?

No, not really. I don't do PR... :) Here is why I'm recommending it:

  • The background picture is entertaining and often leads me to keep in touch with latest news (like the latest results from the Olympics) 
  • Search results are really consistent with what I expect to see (YMMV)
  • Its FAST - the background image comes up fast, the search results are fast!
  • Image search is really fun to use (try searching for "Lolcatz")
  • Ever searched for "Rumour"? Its a misspelling (in US), live.com automatically corrects it. A bunch of other auto-correct features are subtle but useful.

Of course, I wish some other things should be fixed:

  • There are links on the page to "MSN", "Live Search" and "Windows Live". Huh?
  • Related searches returns wacky results sometimes (I ego-searched and got back a random list of people). At least explain how those were generated.
  • This is a nit for the map feature: it doesn't render the UI usefully in my 1024 x 768 Lenovo. I guess mapping is a high-res feature!

So try out live.com. Oh yeah, I heard they have a cashback scheme--haven't tried it personally so can't comment on that.

posted @ Sunday, August 17, 2008 10:27 PM | Feedback (0)
Sunday, February 10, 2008
PowerShell Book Recommendations

Someone asked me to recommend a few books, so here they are:

1) Windows PowerShell Cookbook by Lee Holmes (from PS Team)

2) Windows PowerShell in Action by Bruce Payette (from PS Team)

3) For Exchange specific stuff, I actually haven't tried any (makes sense as we were writing code most of the time :). Here are a few I dug up on the web, they look promising:

In fact, a few guys I know wrote a very nice book that should definitely be on the top of the list.

 

 

Here's to more books, fame and fortune from these fine authors!

posted @ Sunday, February 10, 2008 5:30 PM | Feedback (1)
Friday, October 12, 2007
ExchangeLabs will pwn you!

So its been a long time since I last posted (last December actually!). A lot has happened since then: Exchange 2007 has shipped, Vista has shipped, Office 12 has shipped... and I've been working on, well, new stuff. Since Terry unveiled on the team blog that we have a program called Exchange Labs, I might as well reveal that http://exchangelabs.com also happens to be the new stuff that I'm working on.

I can't totally mention specifics, except that we are enabling a service at ** *** *, which by itself is really cool. And that's just the begining, we're also going to work on *******, *****, ***, **, ***, **, ***** ** ****, *** ****, and not to mention ******* **** *******! Apologies for ****'ing out some words, the censors around here are totally unreasonable :-) Given the new focus, I'll be posting more service and services related stuff on the blog. 

But I still love and use powershell, so I'll continue to sprinkle odds and ends on the blog as well. Evan actually is a good guy to watch for more powershell goodies. Thanks for reading the blog and posting comments, I appreciate it very much!    BTW: The title is a side-effect of me spending lots of time with Halo 3. Here is the wikipedia entry for 'pwn'.

posted @ Friday, October 12, 2007 1:10 AM | Feedback (0)
Thursday, December 21, 2006
Announcing the Exchange 2007 PowerShell Scriptacular demo pack!

 

Dear Exchange 2007 Enthusiast(s),

I'd like to announce the Exchange 2007 PowerShell Scriptacular (download zipfile) demo pack. What is it you ask? Well... this collection of Exchange PowerShell scripts was developed by Mihai Jalobeanu and Vivek Sharma while they (we) developed the Exchange Management Shell. We had a lot of fun writing these scripts, and we used them to showcase the innovations in Exchange 2007 System Management--we also promised that when Exchange 2007 is released to the wild, that we'd make our favorite scripts available to everyone. So here they are!

If you want our exact demo setup: to get started, put everything in a directory (d:\scripts or something), and run generate. You need datafiles and you need to customize initvars.ps1. If you don't, the demo pack won't work.

Or you can just take a look at the scripts and learn from the bits and pieces that interest you.

Note: These are Microsoft copyrighted samples, are provided as-is and confer no warranties or rights.

Script Pack Contents: 

  • addfakeservers.ps1 and removefakeservers.ps1

    NOT standalone. Makes it looks like your demo setup has more than one server. We use a very useful feature of Exchange 2007 setup to make this work. Basically we use the /provisionedserver switch to create pre-provisioned servers (AD objects). The admin interface detects these as servers, even though the hardware has not been installed yet. This enables server delegation and pre-provisioning in  Exchange topologies.

  • balancemailboxes.ps1

    Standalone. This script shows how you can use PowerShell to easily load balance mailboxes by size across servers. Key thing is the use of get-mailboxstatistics and move-mailbox.

  • cleanup.ps1 and generate.ps1

    NOT standalone. Cleanup.ps1 completely restores your demo environment to a basic-just-installed-Exchange state. New car smell not included. Generate kicks off everything else to generate the demo environment. 

  • initvars.ps1

    NOT standalone. Standard variables shared by all the scripts. Called by generate.ps1. MAKE SURE TO EDIT THIS FILE to customize this for your environment.

  • mailstorm.ps1 and mailstorm2.ps1

    Standalone with caveats. What's a demo Exchange enviroment without some email? BTW: this is the one script that is likely to not just work automatically in your environment. This is because we assume some basic auth settings to make the Hub server accept mail. This step is done in generate.ps1 so make sure you do that if you plan to use this script by itself.

  • multi-matrix.ps1

    Standalone. Totally totally useless. But cool damnit. This is the matrix unloaded ... er... powershelled.

  • out-email.ps1

    Standalone, with caveats. Just like the name implies, sends content over mail. You can pipe text to it. get-content c:\sometext.txt | out-email -to jimmy@crackcornandidontcare.com -subject "Report". It uses the "from" from initvars.ps1 so make sure to copy those lines as needed to make it standalone.

  • out-html.ps1 and out-ie.ps1

    Standalone. I already posted these to http://www.viveksharma.com/techlog earlier.

The datafiles are politician names, committees, standard drafts of the European Union. We've used US congress / house data in the past too---anything goes, it was publicly available data so it works great for demos.

The scripts (and their latest versions) can always be found at: http://www.viveksharma.com/techlog.

-Mihai Jalobeanu and Vivek Sharma

posted @ Thursday, December 21, 2006 6:12 PM | Feedback (13)
Sunday, October 22, 2006
How to get DL membership in Exchange 2007

Abshishek reminded me of this with his post on how to figure out Security Group membership using PowerShell. But Distribution Groups are a whole different beast. So how do you do it? One way is to use the GUI: the awesome Exchange Management Console already shows you group membership. And on the cmdline you can do this in Exchange 2007:

Using server side filtering (faster):

   $filterid = (get-user Administrator).Identity get-group -filter { Members -eq $filterid }

Using client side filtering (slower):

   get-group | where { $_.Members -like '*Admin*' }

This can be adapted to do nested membership as well---I'll leave it to you to figure out how.

posted @ Sunday, October 22, 2006 11:10 PM | Feedback (3)
How to record your shell session

PS> Start-Transcript
PS> # do a bunch of stuff
PS> Stop-Transcript # or just quit the shell

This will create a file (by default in your My Documents folder) that is a verbatim copy of your shell session. No more copy and paste needed from the shell to notepad! A few other things of note:

  • This is the only way to capture verbose and warning information as those cannot be redirected from the shell like errors can (don't ask me why, but make sure to tell Jeffrey Snover about this)
  • You can start a transcript by default every time you start your shell by putting 'start-transcript' in your profile.ps1 file
  • There can change the location of the text file that is created. Look at get-help start-transcript for more details
Have fun! 
posted @ Sunday, October 22, 2006 11:10 AM | Feedback (0)
Friday, October 20, 2006
Looking for feedback on PowerShell and Exchange
Every once in a while its important to take a step back and see where we can improve---I already have a list of around ~150 things I'd like to improve in the Shell (Exchange and PowerShell), but I'd like to hear what you a) like about the shell and b) would like to see improved. You can leave comments, trackbacks on your own blogs, or mail me directly at (munging the address to defeat the spammers): vivek (at) thiswebpage'sdomainname.com   Here are some examples I found recently:
  • Devin comments on how easy it is to move databases through cmdline and wonders why it isn't easy to move queues as well.
  • Newsgroup commentary from a user on how much it rocks to use the Exchange cmdlets to do management, and why AD management from PowerShell isn't just as easy (I don't work for AD team, but rest assured, I'm still working hard on making this happen).
So let us know what you like (or like a lot) so we can continue making it better and what you would like to see improved / fixed / added so we can plan for our future releases or work up some cool community snippets (after all, that is one huge advantage of the shell). Remember, if you don't speak up, I can't hear you, and thus I can't do anything about any issues :)  
posted @ Friday, October 20, 2006 10:10 AM | Feedback (0)
News
A little slow these days as I'm busy working on exchangelabs.com. I will try and post tidbits when I get some time. Enjoy the older posts till then!