2017-06-29 71 views
0

我想指定我從這裏拿到劇本我的文件路徑:https://gallery.technet.microsoft.com/scriptcenter/Outputs-directory-size-964d07ff試圖指定文件路徑

當前文件路徑指向的目錄,但我無法找到的變量,我需要改變以指定不同的路徑。

# Get-DirStats.ps1 
    # Written by Bill Stewart ([email protected]) 
    # 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 
+0

你是什麼意思「變量,我需要改變,以指定一個不同的路徑」?你不會改變任何東西。您運行腳本並將路徑作爲參數傳遞:'。\ Get-DirStats.ps1 -Path「C:\ some \ folder」'。 –

回答

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" 
0

你所謂的價值取決於你來自哪裏調用它。 此cmdlet的「主要」部分接受幾個參數之一;路徑和literalPath,路徑將優先於文字路徑使用。如果沒有指定,則當前工作目錄將成爲起點。向cmdlet傳遞不同的參數似乎是最簡單的技術。作者的預期用法。

但是... 在第一個函數中,參數綁定在「開始」部分之後...實際路徑是「$ item」。

裏面的Get-DirectoryStats它被稱爲$目錄。

有些地方被稱爲$ _。

關於「範圍」的主題有很多文章。這裏是一個:https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_scopes