PowerShell: Get More Information On Your CmdLets

With the PowerShell Window, you can run Help or Get-Help to go looking for information of syntax and flags.  If you want, you can even use the get-member cmdlet, such as:

Get-NetAdapter | Get-Member

There’s two things that are happening with the above command.  Get-NetAdapter is being run, and then we’re using a pipe to send the output as an input to Get-Member.  Get-Member will list all of the attributes of the Get-NetAdapter output, including methods and properties.  You might want to filter that down, and you can do that by running:

Get-NetAdapter | Get-Member –MemberType Property

Where did I get –MemberType from?  Look at the titles of the columns from the previous command.  One of them was called MemberType.

I could have just as easily used:

Get-NetAdapter | Get-Member –Name *Interface*

That would have limited the results to attributes of Get-NetAdapter that contained “Interface” in their name.

Technorati Tags: ,

Hyper-V & PowerShell – Getting The Host On The Network

Note: I’d normally configure TCP-IP at this point but the cmdlets that I seem to need to use either are not working correctly or are very confusing – that’s pre-beta software for you!  I’ll try to return to that topic at a later point if I find a solution.

When you install Windows without some clever imaging solution, the machine comes up with some random (not completely) computer name.  You can PowerShell this if you want:

Rename-Computer MyNewComputerName –restart

In a previous blog post I mentioned that POSH (PowerShell) is great for scripting.  You can put a whole load of these commands in a single PS1 file and run it.  This cmdlet is an example of where you’d need user input.  In that case, the following embeds the prompt for a new computer name inside of the Rename-Computer cmdlet.  When you run this, the embedded Read-Host cmdlet is run and provides the user input to the Rename-Computer cmdlet.

Rename-Computer (Read-Host “Enter a new computer name”) –restart

Once that reboot is done, you can join a domain.  The following cmdlet does just that.  It will fire up a traditional Windows prompt for a user name and password to do the domain join.

Add-Computer –DomainName “demo.internal” –Restart

Want some help and examples beyond what you can get from Get-Help?  Try this:

Get-Help Add-Computer –Online

That should fire up your web browser and bring you to an official MSFT web page for the cmdlet in question.  A nice example you’ll find for Add-Computer is that you can use the –OUPath flag to specify a location to place the new computer object in your domain.  I tend to have a special OU for Hyper-V hosts so that’s where I’d place the computer object.  So instead of using the default computer object creation location (and probably forgetting to move the object to the correct one before the reboot), I can save some human effort by running:

Add-Computer –DomainName “demo.internal” –OUPath “OU=Hyper-V, OU=Servers, OU=Company, DC=demo, DC=com” –Restart

And that’s your host named and in the domain, all ready to manage with SCVMM.

Hyper-V & PowerShell – Enable Hyper-V

Windows 8 includes native PowerShell cmdlets for Hyper-V for the first time ever.  You can use this to manage Hyper-V from the command prompt and to automate complex and/or repetitive tasks.  In fact, Hyper-V appears to be going down the Exchange route where only so much is revealed in the GUI, and everything is in PowerShell.  I’d expect System Center Virtual Machine Manager to provide that additional GUI functionality (and use PowerShell under the hood).

But before you even get to doing Hyper-V cmdlets, you’ll need to enable the Hyper-V role.  One could use Server Manager, but we’ll use the Server Manager cmdlets instead.  You can learn lots more about this cmdlets in the chapter that I wrote on Server Manager in Mastering Windows Server 2008 R2.

PowerShell cmdlets are a subset of modules.  To use a cmdlet you have to load the relevant module.  Which one?  You can run a cmdlet to find that out:

Get-Module –ListAvailable

You’ll notice that PowerShell works in the format of Verb–Noun (action-thing to manage) with optional flags.  In this case it’s retrieving the list of all available modules that can be imported.

Then I can run the following to import the Server Manager module to get access to the relevant cmdlets:

Import-Module ServerManager

Try this: type in half of ServerManager and press <TAB>.  This should autocomplete the text for you.

If I now run the following cmdlet then I get a list of all imported modules:

Get-Module

Note: that if I knew what I was doing, I would have just run the Import-Module cmdlet to import ServerManager.

What cmdlets are not available to me?  There’s a cmdlet for that.  I can run Get-Command but that lists every available cmdlet.  I want to see the ones in the ServerManager module:

Get-Command –Module ServerManager

It turns out that I have:

  • Get-WindowsFeature: list what features and roles are available, and what their install status is (indicated by an X).
  • Install-WindowsFeature: install a feature or role
  • Uninstall-WindowsFeature: install a feature or role

Need some help? That’s built in too:

Help Get-WindowsFeature

We want to know what the state of the machine is so we can run

Get-WindowsFeature

This spits out a list of every role, role service and feature that you can install or uninstall via Server Manager.    You should note the name(s) of the ones you want to work with because you you’ll need them for the next step.

When you check the help of Install-WindowsFeature you’ll see some interesting flags:

  • -Whatif: It doesn’t run the cmdlet, but it simulates it.  It will tell you (to the best of its ability) if the command will work or not.
  • -Restart: forces and automatic reboot to happen, rather than prompting for it.  I know that enabling Hyper-V requires a reboot so this is useful.

For example this will simulate the install of the Hyper-V role:

Install-WindowsFeature Hyper-V –Restart –Whatif

If I’m happy, and I know a reboot of the new host is OK then I can run:

Install-WindowsFeature Hyper-V –Restart

A progress bar will appear in the PowerShell window, and the new host will reboot so the Hyper-V role can be enabled and started up.

That sounds like we’d done a lot of cmdlet-ing?  We actually could have consolidated it into two commands or a 2 line script:

Import-Module ServerManager

Install-WindowsFeature Hyper-V –Restart

That simple .PS1 script could have been stored in a file share, attached to a Windows deployment, or used in many different ways to automate a Hyper-V enablement.  And that’s just the tip of the iceberg.

What if you want to uninstall Hyper-V?  Well that uninstall is just like the install:

Uninstall-WindowsFeature Hyper-V –Restart –Whatif

Hyper-V is now up and running, and ready to manage.  Sure, you can use the Hyper-V console, and to be honest, that is my primary tool.  But many will want to use PowerShell for automation.  Here’s how you can get started:

Import the modules with:

Import-Module Hyper-V

And list the cmdlets:

Get-Command –Module Hyper-V

There’s lots there to get playing with!