Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Powershell迭代期间跳过驱动器_Powershell - Fatal编程技术网

在Powershell迭代期间跳过驱动器

在Powershell迭代期间跳过驱动器,powershell,Powershell,我目前正在列出计算机中的所有驱动器列表。但是,我想从输出中跳过C:\驱动器: Name Used (GB) Free (GB) Provider Root --------- --------- -------- ----

我目前正在列出计算机中的所有驱动器列表。但是,我想从输出中跳过C:\驱动器:

Name           Used (GB)     Free (GB) Provider      Root                                                                                      ---------     --------- --------      ----                                                                           ---------------
C                 150.62        688.78 FileSystem    C:\                                                                                           
I                   2.42         65.94 FileSystem    I:\                                                                                           
F                   0.09          2.96 FileSystem    F:\                                                                                           
G                   0.05         16.43 FileSystem    G:\                                                                                           
D                   0.05         15.67 FileSystem    D:\                                                                                           
H                   0.03          9.73 FileSystem    H:\                                                                                           
这是我的代码:

# Iterates over all drives found
$SortedDrives = $Drives | Sort-Object -Property Used -Descending

foreach ($Drive in $SortedDrives) {
    if ($Drive.Name -eq "C") {
        continue
    }

    Write-Output $Drive.Root
    $newSortedDrives = $newSortedDrives += $Drive.Root
}
这是输出:

Name           Used (GB)     Free (GB) Provider      Root                                                                                      ---------     --------- --------      ----                                                                           ---------------
C                 150.62        688.78 FileSystem    C:\                                                                                           
I                   2.42         65.94 FileSystem    I:\                                                                                           
F                   0.09          2.96 FileSystem    F:\                                                                                           
G                   0.05         16.43 FileSystem    G:\                                                                                           
D                   0.05         15.67 FileSystem    D:\                                                                                           
H                   0.03          9.73 FileSystem    H:\                                                                                           
理想情况下,我希望跳过C驱动器,只显示驱动器I F G D H

我做错了什么?看起来像这样的说明:

if ($Drive.Name -eq "C") {
    continue
}

没有执行它应该执行的操作。

除了在循环之前缺少初始化$newsortedAddress=@之外,问题中的代码现在工作正常,正如预期的那样,因为您后来编辑了问题以修复原始问题,该问题使用了错误的变量作为foreach循环中的枚举源

PSv3+是一种更简洁、PowerShell惯用的代码形式:

# Collects just the .Root property values; omit the (...).Root to get drive objects.
[array] $newSortedDrives = (Get-PSDrive -PSProvider FileSystem | 
                              Where-Object Name -ne C | 
                                Sort-Object -Property Used -Descending).Root
这避免了输出数组$newsortAddress的低效迭代构造,因为使用+=附加到数组实际上会创建一个新数组,并在幕后附加新元素

请注意,…Root如何用于提供通过访问输入集合的各个元素上的.Root属性值而自动收集的.Root属性值数组,该特性称为

它还表明,通过cmdlet过滤集合自然适合PowerShell的管道

在表达式领域,内存中已经收集到的东西也有更快的PSv4+;e、 g: