Wednesday, July 30, 2008

Powershell: Confirmation Level

Part of the scripts I'm making for work involve the update of several Exchange Distribution lists in an automated way.
I was on this when I run into an unexpected behavior: the Remove-DistribtionListMember Command was asking for confirmation, even when I had not specified the -Confirm parameter .

A glance to the exchange.ps1 reveled that, in effect, the Powershell environment for exchange has the confirmation Preference set to HIGH by default (thus asking for confirmation on any task that performs a important change to AD)

Changing the confirmation level it's as easy as :

$ConfirmPreference = 'High' # The allowed values seem to be High, Medium, Low, None.


But, I didn't like the idea of changing the value for the whole environment, who knows what would I like to run next ...

So decided to implement a simple temporary variable to store the value of $ConfirmPreference on the script's start and revert it to normal just before the end.

$ConfirmPreference =$OLD_ConfirmPreference

... # My Code here


$OLD_ConfirmPreference =$ConfirmPreference

My next thinking was on how cool would it be to be able to implement a confirmation switch on my own scripts, and I realize that it's easy to do:

Param(
[switch] $Confirm
)

$OLD_ConfirmPreference =$ConfirmPreference

if ($Confirm) {
$ConfirmPreference = 'High'
} else {
$ConfirmPreference = 'None'
}

...... # My Code here

$ConfirmPreference =$OLD_ConfirmPreference



Now I can have (and give to my script users) control on the Powershell confirmations.


Tuesday, July 29, 2008

The PATH in Powershell

One of the things I wanted to do when I had a Blog was to post a recap of the interesting things I learn periodically, specially if it took me a while to learn them or if there was some complications for me to understand them.
I will start this today with this posting.


The PATH in Powershell

Maybe because I never formally read much about powershell, maybe because I did not bother enough to find it sooner, or for whatever reason, I had never found out what to do in order to be able to execute my scripts as just another PS command, meaning be able to use
my-script
instead of
. my-script.ps1

Yesterday I decided to research on it and after some goggling I got a clue: The Script's directory MUST be in the PATH, meaning that even if you are standing right in the Script's directory, you won't be able to execute it as a command unless the directory is in the PATH. Crazy, isn't it ???

Okay,now that I knew that I need to figure out 2 things:
  1. How to read the path from Powershell (I found that Path and $path did not work), and
  2. How to change it (To be able to add my own directories)

A little more googling revealed that to get the PATH I only need to query the Environmental variables:
$env:path

Easy, right ???? ... Then, Why didn't I figure it our sooner ???

To add items to the pat, it's just as simple:

$env:path = $env:path + ";E:\My Scripts\Powershell\"

Now, that script that i was only able to execute as:
& "E:\My Scripts\Powershell\my-script.ps1"
can now be executed as:
my-script


To see if an specific directory is part of the PATH, we can use simple string functions:

($env:path -match 'My Scripts\\Powershell')

(Notice the double slash in order for the slash not to be considered as a REGEX command)

Well, not "rocket science", but it did took me a while to put all the pieces together, and I hope that I'll save some time to the next person trying to accomplish this.

Blowing candles with martina and luis

Posted by Picasa

Sunday, July 27, 2008

Birthday

Yes, it's my birthday.
The english word sound, to me, much better than it's spanish counterpart ("cumpleaños").
After all, that's what it is, a reminder of the day of birth, the same day of the year that you were born, not just "on other year on your back". To tell the truth, I do not really care about my age, for me the years are like my gray hairs, part of life.

Going back to the issue of the title, it has a double implication, it's not just my birthday, but also the birth day of this blogs. For one reason or another, I haven't entered this world before ... at the begining I was thinking that it was only for very special and very smart people that had important things to say, and later I was thinking to do it directly on my (forever unfinished) website.

This year I had it all planed, to launch on my birthday the finished website and a blog (hosted in Blogger, but crossposted to my site) ... but technology comploted against it and I was unable to finish the things I absolutely want to have in my website, so about 90% of what I've done so far will remain hidden for a while ... but I decided that I would at least launch this blog ... and ... well, here it is.

Those that know me well, can see that this is a very special birthday celebration for me ... or at least an uncommon one. Instead of the house full of friends until late hours and the famous "picadas MOK" (let me translate that as LOTs and LOTs of finger food), this year was a simple family reunion ... and, I must admit, I wasn't even planning to do that ... My plan was to sleep late on sunday, have breakfast in bed, and rest all day :-) .
But the family was not happy about it and, truth be told, I'm really glad that I had them over tonight.

They left a few minutes ago, and I decided that, before going to bed, I was going to make my "debut" in this new way of communication.


I will soon write again so you can know a bit more about me and my crazy ideas ...

Best regards to all,
Mariano, MarianOK, MOK