Created
July 21, 2019 09:23
-
-
Save Agazoth/e9495df64cc6cb178829fd16724dbcc4 to your computer and use it in GitHub Desktop.
Iron Scripter july, 19 2019 - Advanced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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