We run a powershell script that queries our workstations and if they've been running too long we bounce them using powercli restart-vm command. Up until recently this has been working fine.
Here is the snip of code we run
Restart-VM -VM $arrProblemComputers -confirm:$false
$arrProblemComputers is the array of boxes that need to be bounced. Again this worked great for a while then we noticed that boxes were not being restarted anymore. Looking into the issue I see we started getting this error:
Any ideas?
It looks as if you passed a name of a non-existing VM in that array.
Does this return anything
Get-VM -Name LSISNA01V015013
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I thought that at first too but the VM does exist. Get-VM comes back successful
Is it possible that the variable $arrProblemComputers is set to something else in the session? I know I came across a similar error today when I was using the variable in two places in the script. I changed my script and used Clear-Variable to make sure there was no value assigned for that session. Just a thought.
Example:
Clear-Variable arrProblemComputers
I make the array null at the beginning of the script
Would you mind posting the script you are using ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I can but I would like to get this part working. Can I post code here?
Sure, you can post it inline or as an attachment.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ok here it is.
$intUpDays is how many days a VDI machine needs to be on for it to get added to the reboot list.
$intBoxestoBounce is how many VDI machines will be bounced when the script runs. We did't want to restart too many at once so we do about 160 a night.
$strComputerDescription is how we find our VDI machines in AD. All VDI machines that get created in our environment have the descrption of VMWare View. You can change that if you want.
VCServer/VCUser/VCPassword should be self explanitory. Also, starting at line 36 you need to modify for your own SMTP needs.
The gist of the script is like this, we search AD for all computer objects that have VMWare View in the description. We then use WMI to get an uptime of those boxes. If the uptime is more than $intUpDays the vdi machine gets added to a new array. After getting uptimes we determine how many boxes need to be bounced and if it is over $intBoxestoBouce we select a random sample from that array and create a new array with that is equal to $intBoxestoBounce. We then send an OS level restart command and wait. After a 5 minute wait we query the boxes again and make sure the uptime is now less than a day. If it isn't we assume the OS has hung and we try to send a VC level restart-vm command. This is where is breaks. Line# 79 should bounce the new array but it doesn't.
For the time being we keep sending the OS restart command. It will loop through this process five times total before giving up and sending an email to administrators with a list of boxes that didn't restart.
Let me know if there are any questions.
That script looks ok, no obvious reason why there should be an error.
I'm still convinced that the error comes because the Object By Name (OBN) mechanism can not convert one of the names in the array to a VM.
Can you try changing the line to
Restart-VM -VM $arrProblemComputers -confirm:$false -ErrorAction SIlentlyContinue
That way a faulty entry in the array will at least not stop the script from handling the remaining VMs in the array.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Similar, but Get-VM | Shutdown-VMGuest will do what you're looking to do