提供程序(PSProvider):本质上是一个适配器。它能够像访问磁盘驱动器同样访问某些数据存储介质。shell
驱动程序(PSDrive):驱动程序可经过一个特定的提供程序去链接到某些存储数据的介质。ide
获取本机上的提供程序:测试
PS C:\Windows\system32> Get-PSProvider Name Capabilities Drives ---- ------------ ------ Registry ShouldProcess, Transactions {HKLM, HKCU} Alias ShouldProcess {Alias} Environment ShouldProcess {Env} FileSystem Filter, ShouldProcess, Credentials {C, D, E, F} Function ShouldProcess {Function} Variable ShouldProcess {Variable}
经过Get-PSProvider的执行结果,观察提供程序和驱动程序二者的关系。若是用磁盘和分区来类比的话,提供程序就是一块磁盘,驱动程序就是这块磁盘上划分的几个分区。要想访问磁盘上的数据必须先进入某个分区,咱们没法绕过度区直接打开或修改磁盘上的数据。code
因此Registry、Alias、Environment等提供程序就是存储数据的底层介质,HKLM,、HKCU、Alias、Env等驱动程序就是上层的分区。而咱们要想访问这些介质中的数据,必须先打开HKLM,、HKCU、Alias等驱动程序,就像打开C盘、D盘同样,下一步才能选择其中的文件和数据。对象
从 Get-PSProvider的执行结果能够看出每一个提供程序都有各自不一样的功能。这一点很重要,由于这决定了咱们如何使用这些提供程序。下面是常见的一些功能描述:接口
-whatif
和-confirm
参数,保证咱们正式执行这部分脚本以前能够对他们进行测试。-Filter
参数。-Credentials
参数的做用。Windows文件系统主要由三种对象组成:磁盘驱动器、文件夹和文件。磁盘驱动器是最上层的对象,包含文件夹和文件。文件夹是一种容器对象,它能够包含文件以及其余文件夹。文件不是容器对象,它能够算做最小单位的对象。事务
Powershell中的术语和文件系统中的略有不一样。由于PSDrive 可能并非指向某个文件系统——好比PSDrive能够映射到注册表,因此powershell并不会使用“文件”和“文件夹”的说法。相反,powershell采用更通俗的说法“项”(Item)。一个文件或者文件夹都叫项,尽管本质他们是不一样的。因此powershell中操做对象的命令都包含“Item”字符。使用下列命令搜索包含“Item”的Cmdlet:ci
PS C:\> Get-Command -Noun *Item* CommandType Name Version Source ----------- ---- ------- ------ Function Get-DAEntryPointTableItem 1.0.0.0 DirectAccessClientComponents Function Get-TestDriveItem 3.4.0 Pester Function New-DAEntryPointTableItem 1.0.0.0 DirectAccessClientComponents Function Remove-DAEntryPointTableItem 1.0.0.0 DirectAccessClientComponents Function Rename-DAEntryPointTableItem 1.0.0.0 DirectAccessClientComponents Function Reset-DAEntryPointTableItem 1.0.0.0 DirectAccessClientComponents Function Set-DAEntryPointTableItem 1.0.0.0 DirectAccessClientComponents Cmdlet Add-ClusterVMMonitoredItem 2.0.0.0 FailoverClusters Cmdlet Clear-Item 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Clear-ItemProperty 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Copy-Item 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Copy-ItemProperty 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Get-ChildItem 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Get-ClusterVMMonitoredItem 2.0.0.0 FailoverClusters Cmdlet Get-ControlPanelItem 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Get-Item 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Get-ItemProperty 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Get-ItemPropertyValue 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Invoke-Item 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Move-Item 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Move-ItemProperty 3.1.0.0 Microsoft.PowerShell.Management Cmdlet New-Item 3.1.0.0 Microsoft.PowerShell.Management Cmdlet New-ItemProperty 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Remove-ClusterVMMonitoredItem 2.0.0.0 FailoverClusters Cmdlet Remove-Item 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Remove-ItemProperty 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Rename-Item 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Rename-ItemProperty 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Set-Item 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Set-ItemProperty 3.1.0.0 Microsoft.PowerShell.Management Cmdlet Show-ControlPanelItem 3.1.0.0 Microsoft.PowerShell.Management
对上面的命令名称构成的一些解释:文档
上面的Cmdlet都是通用的,由于它们须要处理各类不一样的数据源。可是须要注意的是,某些Cmdlet在特定场合下不必定能正常工做。就是说全部的提供程序都支持这些 Cmdlet 的使用,可是不支持部分参数的使用。从提供程序的功能描述部分(1.2节)中能够找到缘由。string
例如:文件系统不支持事务,因此在文件系统驱动器下使用命令时都不支持-UseTransaction
参数;注册表不支持 Filter 功能,因此在注册表驱动器中使用命令时,也不能使用-Filter
参数。
另外某些 PSProvider 并不具备对应的项属性。好比 Environment 提供程序主要的做用是访问Windows 中的环境变量,但它并无任何项属性:
PS C:\> Get-ItemProperty -Path Env:\PSModulePath Get-ItemProperty : 没法使用接口。此提供程序不支持 IPropertyCmdletProvider 接口。 所在位置 行:1 字符: 1 + Get-ItemProperty -Path Env:\PSModulePath + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotImplemented: (:) [Get-ItemProperty], PSNotSupportedException + FullyQualifiedErrorId : NotSupported,Microsoft.PowerShell.Commands.GetItemPropertyCommand
在使用提供程序时,首先须要熟悉的命令为 Set-Location。它的功能是将Shell中当前路径变动为不一样的路径。使用 Set-Location命令进入不一样的驱动程序,必须在驱动程序末尾加上:\
(这里使用斜杠效果也是同样的:/
),例如:
PS C:\> Set-Location d:\ PS D:\> Set-Location env:\ PS Env:\> Set-Location Function:\ PS Function:\> Set-Location HKCU:\ PS HKCU:\>
建立一个新的项的命令是 New-Item,因为该命令在全部提供程序中都能使用,因此使用时必须指定新项的类型,例如文件夹、文件或者注册表。例如:
PS C:\> New-Item -Name 123 -ItemType Directory 目录: C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2019/4/2 22:28 123
大部分的 Cmdlet 都包含-Path
参数,默认状况下该参数支持通配符。例如咱们查看 Get-ChildItem 的帮助文档中关于-Path
参数的部分:
PS C:\> help Get-ChildItem -Parameter path -Path <String[]> Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (.). 是否必需? False 位置? 0 默认值 Current directory 是否接受管道输入? True (ByPropertyName, ByValue) 是否接受通配符? True
*
通配符代码0个或多个字符,?
通配符仅表明单个字符。因为它们是通配符,因此在文件夹或文件的名称中不容许使用这两个字符。可是在其余类型的数据存储中,Item 的名称中是能够包含*
和?
的。例如注册表中的项名称就可使用*
和?
,这就致使一个问题。当一个路径参数中使用了*
和?
powershell该如何识别,是做为通配符仍是名称中的一个字符。针对该问题,powershell给出的解决办法是增长一个参数-LiteralPath
。
PS C:\> help Get-ChildItem -Parameter liter* -LiteralPath <String[]> Specifies, as a string arrya, a path to one or more locations. Unlike the Path parameter, the value of the LiteralPath parameter is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences. 是否必需? True 位置? named 默认值 None 是否接受管道输入? True (ByPropertyName) 是否接受通配符? False
若是要查询的名称中带有*
和?
,就要使用-LiteralPath
参数,而不能使用-Path
。注意-LiteralPath
不能隐式赋予,使用时必须写上参数名称。