For some reason when I try to use the $scanpath variable after the Get-ChildItem in the below code, it doesnt work. But if I put the actual path in place of $scanpath, it works. What am I doing wrong? Both the $computer and $savepath variables work fine.
$computer = 'Server'
$scanpath = 'P$\Directory\Directory\Z'
$savepath = 'C:\Z-Media.csv'
Invoke-Command -ComputerName $computer -scriptblock {Get-ChildItem $scanpath -recurse -include *.mp3,*.wma,*.wmv,*.mov,*.mpg,*.ogg,*.jpg -force | select FullName, Length | Sort-Object { [long]$_.Length } -descending} | Export-Csv $savepath -NoTypeInformation