Hi Gang,
I’m attempting to get a list of files on some of our file servers that are newer than a given date range. I’ve been through a couple different methodologies of gathering the objects, but ultimately, running the script block in an invoke-command proved substantially faster.
My question is, once I invoke the command on the remote machine, I see it running through the remote systems task manager just fine. But then, I want to return those objects I’ve stored in the invoke-command variable back to the local system and run a foreach loop on it locally. I’m getting hung up, and my output isn’t being returned to the host I’m running the script from and going to excel like planned.
Invoke-Command -Session $sessionFS13 {$colFS13Files= Get-ChildItem d:\ -Recurse | where {$_.psiscontainer -eq $false -and $_.CreationTime -gt $date} | Select-Object fullname,length,creationtime}
Invoke-Command -Session $sessionFS13 {$colFS13Files}
foreach ($file in $colFS13Files) {
$filepath = $file.FullName
$excelSheet4.Cells.Item($intRow,1) = $filepath
$size = $file.Length
$excelSheet4.Cells.Item($intRow,2) = "{0:N2}" -f ($size / 1MB)
$creationTime = $file.CreationTime
$excelSheet4.Cells.Item($intRow,3) = $creationTime
$owner = ((Get-Acl $filepath).owner) #$filepath | Get-Acl | select owner
$excelSheet4.Cells.Item($intRow,4) = $owner
$intRow ++
}#foreach
# add up length column (column B)
$range = $excelSheet4.usedRange
$row = $range.rows.count # Takes you to the last used row
$Sumrow = $row + 1 #last used row + 1
$r = $excelSheet4.Range("B2:B$row") # select the column to Add up
$functions = $appExcel.WorkSheetfunction #setup variable for excel to run a built-in function (next line)
$excelSheet4.cells.item($Sumrow,2) = $functions.sum($r) # this uses the Excel sum function defined above
$rangeString = $r.address().tostring() -replace "\$",'' # convert formula to Text
$excelSheet4.cells.item($Sumrow,1) = "Total Size (MB)" # Print string "Total Size" in column A & last row + 1
$excelSheet4.cells.item($Sumrow,2).Select() #Print calculation
$excelSheet4.range("a${Sumrow}:b$Sumrow").font.bold = "true" # makes text in last row bold
$excelSheet4.range("a${Sumrow}:b$Sumrow").font.size=12 # Changes the font size in last row to 12 points
Thanks!
Karson