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 编辑Get-DirStats.ps1以获取深度(子目录)参数_Powershell_Powershell 2.0 - Fatal编程技术网

Powershell 编辑Get-DirStats.ps1以获取深度(子目录)参数

Powershell 编辑Get-DirStats.ps1以获取深度(子目录)参数,powershell,powershell-2.0,Powershell,Powershell 2.0,由于我是powershell的新手,我需要帮助编辑流行的Get-DirStats.ps1,它采用了一个新的(深度)参数,该参数将定义它将读取和输出统计数据的子目录数量 目前的代码将输出所有子目录,我需要限制的深度 这应该在powershell 2.0中,因为我有一些windows 7机器 ps1可在此处下载: 我从这条线索中学到: 这应该是一个普遍的解决办法: $Depth=2 $Levels=“*”*$Depth 但是将其添加到Get-DirStats.ps1会产生错误 # Written b

由于我是powershell的新手,我需要帮助编辑流行的Get-DirStats.ps1,它采用了一个新的(深度)参数,该参数将定义它将读取和输出统计数据的子目录数量

目前的代码将输出所有子目录,我需要限制的深度

这应该在powershell 2.0中,因为我有一些windows 7机器

ps1可在此处下载:

我从这条线索中学到:

这应该是一个普遍的解决办法: $Depth=2 $Levels=“*”*$Depth

但是将其添加到Get-DirStats.ps1会产生错误

# Written by Bill Stewart 
# Outputs file system directory statistics. 

#requires -version 2 

<# 
.SYNOPSIS 
Outputs file system directory statistics. 

.DESCRIPTION 
Outputs file system directory statistics (number of files and the sum of all file sizes) for one or more directories. 

.PARAMETER Path 
Specifies a path to one or more file system directories. Wildcards are not permitted. The default path is the current directory (.). 

.PARAMETER LiteralPath 
Specifies a path to one or more file system directories. Unlike Path, the value of LiteralPath is used exactly as it is typed. 

.PARAMETER Only 
Outputs statistics for a directory but not any of its subdirectories. 

.PARAMETER Every 
Outputs statistics for every directory in the specified path instead of only the first level of directories. 

.PARAMETER FormatNumbers 
Formats numbers in the output object to include thousands separators. 

.PARAMETER Total 
Outputs a summary object after all other output that sums all statistics. 
#> 

[CmdletBinding(DefaultParameterSetName="Path")] 
param( 
  [parameter(Position=0,Mandatory=$false,ParameterSetName="Path",ValueFromPipeline=$true)] 
    $Path=(get-location).Path, 
  [parameter(Position=0,Mandatory=$true,ParameterSetName="LiteralPath")] 
    [String[]] $LiteralPath, 
    [Switch] $Only, 
    [Switch] $Every, 
    [Switch] $FormatNumbers, 
    [Switch] $Total 
) 

begin { 
  $ParamSetName = $PSCmdlet.ParameterSetName 
  if ( $ParamSetName -eq "Path" ) { 
    $PipelineInput = ( -not $PSBoundParameters.ContainsKey("Path") ) -and ( -not $Path ) 
  } 
  elseif ( $ParamSetName -eq "LiteralPath" ) { 
    $PipelineInput = $false 
  } 

  # Script-level variables used with -Total. 
  [UInt64] $script:totalcount = 0 
  [UInt64] $script:totalbytes = 0 

  # Returns a [System.IO.DirectoryInfo] object if it exists. 
  function Get-Directory { 
    param( $item ) 

    if ( $ParamSetName -eq "Path" ) { 
      if ( Test-Path -Path $item -PathType Container ) { 
        $item = Get-Item -Path $item -Force 
      } 
    } 
    elseif ( $ParamSetName -eq "LiteralPath" ) { 
      if ( Test-Path -LiteralPath $item -PathType Container ) { 
        $item = Get-Item -LiteralPath $item -Force 
      } 
    } 
    if ( $item -and ($item -is [System.IO.DirectoryInfo]) ) { 
      return $item 
    } 
  } 

  # Filter that outputs the custom object with formatted numbers. 
  function Format-Output { 
    process { 
      $_ | Select-Object Path, 
        @{Name="Files"; Expression={"{0:N0}" -f $_.Files}}, 
        @{Name="Size"; Expression={"{0:N0}" -f $_.Size}} 
    } 
  } 

  # Outputs directory statistics for the specified directory. With -recurse, 
  # the function includes files in all subdirectories of the specified 
  # directory. With -format, numbers in the output objects are formatted with 
  # the Format-Output filter. 
  function Get-DirectoryStats { 
    param( $directory, $recurse, $format ) 

    Write-Progress -Activity "Get-DirStats.ps1" -Status "Reading '$($directory.FullName)'" 
    $files = $directory | Get-ChildItem -Force -Recurse:$recurse | Where-Object { -not $_.PSIsContainer } 
    if ( $files ) { 
      Write-Progress -Activity "Get-DirStats.ps1" -Status "Calculating '$($directory.FullName)'" 
      $output = $files | Measure-Object -Sum -Property Length | Select-Object ` 
        @{Name="Path"; Expression={$directory.FullName}}, 
        @{Name="Files"; Expression={$_.Count; $script:totalcount += $_.Count}}, 
        @{Name="Size"; Expression={$_.Sum; $script:totalbytes += $_.Sum}} 
    } 
    else { 
      $output = "" | Select-Object ` 
        @{Name="Path"; Expression={$directory.FullName}}, 
        @{Name="Files"; Expression={0}}, 
        @{Name="Size"; Expression={0}} 
    } 
    if ( -not $format ) { $output } else { $output | Format-Output } 
  } 
} 

process { 
  # Get the item to process, no matter whether the input comes from the 
  # pipeline or not. 
  if ( $PipelineInput ) { 
    $item = $_ 
  } 
  else { 
    if ( $ParamSetName -eq "Path" ) { 
      $item = $Path 
    } 
    elseif ( $ParamSetName -eq "LiteralPath" ) { 
      $item = $LiteralPath 
    } 
  } 

  # Write an error if the item is not a directory in the file system. 
  $directory = Get-Directory -item $item 
  if ( -not $directory ) { 
    Write-Error -Message "Path '$item' is not a directory in the file system." -Category InvalidType 
    return 
  } 

  # Get the statistics for the first-level directory. 
  Get-DirectoryStats -directory $directory -recurse:$false -format:$FormatNumbers 
  # -Only means no further processing past the first-level directory. 
  if ( $Only ) { return } 

  # Get the subdirectories of the first-level directory and get the statistics 
  # for each of them. 
  $directory | Get-ChildItem -Force -Recurse:$Every | 
    Where-Object { $_.PSIsContainer } | ForEach-Object { 
      Get-DirectoryStats -directory $_ -recurse:(-not $Every) -format:$FormatNumbers 
    } 
} 

