Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I've just started in the world of Powershell and I'm writing a simple script to put hosts in maintenance mode, apply patches, reboot, then put back into server. I decided to use the failover clustering service to check the host's availability before placing back into service. But I seem to be getting an error which I can't find much about.

Get-ClusterNode : Cannot convert 'hyperv1.example.com' to the type     'System.Collections.Specialized.StringCollection' required by parameter 'Name'. Unable to cast object of type 'System.Management.Automation.PSObject' to type 'System.String'.

The Code I have is:

$VMMServer = Get-VMMServer -ComputerName "scvmm.example.com"
$Cluster = "HyperVCluster"
$VMHosts = Get-VMHost -VMHostCluster $Cluster
Foreach ($VMHost in $VMHosts) {
psexec \\$VMHost -s wuinstall /reboot /install
sleep 120
$Status = Get-Clusternode -Cluster $Cluster -name $VMHost | select state
function CheckHostStatus {
if ($Status -match "Up") {
Write-Host "Host is Up"
Write-Host "Placing host" $VMHost "back into service."
    Enable-VMHost $VMHost
                        }
elseif ($Status -match "Down") {
Write-Host "Host is still rebooting"
sleep 120
CheckHostStatus
}
                    }
CheckHostStatus
}
share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

Get-VMHost returns "VMHost[]" as a type.

So each VMHost returned is a custom, structured type, not a simple string.

My understanding is that the VM-Host structure is as such (I don't have the PS VM add-ons installed here to double check):

  • State
  • CustomFields
  • ID
  • Name

So perhaps try using $VMHost.Name when you just want the (string) name of the current VMHost:

$Status = Get-Clusternode -Cluster $Cluster -name $VMHost.Name | select state

Write-Host "Placing host " + $VMHost.Name + " back into service."

share|improve this answer
1  
Thank you very much, this solved my problem. I'm still confused as to why all the other times I reference $VMHost have no issue. –  user96596 Sep 4 '11 at 20:29
    
Glad it helped! :) As to your (understandable) confusion: If it knows for sure you want a string (ie: in a Write-Host) it pulls the name as the generic 'string' representation of the object. But if the required type is unclear (which often happens when using Vars in parameters) it returns it as the whole (structured) object, so you have to specifically tell it you want the Name property. –  techie007 Sep 4 '11 at 20:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.