Tuesday, November 18, 2008

ZoneAlarm Pro for free

You can get ZoneAlarm Pro for free today - however be careful, it is NOT full security suit as many sites are reporting, it is "only" firewall & antispyware suit (which I have no use for - both are already included in Vista by default)...

Anyway, if you find this information useful, so grab a copy while it is available ;)

Saturday, August 2, 2008

Returning values from Fuctions in PowerShell... Tricky :(

First let me explain something: I LOVE POWERSHELL...

No, I really do. After 3 years working every day with batches, vbscript & .NET I can see huge huge potential in PowerShell and more and more I use it I think it is genius shell.

However I am always trying to be skeptical about everything I do - and about every product I love.

I don't want to end up like some people that think their favorite product is PERFECT - and when there is new version, they suddenly realized that something that was PERFECT is more PERFECT ;)

 

I worked with Monad\PowerShell a lot 3 years ago, then I stopped and returned to batches (because of my work). It was quite fun and it turned out that with good imagination you can still achieve a lot with batches (script blocks, complex scopes, private variables, subroutines, functions etc ;)), however it was always just about building some workarounds. 1 month ago I returned to PowerShell and now I am working with it on daily basis - and of course I already encountered some things I don't really like. 

Working with function output

If you are familiar with programming, following code should be easy to understand for you:

Function bar {
$MyVariable = "Foo"
Return $MyVariable
}



Function bar returns MyVariable. So using $X = bar should be equivalent to $X = $MyVariable in fact.



Not in PowerShell. $X will get output from WHOLE function. That means that following function is equivalent to one above:



Function bar {
$MyVariable = "Foo"
$MyVariable
Return
}



I really, really don't like this behavior. Creating complex scripts is getting much more complicated. Consider example where you run into some problems in bar function. Most primitive debugging is always to use Echo (Write-Host) and just see value itself.



If I would have enough time, I would change it as follows:



Function bar {
$MyVariable = "Foo"
Write-Host "DEBUG: $MyVariable"
Return $MyVariable
}



Result would be as expected - if I will use $X = bar, $X will be Foo. However what if I try following:



Function bar {
$MyVariable = "Foo"
"DEBUG: $MyVariable"
Return $MyVariable
}


Looks fine? But doesn't work - $X in this case is Foo Foo and you will never see "DEBUG: Foo" output in console. I really think this is against object-oriented principle of PowerShell and it reminds me of 'For /f' behavior in batches.



You probably won't run into this problem - however you will run into it if you will start creating really complex scripts with XML handling, adding new elements etc. Usually (in .NET), if you for example add something to array, index is returned.



Look at following function. Variable $MyVariable is created, two numbers are added (1 and 2) and then it is returned:



Function bar {
[System.Collections.ArrayList]$MyVariable = @()
$MyVariable.Add("a")
$MyVariable.Add("b")
Return $MyVariable
}



Normally you would expect to get array that contains 'a' and 'b'. But because of PowerShell way of returning object, instead you will get array with 4 (!) entries: 0 1 a b.



0 and 1 in this case is index of added array elements. 



To make this function work as expected, you will need to redirect output to null:



Function bar {
[System.Collections.ArrayList]$MyVariable = @()
$MyVariable.Add("a") | Out-Null
$MyVariable.Add("b") | Out-Null
Return $MyVariable
}



Usually I try to write all my functions in following format:



Function <Name> {
<initial checks>
...
<final checks before return>
Return
}



With PowerShell this is usually not possible, because even if you check if $MyVariable is correct type with correct values etc, it is quite hard to know if something "leaked" before. 



For me ideal solution would be that PowerShell would support BOTH methods for returning:



Function bar {
$MyVariable = "Foo"
Return $MyVariable
}



Would return object $MyVariable



Function bar {
$MyVariable = "Foo"
$MyVariable
Return
}


Would return whole output from function



Friday, August 1, 2008

How to manage your small development projects??

Some of you are maybe hobby developers as I am... I am partially developing for my company and also I have few free public projects like Elevator.

There are 3 tools that I always must have when I work with my projects:

  • Subversion - used for versioning, very easy to setup and use, available for free, no need for database, integrated to shell.
  • Visual Studio - well, of course :)
  • PowerGUI - it contains very nice PowerShell script editor

