PowerShell Re-Introduction: Part Two

Last time we met our new robot friend, the PowerShell Command Line Interface. We learned some cool tricks. We tried to say “Hello” and got a response and started to learn how to talk PowerShellian. We learned that PowerShell really only listens to four things and we called the “Suitable Commands.” They are:

  • CMDLETs
  • Functions
  • Scripts
  • Operable Programs

We looked at that last item, operable programs and we saw how we can run Notepad from our command line.

Now, let’s look at the first, CMDLETs. CMDLETs are the magic of PowerShell. If we’re going to learn anything about CMDLETs we’re going to have to GET some HELP.

That, friends and co-workers, is what Deputy Chief Brenda Lee Johnson (a.k.a. The Closer on TNT) calls a “Clue.”

Get-Help, get-help, Get-Help, gEt-helP, any of those are all the same thing to PowerShell so let’s try it.

Get-Help

The Get-Help CMDLET

Remember this. This is our first cdmlet. Note again that case does not matter. Note also the Verb-Noun format. Every cmdlet employs this format. The verbs are usually something like “get” or “set” while the nouns are any, and I mean any and every, Windows object. Don’t worrry about what this means yet but just remember that when you get the object, you get all of its properties and their values. You also get all of their methods; don’t worry about this yet either.

 Here’s what we “get:”

 
PowerShell Get-Help

The Get-Help CMDLET Output

I gotta tell you friends, for this robot, this really is the keys to the car; here’s where you can find all you need to know to become the greatest Windows robot ever.

Let’s look at some of these details.

First we get a short description.  Very nice.  Then a long description with syntax and remarks. Hmmm…

Let’s just look at the very first line: get-help <CmdletName>

We we know exactly one cmdlet, Get-Help. Let try it…

Get-Help Get-Help

Help for Get-Help CMDLET

Awesome. Get-Help blah blah blah… But, wait, that’s not all. Look at the Remarks at the bottom. Here, you can get examples, more detailed information or even more technical detailed information. For example, Get-Help Get-Help -full. Here we get a full ten pages of things we can do with Get-Help including:

  • Synopsis
  • Syntax
  • Lengthy Description
  • Parameters
  • Inputs (Not allowed on Get-Help)
  • Outputs
  • Notes
  • Examples (15 in all)
  • Related Links – including a go.microsoft.com link to online help.

At the very end here, while we’re still overwhelmed with all that PowerShell does to make Get-Help work for us, they drop a second jewel on us, Get-Command. Let’s try it.Entering the Get-Command CMDLETPress Enter… Did you see that? Below we can see the last of the output that scrolled by when we ran the Get-Command cmdlet.

Output from Get-Command

The Last Screen of Output from Get-Command

And there you have it, every command that PowerShell can run. Along with the cmdlets, we also get functions and aliases and we’ll look at those later. For example, we can start at the top, we’ve got an Alias % and an Alias ? and a function A: and then another Alias, ac. But then we get the first cmdlet in our list Add-Computer. Let’s Get-Help it:

Get-Help Add-Computer

Running Get-Help Add-ComputerAnd we get: Output from Get-Help Add-Computer

That’s great. We see we can roll into PowerShell and run the Add-Computer cmdlet to add our computer to a domain. We see that we add in parameters for the domain name and the credentials and some other stuff necessary to join a domain. Great.

Help for Add-Computer

Output from Get-Help Add-Computer

All we have to do is learn everything about each of these cmdlets and we know all there is to know about PowerShell, right?

Well, no so fast.

First, by default, we only get the Windows library for the version of Windows we’re running, in my case Win 7. That doesn’t mean there’s not more. Microsoft pretty much publishes a library for every product it sells. And here’s the kicker, the “theory” is that ANYTHING you can do using a Microsoft product’s GUI, you can do in PowerShell; it is intended to be the command line interface for all things Microsoft. We just need to learn how to load those libraries.

Second, we’ve got those other “Suitable Commands” such as functions and scripts. We covered operable programs and we started on cmdlets so, sure, functions and scripts will be coming up next.

