Thursday, January 24, 2008

Winter Scripting Games: Profiles in Perfection

The Scripting Guys are putting on their 3rd annual Scripting Games from February 15 to March 3, 2008.

As part of the preparations for this year's contest, people who scored a perfect score were invited to send in a profile of themselves.

Today was my turn.  My profile is HERE.

The challenges are super fun.  Are you up for it?!

Basically about 10 different challenges are posted, and due dates are given.  You must create a script to solve the challenge, and send it in to the Scripting Guys for them to check and score.

They also have scores per country, highest scores, etc.

Friday, January 18, 2008

Copying and pasting long strings into the console

In the PowerShell NNTP newsgroup, a user had a long string of code that covered multiple lines (had line breaks), and the user was attempting to paste the code into the PowerShell console.

The string was a long Exchange 2007 command.  Because of the line breaks that were included in the copy and paste, the command string would not run in the Exchange Management Shell.

A very simple example, yet still similar, of what the user was copying and pasting from Notepad (or some other text editor/application) was:

write-host
"testing"
-foregroundcolor
"red"

So if I copy this from Notepad, then paste it directly into a PowerShell console, and try to run it, it will fail when it hits the -foregroundcolor line.

So, just for fun, I thought there must be a easier way to do this, and started looking at HERE strings.

So from, PowerShell, I typed:

PSH>$multi=@' [hit enter]
[copy and paste the code from Notepad as is]
>> '@ [closed off the HERE string]
>> [hit enter]

Now, I have a string named $multi with all the data I need:

PSH>$multi
write-host
"testing"
-foregroundcolor
"red"

Now, my only problem is that it is on separate lines, but it is still a string object.

PSH>$multi.gettype()

IsPublic IsSerial Name
-------- -------- ----
True     True     String

I need to combine this all into a single string, then I can use invoke-expression to run it.

So, I need to loop through every single character in $multi, and combine it into one single line.  I have to do a something a little fancy with the return character.  To easy the change, I also turn it into a string, then I have the replace method available.

So here's something I put together:

for($i=0;$i -lt $multi.length;$i++)
{
  if($multi[$i].tostring() -eq [char]10)
  {
    [string]$join+=$multi[$i].tostring().replace([char]10,[char]32)
  }
  else
  {
    [string]$join+=$multi[$i]
  }
}

 

After I run this, I get:


PSH>$join
write-host "testing" -foregroundcolor "red"


Finally to invoke it, I just do:


PSH>invoke-expression $join
testing [output is red]


Obviously, I have to reset $join to nothing to reuse it again later:


PSH>$join=""


Hopefully, this helps others when trying to do quick copy and pasting into a PowerShell console.

It would be good to create a function of this, and likely define the input and output strings as arguments.

I'll do just that in a future blog post.

Update: January 24th, 2008.  Check the comments on how to drastically simplify this.  Since a HERE string is a string object, I should have just used the replace method on the entire HERE string, instead of doing a for loop at all...

Tuesday, January 15, 2008

NetCmdlets now free for non-commercial use

Lance Robinson has just announced HERE that /n software is now offering a free non-commercial license of their NetCmdlets product.

NetCmdlets offers more than 30 networking related cmdlets for use with PowerShell.  See the listing HERE.

Lance has also provide cool examples of using NetCmdlets on his blog.  He has setups tags so you should be able to see most of them HERE.

I also have a few examples on my blog here and there.  If I find the time, I'll try to figure out how to add tags to my blog...

Sunday, January 13, 2008

Some news from an established PowerShell vendor soon...

I've heard that an established vendor in the PowerShell 3rd party add-ons space will be releasing a new and exciting upgrade of their product.

Hold your breath for the next 1 or 2 weeks for all the news and examples that will be coming.

Wednesday, January 9, 2008

Windows PowerShell Virtual User Group Meeting #3

Windows PowerShell Virtual User Group Meeting #3

Time: January 16th, 2007 at 8PM EST (New York time)
Place: Online via Live Meeting 2007

-----------------------------------------------------------
To sign up:
-----------------------------------------------------------

Registration site: http://www.clicktoattend.com/?id=123866
Event Code: 123866

-----------------------------------------------------------

This is our third virtual event.  Invited speakers that will talk about various PowerShell-related topics: Lee Holmes (Microsoft), Karl Prosser (MVP) and Brandon Shell (MVP).

To join:
https://www119.livemeeting.com/cc/winserver_usergroup/join?id=W8GTKG&role=attend&pw=kNgb%3F%5D5

Unable to join the meeting?  Follow these steps:
1.Copy this address and paste into your web browser:
https://www119.livemeeting.com/cc/winserver_usergroup/join
2.Copy and paste the required information:
Meeting ID: W8GTKG
Entry Code: kNgb?]5

Agenda:
8:00PM-8:10PM: Introduction
8:10PM-8:45PM: Lee Holmes (Microsoft)
8:45PM-9:20PM: Karl Prosser (MVP)
9:20PM-9:55PM: Brandon Shell (MVP)
9.55PM-10:00PM: Closing

Speaker topics:

*Lee Holmes (Microsoft): "PowerShell: Pragmatic Programming, and Everyday Automation"
*Karl Prosser (MVP): "Embrace the Admin Development model with agility by using tools and workflow that produce efficent, consistant results"
*Brandon Shell (MVP): "Custom Objects in PowerShell"

Notes:
All details related to this event will be posted on http://www.powershellcommunity.org.

(NOTE 1: This is delivered via Live Meeting 2007.  It is possible that you may have to download and install the Live Meeting 2007 client which can take possibly up to 10 minutes typically.)

(NOTE 2: Our first meeting allowed people to call-in to listen the the audio.  This was very distracting, so this meeting will only offer audio streaming over the Internet.  Therefore, you will need access to speakers or a headset to listen to the audio.  This session will be recorded and available for offline viewing later.)

Sponsors/partners:
PowerGadgets