If you have however some professional projects (that means projects with fixed deadline for me ;)) and you are not working for professional developer company, it can be pretty tough to keep project on track and not spend days implementing small features and tweaks. It is very important to organize your time and keep track of everything you have to do.

At beginning I was "wild" developer - that means no versioning system, all notes in txt files and backups and rollbacks implemented by copy&paste (or popular ".old", ".bak", ".backup", ".001" etc..). Of course this was long time ago.

Later on I started working with Subversion and storing my notes in Visio. Still, that was not ideal solution.

Few days ago I discovered Unfuddle webpage. Unfuddle is project that is used for hosting - it provides you with basic bug tracking system (that meets all my needs), subversion hosting, notes taking system...

And best of all - they have also free edition. In free edition you are limited to 1 project and 2 people, however still it is great way to discover Unfuddle.

Tuesday, July 29, 2008

MultiMon utopia

At my previous blog I wrote about tools and utilities I am using and also mentioned that I want to replace some of them...

One of tools I mentioned that I want to replace was some utility for multimon management.

Being partially developer\scripter, I really love multiple monitors. However in Windows using multi mon is not as easy as it could be :(

Ideal scenario

In ideal scenario I would like to have 3 monitors - one main for VS\Script editor & Outlook, one for tons of small windows (as mentioned, Live Messenger, Last.FM, Skype...) and one for internet\documentation. I would like to have this setup because I definitely want to have my primary working tool (editor) fullscreen, see all small windows at once and have (again fullscreen) access to articles and documentation. So from hardware perspective I find 3 monitors best.

I definitely need option to easily move windows between screens - which is quite painful in Windows, because you must make full drag&drop and this is not supported when window is in fullscreen. Obviously this is not supported by Windows natively (shame, shame...), so I need some external utility for this.

Talking about replacement for default Windows functionality, I want following:

  • Easy option to move window to another monitor (HW)
  • Easy option to move window to another screen (virtual desktop)
  • Separate taskbars for each monitor (and virtual desktop)
  • Ability to minimize windows to screen instead of taskbar. Ideally having one virtual desktop to store all minimized windows.
  • Centralized notification system (for windows that are on other desktops)
  • Virtual monitors

Current setup

Currently I have 2x22" (1680x1050) and my laptop (1920x1200) connected through Synergy and I can imagine getting third monitor ;) On one monitor I run Visual Studio\PowerGUI Script Editor together with Outlook and on second I have all other utilities like Messenger, Last.FM player, Skype etc...

For moving windows between monitors I used trial version of UltraMon, however once it expired, I was not sure whether it is what I was looking for - not many features and quite expensive.

I have no virtual desktops - I tried to use them few years ago, however it just didn't work for me (but my situation changed of course).

I have my tablet and laptop connected through Synergy with my primary desktop. Synergy is application that allows you to control multiple computers by 1 mouse and keyboard (virtual KVM without V ;)). It is easy to configure (when mouse reaches end of primary monitor on left, just to computer XXX) and it really works.

Solutions?

Ok, lets go step by step. I will start by basic introduction to 3rd party utilities that are most often used for multi monitor support:

  • MultiMon - small application that is available for free. It allows you to easily move window between monitors using easy to use title buttons. Also it creates multiple taskbars for each monitor. Small feature (important for developers) is extended clipboard, where you see all (text) entries you saved to clipboard and you can easily restore code you added to clipboard some time ago. Main disadvantages are that it doesn't really work with Vista (at least for me) and there is paid version - where all bugs are fixed :( So if you are using XP and are looking for some free solution, I can highly recommend. Price for PRO version is $28.
  • UltraMon - recognized as best multi monitor solution, however I was not really satisfied. In fact only features it adds is multi mon taskbar and title buttons to move window to other desktop. Otherwise it works fine - but I don't think $39.95 is really good price.
  • Actual Window Manager - this is not very famous piece of software, however I find it best. It is most expensive ($49.95), however it have great functionality and tons of small features and tweaks. Because MutliMon and UltraMon are quite famous and AWM not, I will write in detail about it below.

Actual Window Manager

So, I mentioned that I really like AWM (Actual Window Manager), so I want to share some details with you. Information is based on 5.2 beta 1 version (so final 5.2 could have some features added).

At first I was searching only for 2nd taskbar + move to window functionality, however AWM is more like monitor Swiss knife.

AWM provides more than 40 tools for manipulating with Windows (according to their website). Some features (second taskbar, move to monitor, virtual desktops, process priorities...) are very interesting for me, some (transparency settings, stay on top...) are interesting, however not for me and some (rollup, minimize to screen, pin to desktop...) could be very interesting for me, however they would need to be slightly modified to do exactly what I want.

There is trial version available, I highly recommend to try it and explore little bit. Visually AWM is not very nice, however from functionality perspective it is perfect.

There are also many nice tweaks and tunes that are not obvious immediately, for example modifications to cmd.exe that allows you to dynamically resize it or special right-click behaviors. Once you will start digging into configuration, you will be amazed how much can be achieved by AWM.

Consider simple configuration for minimizing. AWM allows 3 minimization modes:

  • minimize to taskbar (default Windows behavior)
  • minimize to systray (I prefer this, I used TaskSwitchXP to achieve that)
  • minimize to screen (creates small icon representing application on screen. Especially with DWM this could be great enhancement, sadly current version creates only static icon)

That's quite nice, however you can specify other settings. For example you can specify that you want to automatically minimize window 1 minute after it was deactivated (or immediately). Or you can specify that if you left-click on Close title button (X), it will only minimize and if you right-click on it, window will close (and of course you can enable this only for particular applications).

Power of AWM comes when you combine different tools (features) together. For example transparency was never really interesting for me. In AWM you can not only configure transparency, but also Ghost mode (which means that anytime you click on application, click goes to application BELOW ghosted application). So using this you can easily create transparent (status, information etc) windows that ignores clicks and you can (using AWM Always on top functionality) keep them in front. Or you can specify special CPU priority when application is minimized or inactive. Or you can specify default sized\monitors etc for each application...