Finally, and this is were we might leave you. We’ve mentioned this before and we’re almost done for the day so you’ll have some time to think about what this means. PowerShell is object oriented. What this means is that when you run Get-Something, you actually have an instance of that something in memory with all of it’s properties and methods. Unlike DOS, when you run a command, you got a text string that you had to work with to figure out what it was telling you, in PowerShell, you get the real object. Set a variable to a Get-Something and that variable has all the properties and can run all the methods of every other Something ever created. When we do this, we will chain cmdlets together, called “piping” because you use the pipe character, “|,” to create, read, update and delete any artifact you’ve ever seen in any Microsoft product.

Let’s review.

Get-Help – Why would you start anywhere else?

Get-Command – Displays all the suitable commands you’re currently able to use.

Cmdlets, Functions, Scripts, Operable Programs – All suitable commands.

Libraries – Bundles of cmdlets that support various Microsoft technologies.

Object Oriented – Uses real objects in memory.

Piping – Chain one command to another to build extremely sophisticated solutions.

We’ll be getting more done soon enough.

-robot

More PowerShell: A Re-Introduction If You’re Starting from Scratch

Any robot knows that robots are for doing things that people don’t want to do. For example, you would never want a robot to go to the rock show for you because you’d want to go yourself. On the otherhand, all this stuff you do everyday that drives you nuts are perfect for a robot and your robot’s favorite tool is probably PowerShell.

The latest versions of Windows gives you PowerShell by default. There’s some goofieness involved if you’re struggling to find it. Our new best friend, Liam, covers that in part here. But let’s just say you can go Start | Programs | Accessories | Windows PowerShell.

Here’s you should find two options: Windows PowerShell and Windows PowerShell ISE.

The latter option here is an integrated script editor. That will become a favorite but first, we need to understand the former option.

Click on Windows PowerShell and you’ll get a blue box.

PowerShell CLI

The Initial PowerShell Command Line Interface

 You can do some pretty cool things here including access the entirety of the PowerShell libraries that are installed. So, you may ask, what is a PowerShell library? Where did I get one or more of those? Am I missing any? I’m so confused.

Well, remember, robot are just like people and the first thing they want to do when they meet an attractive stranger is say “Hello.” If that breaks something, then our relationship was just not to be. We type Hello and hit enter.

Typical Error Message

The Error Message Returned by Unsuitable Command

Well, we are talking to a stranger and seems like he could be a little nicer but at least he is verbose and all in red, presumably to get our attention. Maybe he just didn’t understand. but, instead of going to all the trouble to type “Hello” again, we can just push our up arrow. That will reload our previous command. we hit enter and, no surprise, we get the same  response. And when we do that ten or twelve times, we get get a screen full of repetitive garbage and see our scroll bar moving down the right side of our window.

Here, I’ll give you one for free. Try cls and hit Enter.

Cleared Screen

The Cleared Screen After Running CLS

Ahhh. Sweet clarity.

Note here, now if you tap your up arrow, you get cls again and if you tap it again, you get Hello. This is very clever. In fact, no matter how many commands you’ve entered, tapping up will always give you the previous command. And, as you move back into your command history, you can tap the down arrow of back out of your history.

And just to set your mind at ease here and now and for all time, your commands are NOT case sensitive. Sure HELLO and Hello and hello all generate the same response but you’ll also see that CLS and cls and cLs all clear your screen.

So, let’s tap around until we can run that Hello command one more time and take a closer look. First, it says:

The term ‘hello’ is not recognized as the name of a cmdlet, function, script file, or operable program.

 Okay…

We need to get smarter about what these things are. I’m going to call them “suitable commands.” Obviously, a suitable command is a cmdlet, function, script file or operable program. Let’s start with the last item, an operable program. We know what these are and we have some. For example, we have a program called notepad.exe that lives in c:\windows\system32. So let’s type notepad.exe and hit enter.

Sure enough, Notepad opens.

Also note that while PowerShell opens in c:\windows\system32 and notepad.exe lives in the same folder, you can run a cd.. command twice and your PowerShell will be reading the c:\ root folder. You can run Notepad.exe here and still, Notepad will open. This implies that your PowerShell prompt is able to find the program in this different, remote folder, even if that folder lives on a different path.

Okay, one more robot trick. After you’ve cd.. twice, you can see that you’re in the root C:\ folder. Here, you can type cd pro

A Partially Entered Command

A Partially Entered Command

… and tap your Tab key and our new robot friend will fill in the friggin’ blanks suggesting we’re really trying to type cd program files but just got all lazy human-like.

Type Ahead Code Completion

Type Ahead Code Completion

And it gets even better. Say you’re crazy lazy, here you can just type, n and tap you tab key and our robot friend will give you the first suitable command that starts with the letter “n” …

Type Ahead Code Completion

The First Suitable Command Consistant with Partially Entered Command

In this case, we see, that’s a folder called .\NETGEAR. But we can can cotinue to tap the Tab key and our robot friend will cycle through all the suitable commands in the current folder and all the known paths and offer each of the suitable commands that begin with the letter “n” alphabetically. We can Tab  and Tab until we get to notepad.exe and hit enter and run Notepad.

So lets review:

Up Arrow – Loads previous command consecutively.

CLS – Clears the screen.

CMDLETs are NOT case sensitive.

Command line can open executables.

Command line can read from remote paths and folders.

Tab will try to complete any partially entered command.

That’s a lot for one robot in one day so let’s just plan on looking at those other suitable commands,  next time.

-robot

Debugging the MVC Application

So your MVC apps do really start the same way and our new best friend, Stephen, describes how to set the startup page here.

And then there’s the tutorial from Microsoft’s ASP.NET site here.

SharePoint as a Service Provider

Our objective is to get SharePoint content on an iPad. Our costraints are that we have to use an ASP.NET MVC3 website that runs on the same web servers but outside of SharePoint. This site will use the SharePoint site’s web services to gather content metadata and links to published SharePoint content.

Our tools are:

  • SharePoint 2007
  • Visual Studio 2010
  • MVC3
  • HTML5 

We’ve got a functional SharePoint publishing portal website that’s internet facing to that’s our “requirements” but we need to optimize for the iPad and we’ll use a Safari browser as a proxy for that.

And just like your TV chefs, someone’s already built the whole solution. We copy that to our project’s folder to open it in Visual Studio so we can see it run and, as ususal, barf:

The project type is not supported by this installation.

So, what we have here is something missing. We check with our new best friends at StackOverflow.com where, here, our new best friend MindStalker tells us we need to open our .csproj file in notepad and identify the project type guids.

Sure enough, in the .csproj file, we see:

<ProjectTypeGuids>
{E53F8FEA-EAE0-44A6-8774-FFD645390401};
{349c5851-65df-11da-9384-00065b846f21};
{fae04ec0-301f-11d3-bf4b-00c04f79efbc}
</ProjectTypeGuids>

We google the GUIDs and we see they all realte to MVC. Now, anyone that’s been following along know we know squat about MVC but Microsoft provides a bit of info here including an option to download the MVC3 Installer.

Let’s give that a go… Click, Accept, Install…

This is a pretty clunky install. It gave me one green bar real quick but then took several minutes to start the second. It stopped about half-way for a minute or two than again at about 70%. The pinwheel would spin very slowly. But then it completes and wants to reboot. What’s arobot to do?

After the reboot, we can open the project no problem. To get our project to open in Safari, we need to work a little trickery.

Again, our friends at StackOverflow.com help out here. First we have to find a browsable file such as a .aspx or .html file. Since there’s none of these in my project, I tried an .xml file. Right click on it and select  Open With and Visual Studio will open a dialog box that will allow you to select a browser from your installed options which, since I just installed Safari, include Safari. Then, best of all, it gives us an option to set our selection as our default.

So now we can build our solution and debug right in Safari. What could be easier?

-robot

New Things at SharePoint from Scratch

I hate when bloggers talk about “haven’t been blogging” and “I’ve got to put more time in this” and on and on…

So briefly, I blog so I can remember how to do stuff. Everything I’ve blogged is there so I can go back and use it the next time I need to do something slimilar. If anyone else finds any of it interesting, great.

