Blog

The official blog of managed hosting provider OrcsWeb.

The Power of “$()” in PowerShell

Sometimes, and sometimes all the time, I like to cram as much as I can into a single line of PowerShell code. It’s like a challenge to me, making a command as long as absolutely possible. I haven’t quite broken the 1000 character mark, but I have made it to the 600+ character milestone. I’m sure professional programmers are cringing and mumbling something about spaghetti right now, but I think it’s fun. 

The best tool, besides pipes, for doing this in PowerShell is $().  What is $() you may ask? Officially…I have no idea. Unofficially it means “return the value of whatever’s inside.”  It’s like a spontaneous function or script block.  It is a very handy tool that all PowerShell’ers should know. 

Let me give you an example.  Say you want to write the output of, oh, I don’t know, the product key of your TechNet licenses using my handy keys.xml to keys.txt converter script (http://www.orcsweb.com/blog/james/using-powershell.../), but you want to add a conditional statement if there is a claim date attached to the key.  You have some options.

Tagged as:

.Net 4's new IsNullOrWhiteSpace method

.Net 4 (still in beta right now) has a handy new method that can analye a string and return where it is null or just whitespace versus other text. Here's a quick sample:

ScratchPage.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ScratchPage.aspx.cs" Inherits="ScratchPage" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="textBox" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

ScratchPage.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class ScratchPage : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrWhiteSpace(textBox.Text))
        {
            Label1.Text = "The text box is null or has only white space.";
        }
        else
        {
            Label1.Text = "There is a non-null, non-white-space value in the text box";
        }
 
    }
}

 

Tagged as:

The CodePlex Foundation Accepts MVC Contrib project into ASP.Net Open Source Gallery

The subject of this post speaks for itself. I'm not sure how long the "latest news" will sit on the homepage of this site, but it's there now. Let me know if it moves and I'll adjust the link. http://www.codeplex.org/

Here is more information about the specific MVC project: http://www.codeplex.com/MVCContrib

 

Tagged as:

ASP.Net Forums

If you aren't already familiar with the ASP.Net forums, you should browse over and check them out. It's a great place to get answers to frequently asked questions, post your own custom questions for solution assistance, answer questions for people to provide peer support, and find information about new and upcoming solutions (like the ASP.Net 4 forum).

Some of the general topic groupings include General ASP.Net, AJAX, Visual Studio, Data Access, Advanced ASP.Net, Migration, and a number of Starter Kits and Source Project discussions.

Tagged as:

CSS Sprites: What they are and why they’re cool

I just heard about CSS Sprites today for the first time. It's an interesting concept and has the potential to improve page load performance. Here is a link with some more details about this feature if interested:

http://css-tricks.com/css-sprites/

 

Tagged as:

How to enable .Net 3.5.1 on Windows Server 2008

When Windows Server 2008 is installed and the IIS roles are enabled, it defaults to .Net 2.0.

If you want to run .Net 3.5.1 on the server you might think the process is as easy as just downloading the bits from Microsoft and running the installation process. No, it isn't. It still isn't hard, but it can be frustrating for someone who isn't familiar with the required process.

To enable .Net 3.5.1 on a Windows Server 2008 machine that already has the IIS role enabled...

To get started, from within Server Manager you need to select Features in the left-hand tree and then Add Features in the right-hand pane.

NOTE: I have the SMTP feature enabled on this test server but on your machine you likely won't see that listed.

On the next screen select the ".Net Framework 3.5.1 Features" option from the list.

As soon as you click that box, a pop-up window like the one below will be displayed.

Clicking "Add Required Features" will then install the necessary dependencies and files required to support .Net 3.5.1 on the server.

 

Tagged as:

Using PowerShell to convert ugly TechNet/MSDN keys.xml to pretty, easy to read keys.txt

I love long titles. I wish more stuff had long titles.

Here’s the story. I have a TechNet account and I love pressing that “Get” link to generate as many keys as possible for every interesting product on the list.  I’ll never use them all, but that has never stopped me from pressing the “Get” link.

The problem with being a “Get” link user is copying down all those keys.  I thought up a myriad of ways to copy them all to an easy to read and search text document but too many of them involved a lot of manual work.  Then I noticed, hidden on the top right of the Product Keys table, the “Export Key List to Xml” link, which became my new best friend.

image

Until I looked at the garbled, spaghetti coded XML nonsense that was generated by the “Export Key List to Xml” link.  Spaghetti code…yuck.  They didn’t even include meatballs.

Time to bust out the PowerShell!

Tagged as:

Getting Started with WPF : Hello World in multiple flavors

Here's a great post from Pete Brown about getting started with WPF:

http://community.irritatedvowel.com/blogs/pete_browns_blog/archive/2010/02/09/Getting-Started-with-WPF-_3A00_-Hello-World-in-multiple-flavors.aspx

(Side note about that blog: Wow, Peek and Poke! That's a blast from the past! I wonder how many people notice that theme on Pete's blog skin and actually remember those days. :>)

Tagged as:

Securing ASP.Net Pages - Forms Authentication - C# and .Net 4

As part of our managed hosting solutions we sometimes assist clients with code samples and/our troubleshooting. A fairly popular request is for sample code showing a basic implementation of ASP.NET's Forms Authentication.

I recently discovered that when we converted to Graffiti it messed up the code formatting. After working on it for quite a long time, I decide to just move it off-site to my personal blog where I have better formatting control. So, here it is:

http://bradkingsley.com/securing-asp-net-pages-forms-authentication-c-and-net-4/

To find out more about our solutions, check out the links below our contact us at sales@orcsweb.com

Managed Windows Dedicated Server Hosting
Managed Windows Cloud Server Hosting
Managed Shared Windows Hosting

Design Guidelines for Developing Class Libraries

Someone pointed out my old-school VB naming conventions in a recent C# sample code post and provided some pointers and some suggested reading. I've named objects the same way for so long that I'm sure I'll struggle with the changes, but I took the pointers to heart and will work to adjust my naming conventions moving forward. I haven't read through the link provided yet, but figured I'd share it here (its from MSDN).

He also mentioned that the comments button isn't currently working, which explains why comments have totally dropped off - someone here is looking into that issue to see why it broke and will hopefully have it working again shortly.

Update: Here is another link I was just reading about Pascal Case and Camel Case use in C# programming:
http://cplus.about.com/od/learnc/ss/csharpclasses_5.htm

 

Tagged as: