Can you check if by replacing the following lines it behaves differently
echo y | .\plink.exe -ssh "root@$($vmhost.Name)"-pw $password hostname
$hostdetails= .\plink.exe -ssh "root@$($vmhost.Name)"-pw $password-m .\cmd.txt
Can you check if by replacing the following lines it behaves differently
$hostdetails= .\plink.exe -ssh "root@$($vmhost.Name)"-pw $password-m .\cmd.txt