Since I last blogged, I left the sorriest company I’ve ever worked for and found a new opportunity that has been truly captivatating. The reason for the lack of blog posts is that I’ve been so busy doing stuff I know how to do but none of it was new so I didn’t need to create any blog entries to track any notes; nothing new to help us get going the way we like it, from scratch.

Things change.

I’ve got something new to work on and some blog posts will be coming, probably today.

But, in addition, we’ve heard the first alarms about SharePoint 2013 and Office 15.  With that in mind, your favorite robot has captured the 15Hive.com domain name and pointed it here. we’ve also got the @15Hive Twitter name and we’ve followed some of the SharePoint community’s brightest Twitterers. Finally, and this is cool, we’ve set up an aggregator at paper.li so if you’re stoked about SharePoint 2013 and you want to be spoonfed, subscribe here.

So, enough navel gazing, let’s build something from scratch.

-robot

Unable to Authenticate to Local Site? Disable Loopback Check

This one never gets old. Everytime we want to install SharePoint, we tend to continue to work on the server getting as much done as possible. And, everytime, we’re confronted with the inability to authenticate using Windows username and password and a browser on the web front end server.

You try so carefully to enter your DomainName\UserName and password exactly correctly and hit enter, enter, enter and you get a 401 or, worse, just a blank screen. As we’ve learned, what we’ve encountered is the dreaded anti-loopback monster.

Now, the ALM is a fine security measure on a server that you deploy and then spend the rest of your life as a SharePoint admin working from another workstation. It simply prevents authentication from the local machine using host headers other than the machine name. It’s detailed here on support.microsoft.com. Apparetly the threat is a “Reflection Attack” which reminds me of my mirror on the occasional Sunday morning before my shower.

Essentially, we’re creating a two registry keys:

  1. DisableLoopbackCheck dword registry key with a decimal value of 1 in the HKLM\SYSTEM\CurrrentControlSet\Control\Lsa hive. 
  2. DisableStrictNameChecking dword registry key with a decimal value of 1 in the HKLM\SYSTEM\CurrrentControlSet\Services\
    LanMan
    Server\Parameters hive.

Once we restart, we should be able to access our sites locally using the propery credentials. Of course, this is a “must have” kind of thing if you’re building a development box where you’ll be accessing the WFE from the local machine in Visual Studio.

hth
-robot

SharePoint 2010 Chart Web Parts

So we’ve had a couple of cycles through the 2010 Chart Web Part. It’s a pretty cool thing that your higher level stakeholders will probably love and they’re pretty easy to deploy.

We have a couple of great resources from our new best friends, Corey, Channel 9 and PointBridge.

hth
-robot

Looking at the WCF Demo

The heart of Service Oriented Architecture is the interaction between services. These used to all be “web” services that ran on web servers and traveled via http through ports 80 and 443. Recently, they’ve become more generic using tcp through any port and, so, they’re not web services any more, they’re just services.

Microsoft supports the interaction between services using the Windows Communication Framework (WCF) that connects services with clients and hosts. The demonstration of these components is pretty simple.

If we need information or we need someone to manipulate some information we already have, we’ll meet that need by identifying a provider. We’ll access the provider’s service. We’ll avail ourselves of the service’s solution by accessing it’s  endpoint. The endpoint defines three things:

  • The service location.
  • The binding – The information needed to interact with the service.
  • The contract – The definition of the functionality the service provides.

 The demos include:  

  1. Define a service and its contract.
  2. The service implementation.
  3. Configuring, hosting and running the service.
  4. Create a client proxy.
  5. Configure the client application.
  6. Create and run the client.

1. Define the Service, the Contract and Its Interface

Open Visual Studio 2010. Select File | New | Project.

Select a new console application. Give it a name and a location.

Right click on the Service project in Solution Explorer and select Properties. on the Application tab of the properties dialog change the default namespace to:

Microsoft.ServiceModel.Samples.

On the Program.cs page, make the same change to the namespace.

Rightclick on the project again and select Add Reference. On the .NET tab, select System.ServiceModel and click OK.

Add a using statement for System.ServiceModel in Program.cs.

