Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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_Recursion - Fatal编程技术网

Powershell,检查所有驱动器

Powershell,检查所有驱动器,powershell,recursion,Powershell,Recursion,我是Powershell的新手,我尝试在网上找到解决问题的方法,但似乎找不到。基本上,我需要写一些东西,让powershell查看所有驱动器和目录,以找到以下内容: 文件总数(不包括文件夹)、最大文件大小、平均文件大小、总文件大小 以下是我到目前为止所写的内容: $source = "get-psdrive" #attempt at looking through all drives/directories which didn't work foreach($a in $source) {

我是Powershell的新手,我尝试在网上找到解决问题的方法,但似乎找不到。基本上,我需要写一些东西,让powershell查看所有驱动器和目录,以找到以下内容:

文件总数(不包括文件夹)、最大文件大小、平均文件大小、总文件大小

以下是我到目前为止所写的内容:

$source = "get-psdrive"

#attempt at looking through all drives/directories which didn't work
foreach($a in $source) {

    #counts files in given directory
    Get-ChildItem -Recurse -File -ErrorAction SilentlyContinue -Force |
        Measure-Object |
        ForEach-Object { $_.Count }

    #select largest file in given directory
    Get-ChildItem -Recurse -File -ErrorAction SilentlyContinue -Force |
        Sort-Object Length -Descending |
        Select-Object -First 1

    #get average file size in a given directory
    Get-ChildItem -Recurse -File -ErrorAction SilentlyContinue -Force |
        Measure-Object -Property Length -Average

    #sum of file sizes in given directory
    (Get-ChildItem -Recurse -File -ErrorAction SilentlyContinue -Force |
        Measure-Object -Sum Length).Sum
}
问题是它只在C驱动器中显示。我不知道是否有办法检查我的电脑而不是特定的驱动器,但我似乎找不到办法。非常感谢您的帮助。

使用
“…”
您定义了一个字符串,因此在您的示例中,您尝试在字符串上循环

试着这样做:

$Drives = Get-PSDrive -PSProvider 'FileSystem'

foreach($Drive in $drives) {

    #counts files in given directory
    Get-ChildItem -Path $Drive.Root -Recurse -File -ErrorAction SilentlyContinue -Force |
        Measure-Object |
        ForEach-Object { $_.Count }

    #select largest file in given directory
    Get-ChildItem -Path $Drive.Root -Recurse -File -ErrorAction SilentlyContinue -Force |
        Sort-Object Length -Descending |
        Select-Object -First 1

    #get average file size in a given directory
    Get-ChildItem -Path $Drive.Root -Recurse -File -ErrorAction SilentlyContinue -Force |
        Measure-Object -Property Length -Average

    #sum of file sizes in given directory 
    (Get-ChildItem -Path $Drive.Root -Recurse -File -ErrorAction SilentlyContinue -Force |
        Measure-Object -Sum Length).Sum

}
注意!我没有更改您的
Get ChildItem
code(它将搜索的路径除外)。我只写了
foreach
循环。

请。
foreach($source中的a)
->
foreach($source中的a)
;要执行名称存储在字符串中的命令,必须使用呼叫操作员
&
。另外,您的
Get ChildItem
命令实际上并不引用手头的驱动器
$a