Using providers and PSDrives

Until now, you have only seen cmdlets. Cmdlets are PowerShell commands. PowerShell has another import concept named providers. Providers are accessed through named drives or PSDrives. In the following sections, Using providers and Using PSDrives, providers and PSDrives will be explained.

Using providers

A PowerShell provider is a piece of software that makes datastores look like filesystems. PowerShell providers are usually part of a snap-in or a module-like PowerCLI. The advantage of providers is that you can use the same cmdlets for all the providers. These cmdlets have the following nouns: Item, ChildItem, Content, and ItemProperty. You can use the Get-Command cmdlet to get a list of all the cmdlets with these nouns:

PowerCLI C:> Get-Command -Noun Item,ChildItem,Content,ItemProperty |
Format-Table -AutoSize

The preceding command gives the following output:

CommandType Name                Version Source
----------- ----                ------- ------
Cmdlet      Add-Content         3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Clear-Content       3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Clear-Item          3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Clear-ItemProperty  3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Copy-Item           3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Copy-ItemProperty   3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Get-ChildItem       3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Get-Content         3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Get-Item            3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Get-ItemProperty    3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Invoke-Item         3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Move-Item           3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Move-ItemProperty   3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      New-Item            3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      New-ItemProperty    3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Remove-Item         3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Remove-ItemProperty 3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Rename-Item         3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Rename-ItemProperty 3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Set-Content         3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Set-Item            3.1.0.0 Microsoft.PowerShell.Mana...
Cmdlet      Set-ItemProperty    3.1.0.0 Microsoft.PowerShell.Mana...

To display a list of all the providers in your PowerCLI session, you can use the Get-PSProvider cmdlet:

PowerCLI C:> Get-PSProvider
Name         Capabilities                       Drives
----         ------------                       ------
Alias        ShouldProcess                      {Alias}
Environment  ShouldProcess                      {Env}
FileSystem   Filter, ShouldProcess, Credentials {C, A, D, Z}
Function     ShouldProcess                      {Function}
Registry     ShouldProcess, Transactions        {HKLM, HKCU}
Variable     ShouldProcess                      {Variable}
VimDatastore Filter, ShouldProcess              {vmstores, vmstore}
VimInventory None                               {vis, vi}

The VimDatastore and VimInventory providers are part of PowerCLI. You will soon learn more about the VimDatastore and VimInventory providers.

Using PSDrives

Each provider has one or more drives. For example, the FileSystem provider has drives named C, A, D, and Z, which are hard disks on my PC. You can use the drives to access the providers. Microsoft calls these drives PSDrives to prevent confusing the drives with physical drives in your computer. For instance, to get a listing of all the files and folders in the root of C: on your PC, type the following command:

PowerCLI C:> Get-ChildItem C:

The Get-ChildItem cmdlet has aliases dir, gci and ls that give you the same result:

PowerCLI C:> dir C:
PowerCLI C:> ls C:

You can use the same Get-ChildItem cmdlet to get a list of all your cmdlet aliases by typing the following command:

PowerCLI C:> Get-ChildItem alias:

The Registry PSDrive can be used to browse through the registry on your PC. The following command will list all the keys and values in the HKEY_LOCAL_MACHINESOFTWARE registry hive:

PowerCLI C:> Get-ChildItem HKLM:SOFTWARE

Using the PowerCLI Inventory Provider

The Inventory Provider gives you a filesystem-like view of the inventory items from a vCenter Server or an ESXi server. You can use this provider to view, move, rename, or delete objects by running PowerCLI commands.

When you connect to a server with the Connect-VIServer cmdlet, two PSDrives are created: vi and vis. The vi PSDrive contains the inventory of the last connected server. The vis PSDrive contains the inventory of all currently connected servers in your PowerCLI session.

You can set the location to the vis PSDrive using the Set-Location cmdlet:

PowerCLI C:> Set-Location vis:
PowerCLI vis:>

Use the Get-ChildItem cmdlet to display the items in the current location of the vis PSDrive:

PowerCLI vis:> Get-ChildItem
Name               Type      Id
----               ----      --
192.168.0.132@443  VIServer  /VIServer=vsphere.localadministrator@...

Use the Get-ChildItem -Recurse parameter to display all the items in the Inventory Provider:

PowerCLI vis:> Get-ChildItem -Recurse

Using the PowerCLI Datastore Provider

The Datastore Provider gives you access to the content of your vSphere datastores.

When you connect to a server with the Connect-VIServer cmdlet, two PSDrives are created: vmstore and vmstores. The vmstore PSDrive contains the datastores of the last connected server. The vmstores PSDrive contains the datastores of all the currently connected servers in your PowerCLI session. You can use these two default PSDrives or you can create custom PSDrives using the New-PSDrive cmdlet.

Set the location to the vmstore PSDrive with the following command:

PowerCLI C:> Set-Location vmstore:
PowerCLI vmstore:>

Display the content of the root directory of the vmstore PSDrive with the following command:

PowerCLI vmstore:> Get-ChildItem
Name     Type       Id
----     ----       --
New York Datacenter Datacenter-datacenter-2

You can also create a custom PSDrive for a datastore using the New-PSDrive cmdlet. Start with getting a datastore object and save it in the $Datastore variable:

PowerCLI C:> $Datastore = Get-Datastore -Name Datastore1

Create a new PowerShell PSDrive named ds, which maps to the $Datastore variable:

PowerCLI C:> New-PSDrive -Location $Datastore -Name ds -PSProvider
    VimDatastore -Root ""

Now, you can change your location into the PowerShell PSDrive using the Set-Location cmdlet:

PowerCLI C:> Set-Location ds:

You can get a listing of the files and directories on the datastore using the Get-ChildItem cmdlet:

PowerCLI ds:> Get-ChildItem

You will see an output similar to the following:

Name                           Type                 Id
----                           ----                 --
DC1                            DatastoreFolder
.sdd.sf                        DatastoreFolder 

Copying files between a datastore and your PC

You can use the vSphere Datastore Provider to copy files between a datastore and your PC using the Copy-DatastoreItem cmdlet.

Change the location to a subfolder using the Set-Location cmdlet with the help of the following command line:

PowerCLI ds:> Set-Location "virtualmachine1"

Copy a file or directory to the destination using the Copy-DatastoreItem cmdlet, as follows:

PowerCLI ds:virtualmachine1> Copy-DatastoreItem -Item 
    ds:virtualmachine1virtualmachine1.vmx -Destination $env:USERPROFILE

Now, you can view the content of the virtualmachine1.vmx file with the following command:

PowerCLI C:> Get-Content $env:USERPROFILEvirtualmachine1.vmx

$env:USERPROFILE is the path to your user profile, for example, C:usersusername.

Tip

Files cannot be copied directly between vSphere datastores in different vCenter Servers using Copy-DatastoreItem. Copy the files to the PowerCLI host's local filesystem temporarily and then copy them to the destination.

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

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