Within the namespace, but after the Program class, add the service contract on our new namespace and its interface, ICalculator

[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
  [OperationContract]
  double Add(double n1, double n2);
  [OperationContract]
  double Subtract(double n1, double n2);
  [OperationContract]
  double Multiply(double n1, double n2);
  [OperationContract]
  double Divide(double n1, double n2);
}

2. Create the Contract

After the ICalculator interface code, add a new class that implements the interface:

public class CalculatorService : ICalculator
{
public double Add(double n1, double n2)
   {
       double result = n1 + n2;
       Console.WriteLine("Received Add({0},{1})", n1, n2);
       Console.WriteLine("Return: {0}", result);
       return result;
   }
   public double Subtract(double n1, double n2)
   {
       double result = n1 - n2;
       Console.WriteLine("Received Subtract({0},{1})", n1, n2);
       Console.WriteLine("Return: {0}", result);
       return result;
   }
   public double Multiply(double n1, double n2)
  {
       double result = n1 * n2;
       Console.WriteLine("Received Multiply({0},{1})", n1, n2);
       Console.WriteLine("Return: {0}", result);
       return result;
  }
   public double Divide(double n1, double n2)
   {
       double result = n1 / n2;
       Console.WriteLine("Received Divide({0},{1})", n1, n2);
       Console.WriteLine("Return: {0}", result);
       return result;
   }
} 

3. Configuring, hosting and running the service.

Within the Main() method of the Program class, set the service’s base address:

Uri baseAddress =
   new Uri(http://localhost:8000/ServiceModelSamples/Service);

 Then provide the hosting details, add the using statement:

using System.ServiceModel.Description;

Create the ServiceHost:

ServiceHost selfHost =
   new ServiceHost(typeof(CalculatorService), baseAddress);

Then, add a try\catch block to add the endpoint:

try
{
   selfHost.AddServiceEndpoint(
       typeof(ICalculator),
       new WSHttpBinding(),
       "CalculatorService");

Enable the metadata exchange:

ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);

Create the open and close methods:

selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
selfHost.Close(); 

 Close the try block and add the catch block to itself in the event of an error:

}
catch (CommunicationException ce)
{
    Console.WriteLine("An exception occurred: {0}", ce.Message);
    selfHost.Abort();
}

 At this point, you can run the service, either in Visual Studio or by clicking on the .exe in the project folder. Apparently there’s some goofiness with Windows Vista that requires you to do these as administrator but I’m not sure if a user in the local admin group will suffice. You’ll see the little black box pop up and then it will take an Enter keystroke to kill it.

You can also browse to the noted Uri and get a cool little “You have created a service.” page.

4. Create a client proxy.

 

 

References:

MSDN: Getting Started Tutorial

MSDN: Host a WCF Service in IIS

MSDN: Configuring Services Using Configuration Files

MSDN: ServiceModel Metadata Utility Tool (SvcUtil.exe)



Welcome, Again, To SharePoint from Scratch

It’s a new day…

SharePoint from Scratch has moved to a commercially hosted provider. We may cover those details in a post soon. Otherwise, your best robot friend is making a move away from the huge grindstone of the full-time corporate world so he can spend more time with his readers, you, and do what he was born to do, make it clear how to get things done when you’re starting from scratch.

So there’s a million things going on accordingly. But as my best reader friends, you probably care little about all of that. You’re here because you just want to know: “How do I make this doggone thing work?”

Well, the way this robot works is by making a list of doggone things:

  1. SharePoint Server 2010
  2. All the Stuff the SharePoint 2010 does.
  3. All the other complicated technical stuff  that the boss wants.

Now, this robot can teach you or he can tell you; that means you can “do” or you can “learn.” It’s a fish-fish thing. For those of you that just want fish, we’ll get to that. For those of you that want to fish, we have a brand new option: The Understanding SharePoint Journal (USPJ) and the USPJ Academy (USPJA) If SharePoint has you scratching your head, consider the Acadeny your marigold shampoo.

Now the guys and gals that run that show include several highly respected members of the SharePoint community and a newbie, your’s truly.

