Windows 8 Active Directory PowerShell Provider

October 27, 2011 at 7:03 PMAndy Schneider

One of the most potentially useful features of the AD tools provided by Microsoft is the AD PowerShell Provider. A Provider in PowerShell allows a user to interact with a data structure similarly to how they would interact with a file system. You can change directories, list items, create new items, and delete items. This is a really good model for any kind of hierarchical data. AD is a great example. I like to use PowerShell providers to find and navigate through data very quickly.

The problem I have with the AD Provider is the names that are used when you navigate into Active Directory. In this case, a picture is worth a thousand words.

image

What do you mean the path doesn’t exist? According to the output of my last ls command, I should be able to cd into windev, configuration, and 3 others. It turns out you don’t use the name of the “Name” property. You have to cd into the “Distinguished Name”

image

To me, that is very unintuitive and does not align well with the PowerShell mantra, “Think”, “Type”, “Get.” Well in the Windows 8 Developer Preview Server, you can address this issue. The default drive AD: still exists and behaves in the same way, but it doesn’t have to stay that way.

You can remove the AD PSDrive and create your own new one. The trick here is to use the –FormatType switch with the parameter “canonical.” This will change how the drive is set up and what names the provider binds to

image

Now we can navigate through AD much more easily.

image

Posted in:

Tags:

Random Collection of New Features found while Exploring PowerShell V3

September 18, 2011 at 3:46 AMAndy Schneider

I spent some time this afternoon playing around with PowerShell V3 and came across a (very random) set of new features and functionality that I thought I would highlight here. My wife is pretty convinced I have Adult Attention Deficit Disorder. This random collection of features and tips seem to corroborate that theory. At any rate, here is what I have found.

  • There is a new Add-On model in the ISE. From the help I read “Windows PowerShell ISE now supports add-on tools, which are Windows Presentation Foundation (WPF) controls that are added by using the object model.” It is going to be awesome to see what some of the UI/WPF folks come up with using this new functionality.

The ISE now lets you edit XML natively just like you can with PS1 files. You can even set the token colors using $PSIse.Options.XMLTokenColors.

  • The ISE now supports Intellisense. This is a great new feature. One thing that I found extremely helpful was to switch the option $psise.Options.UseEnterToSelectInCommandPanelIntellisense = $true. If you don’t do this, by default, when intellisense pops up and you hit enter, the command will execute without adding what you selected via Intellisense to the command. There is also an option to use enter to select intellisense in the script pane. I recommend setting both of these to $true.

There are cmdlets to mess with IP Addresses. Get-NetIPaddress and Set-IPAddress to just name the first two obvious ones. This is just an example of the breadth of OS coverage we have. If you know PowerShell, you can manage all of Windows.

There is some new, simpler syntax for where and foreach-object. You no longer need to use braces. You can say something like get-service | where name –like svc*

Last but not least, we now have access to control panel items directly in PowerShell. You can get control panel items and start them. One example is Get-ControlPanelItem System | Start-ControlPanelItem

So much good stuff.. It feels like we might get to PowerShell v4 before we finish finding out how to use all the goodness in V3!

Posted in:

Tags:

A new PowerShell V3 Cmdlet - Invoke-WebRequest

September 18, 2011 at 1:00 AMAndy Schneider

Playing around with PowerShell V3, I just came across an incredibly powerful new cmdlet called Invoke-WebRequest. This cmdlet will return content of a web site organized by properties. You can access the following properties from the returned Microsoft.PowerShell.Commands.HtmlWebResponseObject.

AllElements

BaseResponse

Content

Forms

Headers

Images

InputFields

Links

ParsedHTML

RawContent

RawContentLength

RawcontentStream

Scripts

StatusCode

StatusDescription

With this cmdlet,I was able to get all the links on my blog

$blog = invoke-webrequest get-powershell.com

$blog.links

This is really going to make it super easy to script against web pages and parse them when you don’t have a web service to hit. Again, PowerShell making it easy to reach into a messy world.

Posted in: PowerShell | V3 | Web

Tags:

PowerShell V3 Enables Windows 8 Server to be optimized for the Cloud

September 15, 2011 at 6:44 PMAndy Schneider

Windows Server 8 took the stage on Day 2 of the BUILD conference. This OS was built from the ground up to be highly optimized for private and public cloud computing. To have an OS that is optimized for the cloud, there must be a management framework  built into the OS that can be used to manage hundreds of thousands of servers reliably and securely.

