I have a simple PS script that counts files in 2 folders on remote machines and then exports to a CSV.
These are the lines that make it happen (note: all of the variables like $server, $folder1path, $folder2path are defined and legit)
invoke-command -computername $servers -scriptblock {
$servername = hostname
$infoObject = New-Object PSObject
$folder1count=(Get-ChildItem -recurse -path $folder1path).count
$folder2count =(Get-ChildItem -recurse -path $folder2path).count
if ($folder1count -ne 0 -or $folder2count -ne 0) {
Add-Member -inputObject $infoObject -memberType NoteProperty -name "ServerName" -value $Servername
Add-Member -inputObject $infoObject -memberType NoteProperty -name "Folder1" -value $folder1count
Add-Member -inputObject $infoObject -memberType NoteProperty -name "Folder2" -value $folder2count
$infoObject}} | Select-Object * -ExcludeProperty PSComputerName, RunspaceId, PSShowComputerName | Export-Csv -path "C:\temp\Test_count_folder.csv" -NoTypeInformation
If I run the (Get-ChildItem -recurse -path $folder1path).count
cmd on the local machine it returns the correct value.
The problem is that the scriptblock section consistently returns incorrect values ( 28 & 38?) even when the folders are empty.
What am I missing? It doesn't throw an error but it does output incorrect values.