VMware Cloud Community
BHeidemann
Contributor
Contributor

Restart-VM Cannot bind parameter

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:

Restart-VM : Cannot bind parameter 'VM'. Cannot convert the "LSISNA01V015013" v
alue of type "System.String" to type "VMware.VimAutomation.ViCore.Types.V1.Inve
ntory.VirtualMachine".
At C:\LSITools\BounceVDIMachines.v2.ps1:65 char:23
+         Restart-VM -VM <<<<  $arrProblemComputers -confirm:$false
    + CategoryInfo          : InvalidArgument: (:) [Restart-VM], ParameterBind
   ingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomat
   ion.ViCore.Cmdlets.Commands.RestartVM

Any ideas?

0 Kudos
10 Replies
LucD
Leadership
Leadership

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

0 Kudos
BHeidemann
Contributor
Contributor

I thought that at first too but the VM does exist. Get-VM comes back successful

0 Kudos
aerodevil
Hot Shot
Hot Shot

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

Josh Atwell @Josh_Atwell http://www.vtesseract.com http://github.com/joshatwell/
0 Kudos
BHeidemann
Contributor
Contributor

I make the array null at the beginning of the script

0 Kudos
LucD
Leadership
Leadership

Would you mind posting the script you are using ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
BHeidemann
Contributor
Contributor

I can but I would like to get this part working. Can I post code here?

0 Kudos
LucD
Leadership
Leadership

Sure, you can post it inline or as an attachment.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
BHeidemann
Contributor
Contributor

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.

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
dgauthier99
Contributor
Contributor

Similar, but  Get-VM | Shutdown-VMGuest will do what you're looking to do

0 Kudos