SHiPS

SHiPS is one of those very useful modules that you didn't know you were missing, but which you cannot stop using once you've tried it. The module is available from the PowerShell Gallery, and is actively developed on GitHub. SHiPS can be used to create a custom PSProvider.

To get started with SHiPS, review our section on classes in Chapter 6, Working with Data. SHiPS uses classes and inheritance to build out your hierarchy in PowerShell.

In any PSDrive, there are either containers or leaves. A container would, for example, be a certificate store or a folder, whereas a leaf would be a certificate or a file. Where your data comes from doesn't matter, as long as it can be accessed from within PowerShell.

As an example, we will build access to our private cloud, running on HyperV with SHiPS. The cloud environment contains a tenant as a container, which in our example will simply be localhost. The tenant contains multiple resource types organized in further containers, such as disks, network switches, and machines. The leaves are in those containers.

Our first piece of the puzzle is our root directory, which we step into. We will just call this CloudCloud contains your tenants and nothing else, as you can see in the following piece of code:

class Cloud : SHiPSDirectory
{
Cloud(
[string]$name) : base($name)
{
}

[object[]] GetChildItem()
{
# Return some unique tenant ids
return ([Tenant]::new('localhost'))
}
}

The cloud inherits from SHiPSDirectory, has a name, and returns items when the GetChildItem function is called. Next, we need to create our Tenant class, which contains one container each for Disks, Machines, and VirtualSwitch:

class Tenant : SHiPSDirectory
{
Tenant(
[string]$name) : base($name)
{
}

[object[]] GetChildItem()
{
$obj = @()
$obj += [Machine]::new('Machines')
$obj += [Disk]::new('Disks')
$obj += [VirtualSwitch]::new('Switches')
return $obj;
}
}

Lastly, we need the classes that are returned when GetChildItem is called within a tenant. Each of those classes will again be a container; the items contained are simply objects that are returned with the Hyper-V cmdlets. The following code sample illustrates this:

class VirtualSwitch : SHiPSDirectory
{
VirtualSwitch(
[string]$name) : base($name)
{
}

[object[]] GetChildItem()
{
return (Get-VMSwitch)
}
}

class Disk : SHiPSDirectory
{
Disk(
[string]$name) : base($name)
{
}

[object[]] GetChildItem()
{
return (Get-VM | Get-VMHardDiskDrive | Select -Expand Path)
}
}

class Machine : SHiPSDirectory
{
Machine(
[string]$name) : base($name)
{
}

[object[]] GetChildItem()
{
return (Get-VM)
}
}

If you save all those files in a module, or you are using the module in our code repository, you can then go on and mount your new drive in PowerShell. Observe in the following screenshot how our pieces fit together to create a new, browsable structure in PowerShell:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.223.123