It takes some time to start with AWM (because you have so many options), however it is definitely worth every penny. 100% recommended (hope so I won't change my opinion soon ;)).

Another really nice thing is that I used trial version. Once it expired, I decided to uninstall it and wanted to try UltraMon. After uninstallation I was asked what I didn't like and why I uninstalled that application. I decided to be honest and wrote what I was missing (taskbar and DWM-based minimize to screen). Soon I received answer - not the usual "thank you for your feedback blabla", I received answer - taskbar is going to be implemented in 5.2 version and second request was registered in wishlist.

I played a little bit with Virtual desktops, however I am missing some features there:

  • Easy way to switch desktops (Linux\Microsoft PowerToys like systray)
  • multi monitor aware virtual desktops - I want to use ONLY secondary screen for virtual desktops. AWM instead switches both screens to new virtual desktop. Again, based on WDM, I would like to have live preview of all virtual desktops (so if not needed, I would like to see live previews of all virtual desktops on my second screen).

So let's have a look at features I wanted when I count in AWM:

  • Easy option to move window to another monitor (HW)
  • Easy option to move window to another screen (virtual desktop)
  • Separate taskbars for each monitor (and virtual desktop)
  • Ability to minimize windows to screen instead of taskbar. Ideally having one virtual desktop to store all minimized windows. (however not DWM based)
  • Centralized notification system (for windows that are on other desktops)
  • Virtual monitors

    UPDATE: Version 5.2 of Actual Window Manager supports also taskbar on each monitor, so now AWM is definitely my recommendation. 

    Centralized notification system

    This is one feature I really miss in Windows. Do you know pop-ups from Live Messenger or Outlook whenever you receive new mail\instant message?

    I always wanted to have centralized system for this that will be supported by all Windows applications. Something like Growl for Mac OS.

    Simple interface, where you specify some default settings (timeouts, stickyness...) and subscribe to events you would like to receive (for example Outlook\New mail, Outlook\New RSS, Live Messenger\New message, Total Commander\Copy finished etc).

    There are of course some alternatives like Snarl. Problem is that all such solutions that are not VERY WELL known will have very limited support (which is also case with Snarl).

    Notification system is quite important once you start playing with stuff like Virtual Desktops - you can't just place instant messenger to (hidden) virtual desktop if you want to be aware of what is happening :(

    Currently there is no real solution available... Microsoft, are you listening? ;)

    Virtual monitors

    Virtual monitor is not virtual desktop. Virtual monitor should allow you to turn any device (old laptop) into additional monitor. Most famous application is called MaxiVista. It can also replace Synergy and add more features, however it is not for free ($39.95). Supported features are:

    • Extended screen a.k.a. software monitor
    • Remote control (Synergy)
    • Clipboard synchronization

    Don't get confused by name - MaxiVista is not designed for Vista (and therefore doesn't take advantages of WDM and is using only XPDM) :(

    What I need virtual monitor for? I really like Synergy for my laptop (why using it as monitor when I can use it 4GB Ram and dual-core processor for some tasks meanwhile), however I have also pretty old tablet (800MHz, 256MB ram...).

    I really prefer to read books\documents and make drawings at tablet, however it is too old. So what I would really like to have is tablet, that will act as software monitor, however I could use controls (pen) from TabletPC.

     

    As workaround I could connect through remote desktop, however having ability to drag&drop document to tablet would be really great.

     

    Martin

  • Wednesday, July 23, 2008

    PowerShell problems with transcript

    I really like Start\Stop-Transcript functionality from PowerShell...

    However I already encountered few problems:

     

    1.) Transcript doesn't save output from exe files

    2.) Log file is not creating new lines (one long line instead)

    3.) If script fails, Stop-Transcript is not automatically executed

     

    Transcript doesn't save output from exe files

    Yes, that's right. If you run following script:

    Start-Transcript c:\temp\testscript.log
    "Starting"
    IpConfig
    "Finished"
    Stop-Transcript

    You see different output in log file than on screen. Workaround is pretty simple, but functional:

    Start-Transcript c:\temp\testscript.log
    "Starting"
    IpConfig | Out-Host
    "Finished"
    Stop-Transcript

    You pipe output from executable to cmdlet and it is automatically saved to your transcript log file.

    Log file is not creating new lines

    I experienced this few times and not sure why it happened. If you will run into this problem, you can use my workaround:

    Instead of

    Write-Host -ForegroundColor $Color -Object "Test"

    Use

    "Test" | Write-Host -ForegroundColor $Color

    Stop-Transcript is not executed

    It is not really problem, however you shouldn't forget about it. Easiest way is to include Stop-Transcript in your trap statement:

    Trap {Continue} Stop-Transcript

     

    Have you experienced any other problems with transcript?

    Tuesday, July 15, 2008

    Filter in PowerShell

    I was reading yesterday about filters in PowerShell - I noticed it in one script and didn't play with it before, so I was really curious...

    This reading help about filters (Get-Help about_Filter -Detailed), I was getting more and more excited.

    I understood that you can specify complex filters here, something like

    Filter MyCustomFilter {
    $_.ProcessName -match '^a.*'
    $_.CPU -gt 20
    }


    Get-Process | Where {MyCustomFilter}



    That would be really cool, because that you could easily provide filtering in some XML definition.



     



    Obviously I didn't get it right. After studying a while PowerShell in Action (best book about PowerShell for me, highly recommended), I realized that Bruce is using filter to alter data:



    PS (1) > filter double {$_*2}
    PS (2) > 1..5 | double
    2
    4
    6
    8
    10



    Consider following filter:



    Filter StartWithA {$_.ProcessName -match '^a.*'}



    It should be able to filter all processes and display only ones that starts with a.



    If we try to use it Get-Process | StartWithA, we will get array of booleans:



    PS C:\Links\HDDs\DevHDD\WorkingProjects\S4Maintenance> gps | StartWithA
    True
    True
    True
    False
    False
    False
    False
    False
    False
    False
    False
    False
    ...



    If we try to use Get-Process | Where{StartWithA}, we don't get anything in return.



    If we use change this filter to



    Filter StartWithA {$_ | Where {$_.ProcessName -match '^a.*'}}



    then we finally get what we wanted:




    PS C:\Links\HDDs\DevHDD\WorkingProjects\S4Maintenance> Get-Process | StartWithA | Select Name

    Name
    ----
    AcroRd32
    alg
    audiodg



    It appears to be bug in PowerShell documentation - however it would be great if we could see filter as array of conditions in v2 ;)



    Thanks to Mow for {$_ | Where {$_...}} example

    If (object exist) {...} in PowerShell..

    In VB.NET I always used If Not Object is nothing just to be sure that I am working with real object.

    In PowerShell, this can be done very easily:

    If ($Object) {...}

    or

    If (!$Object) {...}

    Easy, powerful and useful, I love this :)

    Martin