Showing posts with label Script. Show all posts
Showing posts with label Script. Show all posts

Tuesday, 13 April 2010

MVC DateTime Suffix HTMLHelper

Recently I have been working on an MVC Project, and tonight I got to the point where I wanted to output a date in a specific format, for example Tuesday 13th April 2010. Sadly DateTime formatting still doesn't allow you to specify output a suffix, you can do Tuesday 13 April 2010 but not what I wanted.


I decided that I could achieve what I wanted by writing a quick HTMLHelper, this would then allow me to specify a datetime format string with a magic / special character which I could then replace for the appropriate suffix.


Writing the helper was very quick and easy, if you want to learn about HTMLHelpers and how to write your own I recommend looking at Stephen Walther's Post on HTMLHelpers .

The code for my datetimehelper is below:


using System;
using System.Web.Mvc;

namespace mjjames.MVCHelpers
{
 public static class DateTimeExtensions
 {
  public static string DateTimeFormat(this HtmlHelper helper, string dateTimeFormat, DateTime dateTime){
            var dateTimeOutput = dateTime.ToString(dateTimeFormat);
            if (dateTimeFormat.Contains("~"))
            {
                dateTimeOutput = dateTimeOutput.Replace("~", GenerateDaySuffix(dateTime.Day));
            }
      return dateTimeOutput;
  }

        /// <summary>
        /// Generates a Day Suffix from the Day Number
        /// </summary>
        /// <param name="day">Day Number</param>
        /// <returns>Suffix String</returns>
     private static string GenerateDaySuffix(int day)
     {
         var suffix = "";
            //find out if the day matches a suffix which isn't th
         switch(day)
         {
             case 1:
                case 21:
                case 31:
                 suffix = "st";
                    break;
                case 2:
                case 22:
                 suffix = "nd";
                 break;
                case 3:
                case 23:
                 suffix = "rd";
                    break;
                default:
                 suffix = "th";
                 break;
         }
         return suffix;
     }
 }
}

Then to use it first include the namespace in your view:


<%@ Import Namespace="mjjames.MVCHelpers" %>

And then to use it call Html.DateTimeFormat passing the format string and the DateTime value. To use the day suffix include the ~ character. Note you can use it with.


<%= Html.DateTimeFormat("dddd d~ h", Model.StartDate) %>
<%= Html.DateTimeFormat("dddd d h", Model.EndDate) %>

There we go, nice and easy, if you want to use this feel free I hope it helps

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
Randomize
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 _
    objWMIService.InstancesOf("Win32_ComputerSystem")
 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
        Else
             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
 Else
  WScript.Echo "Rename Not Needed"
 End If

Next

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