Using providers and PSDrives
Until now, you have only seen cmdlets. Cmdlets are PowerShell commands. However, PowerShell has another import concept named providers. Providers are accessed through named drives or PSDrives. In the following sections, providers and PSDrives will be explained.
Using providers
A PowerShell provider is a piece of software that makes data stores 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 of 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 of the cmdlets with these nouns:
PowerCLI C:> Get-Command -Noun Item,ChildItem,Content,ItemProperty CommandType Name ModuleName ----------- ---- ---------- Cmdlet Add-Content Microsoft.PowerShell.Management Cmdlet Clear-Content Microsoft.PowerShell.Management Cmdlet Clear-Item Microsoft.PowerShell.Management Cmdlet Clear-ItemProperty Microsoft.PowerShell.Management Cmdlet Copy-Item Microsoft.PowerShell.Management Cmdlet Copy-ItemProperty Microsoft.PowerShell.Management Cmdlet Get-ChildItem Microsoft.PowerShell.Management Cmdlet Get-Content Microsoft.PowerShell.Management Cmdlet Get-Item Microsoft.PowerShell.Management Cmdlet Get-ItemProperty Microsoft.PowerShell.Management Cmdlet Invoke-Item Microsoft.PowerShell.Management Cmdlet Move-Item Microsoft.PowerShell.Management Cmdlet Move-ItemProperty Microsoft.PowerShell.Management Cmdlet New-Item Microsoft.PowerShell.Management Cmdlet New-ItemProperty Microsoft.PowerShell.Management Cmdlet Remove-Item Microsoft.PowerShell.Management Cmdlet Remove-ItemProperty Microsoft.PowerShell.Management Cmdlet Rename-Item Microsoft.PowerShell.Management Cmdlet Rename-ItemProperty Microsoft.PowerShell.Management Cmdlet Set-Content Microsoft.PowerShell.Management Cmdlet Set-Item Microsoft.PowerShell.Management Cmdlet Set-ItemProperty Microsoft.PowerShell.Management
To display a list of all of 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, H, D} Function ShouldProcess {Function} Registry ShouldProcess, Transactions {HKLM, HKCU} Variable ShouldProcess {Variable} Certificate ShouldProcess {Cert} WSMan Credentials {WSMan} VimDatastore ShouldProcess {vmstores, vmstore} VimInventory Filter {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
, H
, and D
, 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 of the files and folders in the root of C:
on your PC, type:
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 of your cmdlet aliases by typing:
PowerCLI C:\> Get-ChildItem alias:
Using the vSphere 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 ---- ---- -- vCenter@443 VIServer /VIServer=bl...
Use the Get-ChildItem -Recurse
parameter to display all of the items in the Inventory Provider:
PowerCLI vis:\> Get-ChildItem -Recurse
Using the vSphere 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 ---- ---- -- YourDatacenter Datacenter Datacenter-d...
You have to go one level deeper to get a listing of your datastores:
PowerCLI vmstore:\> Set-Location YourDatacenter PowerCLI vmstore:\ YourDatacenter > Get-ChildItem
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
that 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:
Datastore path: [Datastore1] LastWriteTime Type Length Name ------------- ---- ------ ---- 12/22/2013 3:14 PM Folder .dvsData 12/20/2013 8:14 PM Folder .naa.600a0b800011... 12/21/2013 10:06 AM Folder ISOfiles 12/21/2013 2:05 PM Folder vCenter Mobile Ac... 12/22/2013 1:24 PM Folder .vSphere-HA
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:\virtualmachine1\virtualmachine1.vmx -Destination c:\virtualmachine1\
Now, you can view the content of the virtualmachine1.vmx
file with:
PowerCLI C:\> Get-Content c:\virtualmachine1\virtualmachine1.vmx