So we’ve just completed our first Beginning SharePoint Adminstration class and I must say, this robot is impressed with both the academy and it’s leadership as well as its student body. If you want to get involved with a group of folks that know how to collaborate and conquer, I’ve got a great group I’d like to introduce you to.

More about the Academy, its mission and its leadership will undoubtedly follow. But for now, let’s just say, it’s good to be refocused, to reduce the grindstone nose buildup and to ramp up the new SharePoint from Scratch. Someone said it’s all about “Winning.” Some of us weren’t born in the lead but learned what it takes to start gaining ground. Let’s get to it.

-robot

SharePoint 2010 Site Templates vs. Features

In SharePoint v4, site templates are considered more generically as solutions; when you save your site as a template, you end up wth a .wsp solution file, not a .stp file like in v3.

So, in our site collection, we go into site settings and select the Solutions gallery.

We can use the ribbon to select Upload Solution and get a dialog box to upload one or more solutions. After uploading, you can pull down the list item menu and select Activate. 

Once activated, we can go back to View All Site Content (/_layouts/viewlsts.aspx) and click Create. In the Create dialog box, we should see our new template listed in the sites group, perhaps filtering for Custom.

We add the site name and URL and click Create. Done.

But, this being SharePoint, like any fake religion such as the Flying Spaghetti Monster, it can be more complicated at times.

As we have seen in the past, the environment from which your site template is created casts a set of requirements on your target environment in the form of host features. For example, on one particular case, we’ve created a template from a site created on a SharePoint Server 2010 host that had been migrated from MOSS 2007.

The MOSS 2007 host, of course, included a site directory and the Site Directory site had been migrated into 2010 and the site from which the template had been created was a 2010 blank site under the Site Directory site. Got it?

Well, our host site was never a MOSS 2007 host so it doesn’t have a site directory. Consquently, when we try to create our new site on this 2010 host, we get:

Error
The site template requires that the Feature
{a392da98-270b-4e85-9769-04c0fde267aa} be activated in
the site collection.
Correlation ID: {9b9ab22e-1774-42f0-b0ca-1d3d795adaf1}
Date and Time: 12/22/2010 11:44:12 AM

So we can search for two things. First,

First, we’re still struggling with this whole Correlation ID thing. Lucky enough, our new best friend, Dina, provides an explanation here.

Then, we find a reference to the Feature ID here. It’s not great but and it’s referring to MOSS 2007 but it suggests we activate the Publishing Infrastructure feature at the site collection level and the Publishing feature at the Site level. Done.a

-Now, attempting to create a site using our template gives us:

Error
The site template requires that the Feature
{e978b1a6-8de7-49d0-8600-09a250354e14} be activated in
the site collection.
Correlation ID: {3aa51c89-319b-4d27-a64e-aa5d6833360e}
Date and Time: 12/22/2010 11:44:12 AM

This is cool because we touched it and made it do something different. When we look for this new Feature ID, we find this from 0ur new best friend, Steve. At the very bottom, he tells us our Feature ID refers to the LocalSiteDirectorySettings feature and, like we said, there’s no Site Directory in the 2010 host.

So what we have here is a feature that’s required by my site template but is deprecated in 2010; it’s only an issue because it was created from a site under a site directory that would never have existed except that it was migrated from 2007.

So, I spend the day looking and find this from MSDN. First it tells me to go look in the \14\Templates\Features folder where, believe it or not, contains all our features listed as folders in English readable text. I open the one called \LocalSiteDirectorySettingsLink and edit the Feature.xml file and see that is indeed the GUID of the feature I need.

Then, the MSDN article suggests we run some PowerShell to install and or activate the feature using the <Command> <FolderName> <URL> format.

I try but find that while the <foldername> parameter doesn’t work, the GUID does. I get the feature activated in my site collection and try to build the site from my template again. This time I get:

Error
The site template requires that the Feature
{fdb3e1d1-a200-46b3-ac54-11f69c0baac4} be activated in
the site collection.
Correlation ID: {adf2a768-2a9d-4fe5-b006-898d9ea0f859}
Date and Time: 12/22/2010 11:44:12 AM

References: