I'm trying to sort my list but it's not sorting as expected. Why is this?
Invoke-Command -ComputerName $computers {
$rscontent = Get-Item C:\temp\file1.txt | Sort-Object LastWriteTime -Descending
"File1.txt last updated: $($rscontent.LastWriteTime) on server $(hostname)"
}
The sorting has to be performed after you have all the data, not before:
Invoke-Command -ComputerName $computers { Get-Item C:\temp\file1.txt } |
Sort-Object LastWriteTime -Descending |
ForEach-Object { "File1.txt last updated: $($_.LastWriteTime) on server $($_.PSComputerName)" }
Notable mention, Invoke-Command
automatically wraps all output in psobject
instances and attaches a .PSComputerName
property to it, so as shown above, there is no need to invoke hostname
in your scriptblock. See about Remote Output for details.