如何在 PowerShell 中使用数组展平?
展平是一种将参数集合作为一个单元传递的方式,这样命令更容易读取。数组展平使用不需要参数名称的展开值。这些值在数组中必须按位置编号顺序排列。
我们有一个下面的复制示例,其中我们正在将一个文件从源复制到目标。现在我们在这里不指定参数,因为我们将对源路径和目标路径使用位置参数。
如果我们查看这些参数的帮助,我们将了解它们的位置。
对于源路径。
help Copy-Item -Parameter path
输出
-Path <System.String[]> Specifies, as a string array, the path to the items to copy. Wildcard characters are permitted. Required? true Position? 0 Default value None Accept pipeline input? True(ByPropertyName, ByValue) Accept wildcard characters? true
如果你查看路径参数的位置是 0,因此我們可以提供值而不首先指定。类似地,目标参数的位置是第二个,如下所示。
PS C:\> help Copy-Item -Parameter Destination -Destination <System.String> Specifies the path to the new location. The default is the current directory. To rename the item being copied, specify a new name in the value of the Destination parameter. Required? false Position? 1 Default value Current directory Accept pipeline input? True (ByPropertyName) Accept wildcard characters? false
所以我们可以直接使用命令,
Copy-Item C:\Temp\10vms.csv 11vms.csv -Verbose
现在为了展开数组值,我们可以将这些值组合到数组中,并且我们可以传递给 Copy-Item 命令。
PS C:\> $items = "C:\Temp\10vms.csv","11vms.csv" PS C:\> Copy-Item @items -Verbose
同样,如果你在第 2 个编号的位置有参数,则可以在逗号后添加它,依此类推。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP