I had a piece of code which deletes Google Chrome cache for all user profiles on remote machine.
To achieve this I had function GetMachineUserProfiles which returns ArrayList of all user profiles on remote machine. In other function I need to run Invoke-Command and loop through all user profiles given with $ListOfUserProfiles and delete Chrome cache for each profile.
But I run into a problem, $ListOfUserProfiles is empty/null inside my Invoke-Command. I tried several solutions but failed each time. My last try is shown in example:
$ListOfUserProfiles = GetMachineUserProfiles
$ListOfUserProfiles.count
Function Delete-Chrome-Temp-Files {
WriteLog "--------------------------------`n"
WriteLog "COMMAND: Delete Chrome temporary files"
$diskSpaceBeforeC = Disk-Free-Space
$ListOfUserProfiles.count
Invoke-Command -ComputerName $machine -ArgumentList (, $ListOfUserProfiles) -ScriptBlock {
$ListOfUserProfiles.count
foreach ($UserProfile in $ListOfUserProfiles){
Write-Host $UserProfile
Get-ChildItem -Path "C:\Users\"$UserProfile"\AppData\Local\Google\Chrome\User Data" -Filter "*.tmp" | foreach {
Remove-Item -Path $_.FullName
WriteLog "INFO: Deleting $($_.FullName)"
}
}
}
Delete-Chrome-Temp-Files
There are 6 profiles on my machine, and you can see I used count method 3 times here, and they return:
6
6
0 (I expect 6 here)