if I run the loop, an it never end.... I was waiting about 10 min and nothing happend. VM was restartet less than a minute. Am I doing something wrong?
Restart-VMGuest -VM $vm -Confirm:$false | Out-Null
while ($vm.PowerState -ne 'PoweredOff')
$vm = Get-VM -Name $vm.Name
while (-not $vm.ExtensionData.Guest.GuestOperationsReady)
No, that looks ok.
Try to determine in which loop it gets stuck.
Preferably with a debugger, but you can also add some Write-Host lines in the code.