尝试指定文件路径

时间:2017-06-29 14:15:18

标签: powershell

我试图在我从这里获得的脚本中指定我的文件路径:https://gallery.technet.microsoft.com/scriptcenter/Outputs-directory-size-964d07ff

当前文件路径指向目录,但我无法找到需要更改的变量以指定其他路径。

  # Get-DirStats.ps1
  # Written by Bill Stewart (bstewart@iname.com)
  # 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 }
    }
  }

... the rest of the code did not seem relevant 

2 个答案:

答案 0 :(得分:1)

您可以在调用脚本时指定$Path变量,也可以添加覆盖默认值的行。我已经突出显示了它的位置。

[CmdletBinding(DefaultParameterSetName="Path")]
 param( 

 [parameter(Position=0,Mandatory=$false,ParameterSetName="Path",ValueFromPipeline =$true)]  

  $Path=(get-location).Path, ################ PATH IS SET HERE ##############
  [parameter(Position=0,Mandatory=$true,ParameterSetName="LiteralPath")]
  [String[]] $LiteralPath,
  [Switch] $Only,

调用脚本时:

C:>.\myscript.ps1 -Path "c:\temp"

答案 1 :(得分:0)

你所谓的价值取决于你从哪里调用它。 &#34;主要&#34;此cmdlet的一部分接受一对参数之一; path和literalPath,path将优先于文字路径使用。如果两者都未指定,则当前工作目录将成为起点。将不同的参数传递给cmdlet似乎是最简单的技术。作者的预期用法。

... BUT 在第一个函数中,参数绑定在&#34;开始&#34; section ...实际路径是&#34; $ item&#34;。

在Get-DirectoryStats中,它被称为$ directory。

有些地方被称为$ _。

关于&#34;范围&#34;的主题有很多文章。这是一个:enter image description here