locked
Collecting files folder information from multiple shares RRS feed

  • Question

  • Hi, 

    I have a requirment to collect folder and files information from the multiple shares.

    I found this script which is working fine, according to my meet, meanwhile i need to do some update in this script

    like , i need to add multiple shares path in this script.

    right now i am running this script like : PS1 -path c:\test01 so it takes information and write in the shell

    how to add this path in scrit itseld along with other path like c:\test02 c:\test03

    here is the script

    [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="SubFolders"; Expression={"{0:N0}" -f $R}},
            @{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 }

        $R=($Directory | Get-ChildItem -Recurse -Force | Where-Object {$_.PSIsContainer}).Count

        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="SubFolders"; Expression={$_.Count; $script:totalcount += $_.Count}},
            @{Name="Size"; Expression={$_.Sum/1024/1024/1024; $script:totalbytes += $_.Sum}}
        }
        else {
          $output = "" | Select-Object `
            @{Name="Path"; Expression={$directory.FullName}},
            @{Name="Files"; Expression={0}},
            @{Name="SubFolders"; 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="SubFolders"; Expression={$script:totalcount}},
          @{Name="Size"; Expression={$script:totalbytes/1024/1024/1024}}
        if ( -not $FormatNumbers ) { $output } else { $output | Format-Output }

      }
    }

    Thanks in advance.


    Ashish Gaur

    Monday, December 9, 2013 4:08 AM

Answers