end { 
  # If -Total specified, output summary object. 
  if ( $Total ) { 
    $output = "" | Select-Object ` 
      @{Name="Path"; Expression={"<Total>"}}, 
      @{Name="Files"; Expression={$script:totalcount}}, 
      @{Name="Size"; Expression={$script:totalbytes}} 
    if ( -not $FormatNumbers ) { $output } else { $output | Format-Output } 
  } 
}
比尔·斯图尔特写的 #输出文件系统目录统计信息。 #需要-版本2 [CmdletBinding(DefaultParameterSetName=“Path”)] 参数( [参数(位置=0,必需=$false,参数setName=“Path”,ValueFromPipeline=$true)] $Path=(获取位置).Path, [参数(位置=0,必需=$true,参数setName=“LiteralPath”)] [String[]$LiteralPath, [开关]仅限美元, [切换]$Every, [开关]$FormatNumber, [开关]$总计 ) 开始{ $ParamSetName=$PSCmdlet.ParameterSetName if($ParamSetName-eq“Path”){ $PipelineInput=(-not$PSBoundParameters.ContainsKey(“路径”))-和(-not$Path) } elseif($ParamSetName-eq“LiteralPath”){ $PipelineInput=$false } #与-Total一起使用的脚本级变量。 [UInt64]$script:totalcount=0 [UInt64]$script:totalbytes=0 #返回[System.IO.DirectoryInfo]对象(如果存在)。 函数获取目录{ 参数(项目) if($ParamSetName-eq“Path”){ if(测试路径-路径$item-路径类型容器){ $item=Get item-Path$item-Force } } elseif($ParamSetName-eq“LiteralPath”){ if(测试路径-LiteralPath$项-路径类型容器){ $item=Get item-LiteralPath$item-Force } } 如果($item-和($item-是[System.IO.DirectoryInfo]){ 退货$item } } #输出带有格式化数字的自定义对象的筛选器。 函数格式输出{ 进程{ $124;选择对象路径, @{Name=“Files”;Expression={{0:N0}“-f$\.Files}, @{Name=“Size”表达式={{0:N0}“-f$\u0.Size} } } #输出指定目录的目录统计信息。使用-recurse, #该函数包括指定对象的所有子目录中的文件 #directory.With-format,输出对象中的数字的格式为 #格式输出过滤器。 函数Get DirectoryStats{ 参数($directory,$recurse,$format) 写入进度-活动“Get DirStats.ps1”-状态“正在读取“$($directory.FullName)” $files=$directory | Get ChildItem-Force-Recurse:$Recurse | Where Object{-not$\ psicontainer} 如果($文件){ 写入进度-活动“Get DirStats.ps1”-状态“正在计算“$($directory.FullName)” $output=$files |测量对象-总和-属性长度|选择对象` @{Name=“Path”表达式={$directory.FullName}, @{Name=“Files”表达式={$\.Count;$script:totalcount+=$\.Count}, @{Name=“Size”表达式={$\.Sum;$script:totalbytes+=$\.Sum} } 否则{ $output=”“|选择对象` @{Name=“Path”表达式={$directory.FullName}, @{Name=“Files”;表达式={0}, @{Name=“Size”表达式={0} } if(-not$format){$output}else{$output | format output} } } 进程{ #获取要处理的项,无论输入是否来自 #管道与否。 如果($PipelineInput){ $item=$\u } 否则{ if($ParamSetName-eq“Path”){ $item=$Path } elseif($ParamSetName-eq“LiteralPath”){ $item=$LiteralPath } } #如果项目不是文件系统中的目录,请写入错误。 $directory=Get directory-item$item 如果(-not$directory){ 写入错误-消息“路径“$item”不是文件系统中的目录。”-类别InvalidType 返回 } #获取第一级目录的统计信息。 获取DirectoryStats-directory$directory-recurse:$false-format:$formatNumber #-仅表示没有经过第一级目录的进一步处理。 如果($Only){return} #获取第一级目录的子目录并获取统计信息 #对他们每个人来说。 $directory | Get ChildItem-Force-Recurse:$Every | 其中对象{$\ PSIsContainer}ForEach对象{ 获取DirectoryStats-directory$\递归:(-not$Every)-format:$formatNumber } } 结束{ #如果指定了-Total,则输出摘要对象。 若有{ $output=”“|选择对象` @{Name=“Path”表达式={”“}, @{Name=“Files”;表达式={$script:totalcount}, @{Name=“Size”;表达式={$script:totalbytes} if(-not$FormatNumbers){$output}else{$output | Format output} } } PS C:\Users\user\Desktop>\Get-DirStats.ps1“C:\Program Files”2
其中2是深度

PowerShell v2.0不支持来自
Get ChildItem
的参数
-depth
。是的,我知道,出于这个原因,我尝试使用以下命令实现它:$Levels=“*”*$Depth,其中$Depth是用户提供的int参数首先,您必须添加一个新的脚本输入参数
Depth
,然后
进程
块中的部分需要进行一些修改。您必须编写您的WIN locig()。在此处添加了深度参数:
param([parameter(Position=…]$Path=(get location)。Path,[parameter(Position=0,必选…][String[]]$LiteralPath,[int[]$Depth,…)$Levels=“\*”*$Depth
并将值分配给$Levels,问题在于将路径与Levels进程`process{if($PipelineInput){$item=$\else{i]连接起来