Friday, June 1, 2012

Programming with PowerShell, or not?

As working on a project which needs to execute some code for a long time. It is very difficult for me to make the code totally bug free. Two problems bothered me for a long time… one is memory leak, and the other one is the unreleased system resources, such as socket and thread. I spent few days on debugging and evaluation, but I found it is really hard for me, a graduate student who has to work on a ill implemented codebase and has no knowledge about software testing, to make it perfect. So I decide to split the tasks into groups, and use a shell script to call my C++ code. Then terminate the program before his crash and initialize another one.
One possible solution is PowerShell, which is very powerful on Windows. I wrote the following code:
   1:  # checks the number of inputs and only accepts one input
   2:  if( $args.Count -ne 1 )
   3:  {
   4:  # print usage information
   5:      Write-Host "Usage:" $MyInvocation.MyCommand.Name "path"
   6:  # exit
   7:      exit
   8:  }
   9:  # the first input is the path to the targeting directory
  10:  $directory = $args[0]
  11:  # find the number of sub-directories 
  12:  $folders = Get-ChildItem $directory;
  13:  Write-Host $folders
  14:  # a fixed step size, which is 10
  15:  $step = 10
  16:  # checks all the sub-folders, such as 10 to 10+10, or 30 to 30+10
  17:  for($from=0; $from -lt $folders.Count; $from=$from+$step)
  18:  {
  19:      $to = $to+$step
  20:  # call my code and pass the parameters
  21:      ./WebSpider.exe $directory 2000 1 $from $to
  22:  }

When I want to run the script, there is an error popped up, which says the script file cannot be load because the execution of script is disabled. WTF… Are you guys at Microsoft kidding me?! You implemented a shell interface, but disabled it to users?! You need administrator authority to enable it by the following command, otherwise, you cannot run scripts with PowerShell.
Set-ExecutionPolicy RemoteSigned

Powershell -ExecutionPolicy RemoteSigned
You could start power shell as above to enable script executions without administrator authority.

The students, like me, do not have administrator authority on the desktops in the lab. So I cannot use the scripted…

Finally, I decide to rewrite a batch version instead.

   1:  @echo off
   2:  IF "%1"=="" goto stop
   3:  set path=%1
   4:  echo %1%
   5:  set start=0
   6:  set step=10
   7:  rem count the number of folders
   8:  setlocal EnableDelayedExpansion
   9:  set /a count=0
  10:  for /d %%d in (%path%*) do (
  11:  set  /a count+=1
  12:  rem @echo !count!. %%d 
  13:  )
  14:  set /a end=%count%
  15:  ECHO Folder %end%
  16:  :loop
  17:  if %start% GTR %end% goto stop
  18:  set /a cur = %start%+%step%
  20:  rem echo from %start% to %cur%
  21:  rem run the code
  22:  WebSpider.exe 2000 1 %start% %step% %1%
  24:  set /a start = %cur%
  25:  goto loop
  26:  :stop
  27:  rem 

I used to admit PowerShell very much, for its integration with DotNet and extendable command-let. However, I just cannot understand why Microsoft disables his own product by default?! You may argue that the users on Windows may make mistake and … It does not make any scene, either. If a shell interface cannot run script, can you find any other functionalities we need, from a shell?!


  1. I agree that it is not obvious why we do that. If you recall, many of the computer worms were script based (ILoveYou.Vbs). PowerShell is very powerful and very safe. We take the approach that we inform the user of the power/risks of something and then let them make their own decision. That means that scripting and remote management are disabled by default.

    You are correct that it requires an admin to change the default policy for the entire machine but any user can change it for a particular session. Just start PowerShell this way:

    Powershell -ExecutionPolicy RemoteSigned

    That will start a session that will let you run the script even if you are not an admin.

    Jeffrey Snover
    Distinguished Engineer and Lead Architect for Windows Server

    1. Thanks for your reply and information:) PowerShell is my first shell all the time.
      BTW, I think MS should put more efforts on promoting powershell. Most of my friends may say how powerful bash is. However, if I show some tricks in powershell, they will be highly shocked and try to learn powershell.