The Windows Management Framework is that framework. The entire stack has been updated. Windows 8 brings with it a new version of WMI, WMI v2. It is now an order of magnitude easier to write WMI providers. WSMan, the HTTP protocol used under the hood of PowerShell remoting has been updated as well. In a presentation at Build, Jeffrey Snover mentioned that something gets built very well when you rely on it 100%. Microsoft is going all in on WSMan and the Windows Management Framework.

To manage an OS optimized for cloud computing, GUI’s are simply not an option. Windows 2008 introduced the first version of Server Core, removing the GUI from the OS completely. Microsoft is starting to push Server Core as the preferred installation for Windows Server 8. To enable this, remoting must work all the time. Therefore, in Windows 8, PowerShell remoting and WSMan is on by default, out of the box.

Not only is remoting on by default, but MS has invested a great deal of engineering into this technology to make it incredibly robust. Users can now create a session, connect to it, disconnect, and reconnect at a later time. This enables many different scenarios. Just imagine you are at work and kick of a set of jobs across 250 servers that will take a while. You can disconnect from those sessions and go home. After dinner, you can connect back in and check on the jobs by reconnecting to those sessions.

In addition to having a great robust remoting story, the Framework must also be able to cover as much of the OS as possible. MS has made it incredibly easy for teams to add PowerShell cmdlets to their product. When teams create a WMI V2 provider, there are now tools that can be used to automatically generate all the cmdlets associated with their WMI provider. Because of this, there are now cmdlets to manage nearly all aspects of Windows, including low level disk management and networking.

Finally, on top of all these requirements, the Framework must run as fast as possible. PowerShell V3 leverages the Dynamic Language Runtime (the DLR) in its execution engine. This allows script code that is running a lot to be compiled on demand and then executed. The PowerShell team has seen up to a 600% increase in performance of script execution because of this change.

The combination of the performance increase, a robust infrastructure, and an incredibly large foot print across the OS will make Windows 8 a highly optimized cloud computing platform. It is absolutely clear that if you have invested in PowerShell, you will not be sorry. It is likely one of the best investments you have ever made in your IT Pro career.

Posted in: PowerShell | V3 | Cloud | Windows Server 8

Tags:

PowerShell V3

September 14, 2011 at 7:52 AMAndy Schneider

I have the privilege of attending BUILD this year. I am super excited about Windows 8. All the BUILD attendees were given a new Samsung Slate PC running a pre-release Developer Edition of Windows 8. One of the first things I did was crack open PowerShell, looked at $psversiontable and indeed, I was running PowerShell V3!

  1. I’ll be sure to blog more details, but a couple things I noticed right away

ISE has Intellisense

44 Modules were available out of the box

1015 Cmdlets returned from Get-Command

There are a bunch of  *-PSWorkflow cmdlets

There are iSCSI cmdlets

There are TPM (Trusted Platform Module) Cmdlets

Basically, it looks like you can manage just about anything on the PC with PowerShell V3.

I’ll be posting more updates here on the blog as well as on Twitter @andys146

 

Posted in: PowerShell | V3

Tags:

Use Nuget to Share PowerShell Modules in your Enterprise

May 23, 2011 at 7:13 PMAndy Schneider

Nuget is not just for developers! If you are an IT Pro, you can use it as well. Nuget is a relatively new tool from Microsoft that provides the ability for people to easily share and use code. Microsoft is marketing it to developers as the way to share and use Open Source code in Visual Studio Projects. It does a great job at this and is really starting to take off in the developer community. But we, IT Pros, can take it and use it for PowerShell Modules.

So why would you want to use Nuget to source your enterprise scripts?

  • Versioning– You can match your Module Version numbers with the Nuget Package version
  • Dependencies – In a Nuget Package, you can say this package depends on this package, so check to see if its installed and if not, please install it for me.
  • At least 10 other cool things that I haven’t discovered yet.
    Nuget is a client server application. You can save a bunch of packages up on a server, and you use the client to download and install these packages. Microsoft hosts a Nuget server that currently has around 1500 published packages that you can use.

Last week at Teched Scott Hanselman gave a talk on using Nuget in the Enterprise.  The quick version of the talk is that you can host your own internal Nuget Server. In fact, all you really need to do is set up a file share. The other key is that all you need to access the packages on a Nuget Server is the Nuget command line tool

