3LCourseAZ040.psm1

<#PSScriptInfo
 
.VERSION 1.0.2
 
.GUID f274d4e3-027e-487c-8d4c-6a86bf3a6808
 
.AUTHOR F. van Drie (3-link.nl)
 
.COMPANYNAME 3-Link Opleidingen (3-link.nl)
 
.COPYRIGHT 3-Link bv (NL)
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>


<#
 
.DESCRIPTION
 This script is developed for training purposes only.
 
#>
 

Param()

Function GetFolderInfo {

    <#
    .SYNOPSIS
        Gets the number of files and total storage for a specified folder.
 
    .DESCRIPTION
        Gets the number of files and total storage for a specified folder on the local or remote computer.
        If no computer is specified, the local computer is queried
 
    .EXAMPLE
        'SVR1', 'SVR2' | GetFolderInfo -path 'C:\Data'
 
        Folder info is retrieved from 2 computers
            Names are presented through the pipeline
 
    .EXAMPLE
        GetFolderInfo -path 'C:\data' -recurse -computerName 'SVR1', 'SVR2'
 
        Folder info is retrieved from 2 computers as presented via the ComputerName parameter
            -Recurse directs the function to process underlying subdirectories
 
 
    .NOTES
        Version 2021-10-09
        Author Frits van Drie (3-link.nl)
 
    #>


    [CmdletBinding()]
    
    param(
        [parameter(mandatory=$false)]
        $path = "$env:USERPROFILE\documents",

        [parameter(mandatory=$false)]
        [switch]$recurse = $false,

        [parameter(mandatory=$false,ValueFromPipeline=$true)]
        [string[]]$computerName = 'localhost'

    )

    Begin {
        Write-Verbose "Start Function"

        if ( $PSBoundParameters.Keys -contains 'Verbose' ) {
            $verboseAction = 'Continue'
        }
        else {
            $verboseAction = $VerbosePreference
        }

    }

    Process {

        foreach ($deviceName in $computerName) {

            Write-Verbose "Connecting to $deviceName"

            $objReturn = Invoke-Command -ComputerName $deviceName {

                $VerbosePreference = $using:verboseAction

                $path    = $using:path
                $recurse = $using:recurse


                Write-Verbose "Connected to $env:COMPUTERNAME"
                Write-Verbose "Path : $path"
                Write-Verbose "Recurse: $recurse"


                if ( -not (Test-Path $path) ) {
                    Write-Error "$path is not a valid path"
                    break
                }
                    Write-Verbose "$path exists" 

                if ( (Get-Item $path).psIsContainer -ne $true ){
                    Write-Warning "$path is not a valid folder"
                    break
                }
                    Write-Verbose "$path is a container"


                # Collect Files
                try {
                    Write-Verbose "Retrieving files in $path"
                    $fileList = Get-ChildItem -Path $path -Recurse:$recurse -File -ErrorAction stop
                    Write-Verbose "Success"

                    Write-Verbose "Measuring files"
                    $objReturn = $fileList | Measure-Object -Property Length -Sum
                }
                catch {
                    Write-Warning "$path is not a valid path"
                    break
                }

                return $objReturn

            } # end invoke

            Write-Output $objReturn

        }  # end Foreach

        

    } # end Process

    End {
        Write-Verbose "End Function"
    }


}