When you’re using the PowerShell module for Hyper-V from James O’Neill (a.k.a. jamesone) then you’ll find out that many cmdlets don’t accept string collections as variable. Here’s an example:

Import-Module c:\psmodules\HyperV\HyperV.psd1
$Hosts = "HV01,HV02,HV03,HV04"
Stop-VM -VM VM03 -Server $HostsString -Force

Throws this error:
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At c:\psmodules\HyperV\VM.ps1:73 char:26
+             Get-WmiObject <<<<  -computername $Server -NameSpace $HyperVNamespace -Query $WQL | Add-Member -MemberType ALIASPROPERTY -Name “VMElementName” -Value “ElementName” -PassThru
+ CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

The module doesn’t interpret the $HostsString as a collection.

Here’s a possible solution:

Stop-VM -VM VM03 -Server $HostsString.split(",") –Force

What I’ve done is include a split-method to split the string and return it as a collection. Now the command works!


Leave a Reply