• No results found

PROVIDER DATA ACCESSED DRIVE

In document Windows PowerShell 2.0 (Page 81-88)

Using Providers

PROVIDER DATA ACCESSED DRIVE

Alias Windows PowerShell

aliases

{Alias}

Certificate X509 certificates for digital signatures

{Cert}

Environment Windows environment

variables

{Env}

FileSystem File system drives,

directories, and files

{C, D, E, …}

Function Windows PowerShell

functions

{Function}

Registry Windows registry {HKLM, HKCU}

Variable Windows PowerShell

variables

{Variable}

WSMan WS-Management {WSMan}

PowerShell includes a set of cmdlets that are specifically designed to manage the items in the data stores that are exposed by providers. You use these cmdlets in the same ways to manage all the different types of data that the providers make avail- able to you. Table 3-4 provides an overview of these cmdlets.

TABLE 3-4 Cmdlets for Working with Data Stores CMDLET DESCRIPTION

Get-PSDrive Gets all or specified PowerShell drives in the current console. This includes logical drives on the computer, drives mapped to network shares, and drives exposed by Windows PowerShell providers. Get-PSDrive does not get Windows mapped drives that are added or created after you open PowerShell. However, you can map drives using New-PSDrive, and those drives will be available.

Get-PSDrive [-PSProvider Strings] [-Scope String]

TABLE 3-4 Cmdlets for Working with Data Stores CMDLET DESCRIPTION

New-PSDrive Creates a PowerShell drive that is mapped to a location in a data store, which can include a shared network folder, a local directory, or a registry key. The drive is available only in the current PowerShell console.

New-PSDrive [-Credential Credential] [-Description String]

[-Scope String] [-Name] String [-PSProvider] String [-Root] String

Remove-PSDrive Removes a PowerShell drive that you added to the current console session. You cannot delete Windows drives or mapped network drives created by using other methods.

Remove-PSDrive [-Force]

[-PSProvider Strings] [-Scope String] [[-LiteralName] | [-Name]] Strings

Get-ChildItem Gets the items and child items in one or more specified locations.

Get-ChildItem [[-Path] Strings] [[-Filter] String] [AddtlParams] Get-ChildItem [[-Filter] String] [-LiteralPath] Strings [AddtlParams] AddtlParams=

[-Exclude Strings] [-Force] [-Include Strings] [-Name] [-Recurse]

Get-Item Gets the item at the specified location.

Get-Item [[-LiteralPath] | [-Path]] Strings [AddtlParams] AddtlParams=

[-Credential Credential]

[-Exclude Strings] [-Filter String] [-Force] [-Include Strings]

New-Item Creates a new item.

New-Item [-Credential Credential] [-Force] [-ItemType String] [-Path Strings] [-Value Object] -Name String

TABLE 3-4 Cmdlets for Working with Data Stores CMDLET DESCRIPTION

Set-Item Changes the value of an item to the value specified in the command.

Set-Item [-Value] Object]

[[-LiteralPath] | [-Path] Strings [AddtlParams]

AddtlParams=

[-Credential Credential]

[-Exclude Strings] [-Filter String] [-Force] [-Include Strings] [-PassThru]

Remove-Item Deletes the specified item.

Remove-Item [[-LiteralPath] | [-Path]] Strings [AddtlParams] AddtlParams=

[-Credential Credential]

[-Exclude Strings] [-Filter String] [-Force] [-Include Strings] [-Recurse]

Move-Item Moves an item from one location to another.

Move-Item [[-Destination] String] [[-LiteralPath] |

[-Path]] Strings [AddtlParams] AddtlParams=

[-Credential Credential]

[-Exclude Strings] [-Filter String] [-Force] [-Include Strings] [-PassThru]

Rename-Item Renames an item in a Windows PowerShell provider namespace.

Rename-Item [-Credential Credential] [-Force] [-PassThru] [-Path] String [-NewName] String

TABLE 3-4 Cmdlets for Working with Data Stores CMDLET DESCRIPTION

Copy-Item Copies an item from one location to another within a namespace.

Copy-Item [[-Destination] String] [[-LiteralPath] |

[-Path]] Strings [AddtlParams] AddtlParams=

[-Container] [-Credential Credential] [-Exclude Strings] [-Filter String] [-Force] [-Include Strings] [-PassThru] [-Recurse]

Clear-Item Deletes the contents of an item but does not delete the item.

Clear-Item [[-LiteralPath] | [-Path]]

Strings [AddtlParams]

AddtlParams=

[-Credential Credential]

[-Exclude Strings] [-Filter String] [-Force] [-Include Strings]

Invoke-Item Performs the default action on the specified item.

Invoke-Item [[-LiteralPath] | [-Path]] Strings [AddtlParams] AddtlParams=

[-Credential Credential]

[-Exclude Strings] [-Filter String] [-Include Strings]

Clear- ItemProperty

Deletes the value of a property but does not delete the property.

Clear-ItemProperty [[-LiteralPath] | [-Path]] Strings [-Name] String [AddtlParams]

AddtlParams=

[-Credential Credential]

[-Exclude Strings] [-Filter String] [-Force] [-Include Strings] [-PassThru]

TABLE 3-4 Cmdlets for Working with Data Stores CMDLET DESCRIPTION

Copy- ItemProperty

Copies a property and value from a specified location to another location.

Copy-ItemProperty [[-LiteralPath] | [-Path]] Strings [-Destination] String [-Name] String [AddtlParams]

AddtlParams=

[-Credential Credential]

[-Exclude Strings] [-Filter String] [-Force] [-Include Strings] [-PassThru]

Get- ItemProperty

Gets the properties of a specified item.

Get-ItemProperty [-Name] String [[-LiteralPath] | [-Path]] Strings [AddtlParams]

AddtlParams=

[-Credential Credential]

[-Exclude Strings] [-Filter String] [-Include Strings]

Move- ItemProperty

Moves a property from one location to another.

Move-ItemProperty [[-LiteralPath] | [-Path]] Strings

[-Destination] String [-Name] String [AddtlParams]

AddtlParams=

[-Credential Credential]

[-Exclude Strings] [-Filter String] [-Force] [-Include Strings] [-PassThru]

New- ItemProperty

Creates a property for an item and sets its value.

New-ItemProperty [-PropertyType String] [-Value Object] [[-LiteralPath] | [-Path]] Strings [-Name] String [AddtlParams]

AddtlParams=

[-Credential Credential]

[-Exclude Strings] [-Filter String] [-Force] [-Include Strings]

TABLE 3-4 Cmdlets for Working with Data Stores CMDLET DESCRIPTION

Remove- ItemProperty

Deletes the specified property and its value from an item.

Remove-ItemProperty [[-LiteralPath] | [-Path]] Strings

[-Name] String [AddtlParams] AddtlParams=

[-Credential Credential]

[-Exclude Strings] [-Filter String] [-Force] [-Include Strings]

Rename-Item- Property

Renames the specified property of an item.

Rename-ItemProperty [[-LiteralPath] | [-Path]] Strings

[-Name] String [-NewName] String [AddtlParams]

AddtlParams=

[-Credential Credential]

[-Exclude Strings] [-Filter String] [-Force] [-Include Strings] [-PassThru]

Set-ItemProperty Creates or changes the value of the specified property of an item.

Set-ItemProperty -InputObject Object [-LiteralPath] Strings [AddtlParams] Set-ItemProperty [-Name] String [-Value] Object

[AddtlParams]

Set-ItemProperty [-Path] Strings [AddtlParams]

AddtlParams=

[-Credential Credential]

[-Exclude Strings] [-Filter String] [-Force] [-Include Strings] [-PassThru]

In addition to the built-in cmdlets, providers can:

N Have custom cmdlets that are designed especially for related data. N Add “dynamic parameters” to the built-in cmdlets that are available only

The drive associated with each provider is listed in the default display of Get-PSProvider, but you can get more information about a provider drive by using the Get-PSDrive cmdlet. For example, the Registry provider makes the HKEY_ LOCAL_MACHINE root key available as the HKLM drive. To find all the properties of the HKLM drive, enter the following command:

get-psdrive hklm | format-list *

You can view and navigate through the data in a provider drive just as you would data in a file system drive. To view the contents of a provider drive, use the Get-Item or Get-ChildItem cmdlet. Type the drive name followed by a colon (:). For example, to view the contents of the Function drive, type:

get-childitem function:

You can view and manage the data in any drive from another drive by including the drive name in the path. For example, to view the HKLM\Software registry key in the HKLM drive from another drive, type:

get-childitem hklm:\software

To get into the drive, use the Set-Location cmdlet. Remember the colon when specifying the drive path. For example, to change your location to the root of the Function drive, type set-location function:. Then, to view the contents of the Function drive, type get-childitem.

You can navigate through a provider drive just as you would a hard drive. If the data is arranged in a hierarchy of items within items, use a backslash (\) to indicate a child item. The basic syntax is:

Set-location drive:\location\child-location\...

For example, to change your location to the HKLM\Software registry key, use a Set-Location command, such as:

set-location hklm:\software

You can also use relative references to locations. A dot (.) represents the current location. For example, if you are in the C:\Windows\System32 directory and you want to list its files and folders, you can use the following command:

get-childitem .\

When PowerShell loads providers, the providers can add dynamic parameters that are available only when the cmdlet is used with that provider. For example, the Certificate drive adds the –CodeSigningCert parameter to the Get-Item and Get-ChildItem cmdlets. You can use this parameter only when you use Get-Item or Get-ChildItem in the Cert drive.

Although you cannot uninstall a provider, you can remove the Windows Power- Shell snap-in for the provider from the current session. To remove a provider, use

removes all the contents of the snap-in, including providers and cmdlets. This makes the related providers and cmdlets unavailable in the current session.

Another way to remove features made available based on snap-ins is to use the Remove-PSDrive cmdlet to remove a particular drive from the current session. When you remove a drive, the data on the drive is not affected, but the drive is no longer available in the current session.

Often, you’ll want to ensure that a particular PowerShell provider or PSDrive is available before you try to work with its features. The easiest way to do this is to attempt to perform the action or run a script only if the provider or PSDrive is avail- able. Consider the following example:

iIf (get-psprovider -psprovider wsman -erroraction silentlycontinue) {

Code to execute if the provider is available. } else {

Code to execute if the provider is not available. }

Here, when the WSMan provider is available, the statement in parentheses evaluates to True, and any code in the related script block is executed. When the WSMan provider is not available, the statement in parentheses evaluates to False, and any code in the Else statement is executed. Note also that I set the –ErrorAction parameter to SilentlyContinue so that error messages aren’t written to the output if the provider is not found.

In document Windows PowerShell 2.0 (Page 81-88)