Skip to content

Instantly share code, notes, and snippets.

@Agazoth
Created Jul 21, 2019
Embed
What would you like to do?
Iron Scripter july, 19 2019 - Advanced
function Get-FileInfo {
[CmdletBinding(DefaultParameterSetName='Local')]
param (
# The Path parameter takes an array of strings to query. Pipe away if desired
[Parameter(ValueFromPipeline=$true)]
[Parameter(ParameterSetName = 'Local')]
[Parameter(ParameterSetName = 'Remote')]
[string[]]
$Path,
# The ComputerName parameter takes several computernames and executes the Get-FileInfo command against them
[Parameter(ParameterSetName = 'Remote')]
[string[]]
$ComputerName,
# The Credential parameter takes a credential object if required to execute remote commands against the computers specified in the ComputerName parameter
[PSCredential]
[Parameter(ParameterSetName = 'Remote')]
$Credential,
# The AsJob switch runs the command as a job on the remote machine
[Parameter(ParameterSetName = 'Remote')]
[switch]
$AsJob,
# The JobName parameter lets you specify a name for the job
[Parameter(ParameterSetName = 'Remote')]
[string]
$JobName
)
begin {
$s = [scriptblock]::Create('param ($path) foreach ($p in $Path){
if (Test-Path $p){
try {
Get-ChildItem $p -Recurse -Force |
measure-object length -sum -Average -ErrorAction Stop |
select-object Count,Sum,Average,@{n="Date";e={Get-date}},@{n="ComputerName";e={hostname}},@{n="Path";e={$p}}
} catch {
Write-Verbose "No files in $p"
}
} else {
Write-Warning "$p does not exist!"
}
}')
}
process {
$paramHash = @{
ScriptBlock = $s
ArgumentList = $Path
}
if ($AsJob){$paramHash.Add('AsJob',$True)}
if ($JobName -and $AsJob){$paramHash.Add('JobName',$JobName)}
if ($ComputerName){$paramHash.Add('ComputerName',$ComputerName)}
if ($Credential){$paramHash.Add('Credential',$Credential)}
Invoke-Command @paramHash
}
end {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment