Monday, 22 September 2008

Rename a Computer Using WMI Scripting

This Wednesday I'm doing an MVC Hack Night at my work with Derek Fowler. This evening will hopefully give us a chance to show other employees what ASP.NET MVC is and to give them some simple tutorials to work on.

As ASP.NET MVC is only preview 5 we didn't want to install the extensions onto the work machines, so we decided to create a time based XP Virtual Machine that everyone could copy and use for the evening. Not only did this mean we could install the extensions without fear of breaking our development machines but it also meant we could ensure that Sql Express and Visual Studio Express was setup and working with the tutorial code properly.

There was one giltch with the plan and that was that the Virtual Machines would all be using the same Computer Name, this would result in the computers not logging onto the network and all sort of errors could occur.

My solution to this was to write a dead simple WMI Script in VB that would run at logon, this script would check to see if the Computer Name was the default, in this case MVC, and if it was to rename it to MVC-{Random Number}. It would then reboot the computer and then find the name changed and hopefully find it can logon to the network fine.

The source code is below, I've chosen to generate a number between 1 & 1000. This should hopefully not generate any machines with the same name, as there's only 10 of us.
The script really is quite simple, I first get the Computer WMI object and check it's name to see if it equals MVC, if it isn't I change it, echo out its been changed and then use the WMI Script object to run Shutdown.exe /r (reboot the computer). Now I could use the Operating System WMI Object to issue a shutdown but I didnt think of this until after I had wrote the script.

lowerbound = 0 ' lowest value
upperbound = 1000 ' highest value
randomvalue = CInt(Int((upperbound - lowerbound + 1) * Rnd() + lowerbound)) 'generate random number

Name = "MVC-" & randomvalue ' our new computer name
Password = "abc123" 'my admin password
Username = "admin" 'my admin username

Set objWMIService = GetObject("Winmgmts:root\cimv2")

' Call always gets only one Win32_ComputerSystem object.
For Each objComputer in _
 If objComputer.Name = "MVC" Then ' check to see if comp is using default name  
         Return = objComputer.rename(Name,Password,Username) ' rename comp
         If Return <> 0 Then 'check for error
             WScript.Echo "Rename failed. Error = " & Err.Number
             WScript.Echo "Rename succeeded." & _
                 " Rebooting for new name to go into effect"
   set objWSHShell = WScript.CreateObject("WScript.Shell")
   objWSHShell.Run "shutdown.exe /r" ' reboot comp for changes to take effect

         End If
  WScript.Echo "Rename Not Needed"
 End If


If you want to use this script / adapt it for your needs feel free, it certainly made my life easier!

Tuesday, 16 September 2008

Welcome to Google Developer Day 2008

OK, I'm here, I have my shiny Google pass and a coffee I'm ready ;)

It all looks preety good so far, we strangly have a small wrapped up box which is for a session later.. I want to open it now but much like Christmas I'm thinking it may spoil it, apart from that upon arrival we are being showered in free coffee and pastries, if only I could eat the pastries I wouldn't be hungry, very good none the least.

Wembley Stadium is gorgeuos, it may have been over time and budget but it is beautiful, a nice venue to hold such an event in. I'll try to put some pictures online of the stadium later.

I have taken a few quick shots so far and will upload them to Flickr shortly, sadly I forgot my SLR and then Chris Alcock's small camera so alas it's LG "Viewty" quality only, I'll post later with more thoughts and ramblings about the day, given last night's performance though I'm thinking I need to scout out power sources soon!

Google's WiFi has fallen over already and there's only 50 people here, I've switched to my 3G connection, it may be more reliable, although it will suck my battery quicker! Doh!

Pictures so Far

Sunday, 14 September 2008

Google Developer Day 2008 London

Tuesday is the UK's Google Developer Day, this year its taking place at Wembley Stadium and I'm lucky enough to be attending

I have to say I'm quite looking forward to it, a chance to get see Google Speakers and to catch up with other developers that utilise the Google API's will be awesome.

So far I have used the Google Calendar API and started to write a .Net wrapper for the YouTube API, so learning about others will be good. The sessions I have pencilled in to attend so far are: State of AJAX: Dion Almaer, YouTube API: Build YOUR YouTube: Jean Laurent Wotton, Mashing up Google Data APIs: Ryan Boyd and probably but not fully sure V8 - the Chrome engine Kevin Millikin.

As you can see it's going to be a jam packed day, but the sessions look top and I look forward to sharing and learning.

Due to travelling I'm heading down Monday night, and coming back on Tuesday Evening. If you are in London on Monday night, and fancy catching a pre dev day pint or even food, drop me a DM on twitter (mjjames)

After the day I plan on doing a summary writeup on here and hopefully sharing some cool info

Tuesday, 2 September 2008

Going Chrome

So today Google Launched Google Chrome, and like any good geek I got on it straight away.

Installation was quick and painless, and it fired up blistering quick. It was then the magic started. Now I won't focus on how nice the interface is to use, the new home page, etc I want to concentrate on the features I have used tonight whilst using it and whilst coding.

HTML & CSS Features

The first thing I did whilst playing was to view some page source, this as you would expect is on the context menu, nothing overly special here, however I did like the nice syntax highlighting, +1 to Google. However it doens't do any sort of nice reformat document, this would have been a really nice feature but we can't have it all.

Whilst looking for View Source I discovered that Chrome has a DOM Inspector built in, I got to it by "inspecting an element". This is far more than the DOM Inspector that you can get for FireFox, it gives you a nice DOM view, style information (computed, inherited, etc), Metrics, which shows the Box Model being applied to the element, and finally a properties tab giving you anything being applied to an element.

Next there's a funky search box, this allows you to search the DOM for anything, how many times have you needed to find the image with a class and you aren't sure where it renders? Now you could use inspect element, but if you are already in the inspector simply type "img" into the search box to find it lists all of the image tags within the document, now if it was really ace you could type image + class="photo" but this is not the case yet...

Resources and Debugging

All of that was only under the Elements section, there's another section... Resources This is similar to the net view tab in FireBug, it shows all of the "resources" requested by the page and shows them on a nice graph, showing how long they took to load and where in the process or you can compare the file sizes.
By clicking on the file the graph turns into a preview of the file, be it the image, JavaScript file etc.

I was slightly disapointed when I opened a JavaScript file to preview, I was expecting to be able to set breakpoints and watch's but alas no :( However there is a JavaScript Console, this is located at the bottom of the screen, the 2nd button from the left, this pops up the console.

Now this is really good, it has intelisense ;) So if you start typing document.getElementByID for example all you need to do is: do [tab].get[tab] and choose by ID. If you cant remember the name, for example getElementsByName you can just keep pressing [tab]

Now at the moment it only seems to do DOM but I'm hoping it may eventually pick up references and then you can do funky jQuery or something within the console...

Now there is soo much more I could talk about, however the final feature I used tonight was the JavaScript debugger, this is under Developer > Debug Javascript or you can use the keyboard shortcut.

Now I expected more than this, but maybe It does do more than I currently know, if you type help within the console you can see what you can do. You can attach breakpoints using the console, print variables, see what scripts are loaded, etc.

When attached to a tab the debugger will catch any errors and you can print out any variables that you need to inspect before typing continue to continue.

For me I still prefer firebug for JS debugging but I'm sure this will evolve further over time.

So that's it for tonight, I am loving Chrome, it's fast, it's slick but... there are still some bugs and glitches to iron out. For example I have seen quite a few pages where font-size is being inherited wrong within li's, so if I had 90% font size by the 5th li the text was unreadable :( There have been some other weird text issues but nothing that major.
I'm not going to make Chrome my default browser yet... but I'm not far off ;)