After seeing this talk, I was thinking this could be a great tool to distribute and share PowerShell Modules with the rest of my IT Department. What I didn’t know was whether or not it would be easy to package up a module and then install it using Nuget. So I started playing. As an example, I am going to use a Module I wrote to manage my work items in TFS.

The first thing to do is download the Nuget command line tool. After you download it, be sure to unblock it because it was downloaded from the Internet. I put the file in a directory in my documents folder and created an alias to it.

image

Once that is set up, you can start creating a package. There are a couple things you need to do if you are using the command line version of Nuget.

First, you need to create a spec file.  You can do this with the command nuget spec, as shown below. You can see that it creates an XML file.

image

There are all kinds of properties here that you can set. I am going to leave everything as default for demo purposes, except that I am going to rip out the dependency node and change the tag values to something more reasonable. This could obviously be automated very easily, but I simply used good old notepad2.

Now the dependency node is gone and tags are updated.

image

Once we have this we can create the package, again using nuget.exe. I really should mention that there are conventions for structuring a package. You can read all about it on http://nuget.org. I am going to completely ignore them because all I really care about the PowerShell Module.

Nuget.exe has a parameter called pack. You can specify a directory to package up by specifying the basepath parameter, the spec file, and the output directory.

image

Now I have a package called tfs.1.0.nupkg. NUPKG is the extension for Nuget Package. All this really is a zip file. In fact,you can rename this to tfs.1.0.zip and unzip the contents. In there is all the files for my modules.

OK, so now that I have a package, I need to put it somewhere where people can access it and pull it down with a Nuget client. There are tons of articles on how to create your own Nuget Server. http://docs.nuget.org/docs/creating-packages/hosting-your-own-nuget-feeds.

If you just want to use a file share, you can do that. Or you can actually build a NuGet Server Web Site. I guarantee that you won’t have to write any code, but you would have to install Visual Studio Express, or get a developer to build a quick app for you. NuGet server itself is a Nuget Package. With Visual Studio installed, I had my NuGet Server up and running in under 10 minutes. 

After you set up your Nuget Server, there is a directory where you can place your packages. It is appropriately named, “Packages.” Here is the directory where I have all my files for my local instance of IIS. You can see that I have two packages up here, one for the DataOnTap Module and the one we just created.

image

Here is the client listing the available packages from this source.

image

For the sake of demonstration, I created a folder called c:\modules and used nuget.exe to install my module there.

image

The only thing wrong with this is that the folder name contains the version and the nupkg file is still around. This can all be cleaned up pretty easily, manually, using PowerShell, or probably with some Nuget options I haven’t found yet.

I will be testing this out this week too see what else I can come up with. I am looking forward to seeing how versioning works. Also, I want to wrap some of this functionality with Advanced Functions.

If you install Visual Studio and Nuget, you will also get a set of PowerShell cmdlets to manage packages. However, as of right now the Module is not a separate download. I’ll look into this and let you know what else I find. I am sure there will be at least one or two follow up posts on this as I learn more about NuGet.

Posted in: PowerShell | Modules | Nuget

Tags:

When Read-Host doesn’t quite cut it

May 6, 2011 at 11:37 PMAndy Schneider

Ninety percent of the time when you are writing PowerShell code, you can use parameters in advanced functions to get the data you need to get from a user. However, there are times that you may want to have a bit more control over the user experience. Out of the box, PowerShell provides a cmdlet called Read-Host.

image

From here you can use the variable in your code

image

This is cool but what if you want to offer choices to the user, and what if you want to customize the caption in the window in addition to the actual message. It turns out PowerShell has some messaging capabilities built in that are not exposed as direct Cmdlets. If you have used –confirm and –whatif on some Cmdlets, you have probably seen this UI.

image

I thought it would be pretty cool to be able to use this functionality with my own custom choices, caption, and message. I wrote a function called New-Choice that is up on Poshcode

Here are some example of using the function.

In PowerShell.exe

image

In ISE

image

And even in PowerGUI

image

In summary, this function provides a great way to provide a rich user experience and maintain control of possible inputs the user could provide.

Posted in: PowerShell | Read-Host | User Input

Tags:

NetApp PowerShell Toolkit 1.4 Released! Get-NaHyperVHost

May 4, 2011 at 9:09 PMAndy Schneider

Last Friday, NetApp released version 1.4 of their PowerShell Toolkit. They have a total of 501 Cmdlets with this release.

image

Their stuff just keeps getting better and better.

There are a couple of Cmdlets that I wanted to highlight because they were extremely useful for me the other day. We have several 8 to 10 node Hyper-V Clusters all using NetApp and iSCSI storage. We have been moving VM’s to faster disks on our NetApp. One challenge that can crop up is correlating which VM’s in HyperV are stored on which Volume or QTree on our NetApp.

We have a great Ops guy who is super nitpicky about naming standards and because of our naming standards, we know exactly how everything lines up, at least for the VM’s that have been created in the last year or so. The problem is some legacy VM’s that don’t adhere to our standards in development and test environments. This is where Get-NaHyperV comes in to save the day. This CmdLet has actually been around for a while, but with this release, it now supports clustered disks, which is exactly what we needed. In addition to getting info on our CSV’s and exact location of VHD’d, we were also able to enumerate exactly which NetApp Volume, QTree, and LUN the VM Disk resources were associated with. Absolutely brilliant!

image

Here’s a screenshot of an example from the NetApp Help on the cmdlet

image

There is also a more generic cmdlet call Get-NaHostDisk which does essentially the same thing for disks that are on the SAN but not necessarily associated with Hyper-V VM’s. This can be used for clustered SQL or something else that uses shared storage.

I use these cmdlets nearly everyday. I can’t tell you how much they have streamlined our processes and tooling for working with our storage on a daily basis.  NetApp, keep up the good work!

Posted in: PowerShell | NetApp | Toolkit | Hyper-V | Storage

Tags:

NetApp PowerShell Toolkit has a PowerShell Provider

April 21, 2011 at 7:11 PMAndy Schneider

I was at the PowerShell Deep Dive the first half of this week. During a break, I had the chance to meet Clinton Knight, the lead guy behind the NetApp PowerShell Toolkit. Since v1, I have been asking for a provider. Well it turns out that they slipped one in in their latest release and I didn’t even notice. That will teach me to start reading release notes.

Anyway, now with a filer you can do things like the following. This is going to make it even easier now to navigate all your volumes, q-trees, and LUN’s.

image

Another very cool feature that I just found out about as well is the ability to store credentials to connect to different filers. They have three cmdlets that allow you to Get, Add, and remove credentials.These are encrypted and stored on disk so that only the user that created them can access them. Definitely a nice touch.

image

NetApp has really done a fantastic job with their Cmdlets. Their implementation of PowerShell is by far one of the best I have seen, including Microsoft and third parties. Companies looking to use PowerShell in their products should definitely take a look at what NetApp has done.

Posted in: NetApp | PowerShell Provider

Tags:

Use PowerShell to find out if a Disk Partition is GPT or MBR

April 21, 2011 at 2:08 AMAndy Schneider

When you go to create a partition on a disk in Windows, you can use two different styles, MBR and GPT. The stand for Master Boot Record and Guid Partition Table. One of the major differences between these two is the size of partition you can create. If you have SAN storage and are using CSV’s in Hyper-V, you very well might bump up against this. MBR partitions are limited to 2 Terabytes (2.19 * 1012). GPT allows for a maximum size of 9.4 ZettaBytes (9.4 x 1021) In other words, at this point size doesn’t matter, at least in this decade.

Anyway, I had a situation where I needed to use diskpart.exe to reset a disk signature. The problem was, disk signatures are GUID’s for GPT disks and a set of hex digits for MBR’s. I needed to figure out how to code this and do the appropriate task in DiskPart. I ended up using WMI to figure it out

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027

Function Get-DiskPartitionStyle {

param

(
[Parameter( ValueFromPipeline=$true,
ValueFromPipelineByPropertyName = $true)]
$Disks="*"
,

[
Parameter( ValueFromPipeline=$true,
ValueFromPipelineByPropertyName = $true)]
$Computer ="."
)

PROCESS
{

$partitions = Get-WmiObject Win32_DiskPartition -computer $Computer

foreach ($disk in $Disks) {
    $partitions | Select @{name="DiskID" ;e={$_.DiskIndex}},Type,Name |
    Where-Object {$_.DiskID -like $disk}
        }
    }
}


Get-DiskPartitionStyle
Get-DiskPartitionStyle
-disks 10
Get-DiskPartitionStyle -disks 0,5,7,10

I used PowerShell to create an Advanced Function. By Default, it will return all partitions and their disks. You can also specify just the disk you want as well an array of disks.

Posted in:

Tags: