So I am writing a powercli scrip that will take a cluster name and with a filter enumerate all the datastores on the cluster that are on our SAN. It will then snap the VMs so that I can issue a SAN snap. I am rather new to powershell so I realize this may not be the cleanest of scripts, but the main issue I am having is with this line:
$datastorelist = Get-Datastore -Name $arrayname -VMHost $nodelist
Any help would be greatly appreciated, thanks!
If I write the $nodelist to the screen it is formatted correctly and if copy the contents of $nodelist and hard code it into the parameter it works fine, but passing the string creates the following error:
Get-Datastore : 5/13/2013 8:26:29 AM Get-Datastore Could not find DatastoreRelatedObjectBase with name 'vh110
1119.mydomain.com,vhost20.mydomain.com,vhost18.mydomain.com,vhost02.mydomain.com,vhost03.mydomain.com,vhost04
.mydomain.com,vhost05.mydomain.com'.
At E:\Program Files\3par scripts\vmsnaps.ps1:64 char:32
+ $datastorelist = Get-Datastore <<<< -Name $arrayname -VMHost $hostlist
+ CategoryInfo : ObjectNotFound: (vhost19.mydom...05.mydomain.com:String) [Get-Datastore], VimException
+ FullyQualifiedErrorId : Core_ObnSelector_SelectObjectByNameCore_ObjectNotFound,VMware.VimAutomation.ViCore.Cmdle
ts.Commands.GetDatastore
The scrip defines some variables and loads them, then calls FindNode then QueryDatastores
Function FindNodes
#This function will take cluster and enumerate it to the hosts wthin it and add them to a comma delimited string
{
Write-Host
Write-Host Enumerating cluster nodes in $cluster cluster....
#$nodes = Get-VMHost -Location $cluster
$nodes = Get-Cluster $cluster | Get-VMHost
Write-Host Found $nodes.Length nodes in cluster
foreach ($i in $nodes) {
$i.name
$nodelist = $nodelist +","+$i.name
$script:nodelist = $nodelist
}
$script:nodelist = $nodelist.substring(1)
}
Function QueryDatastores
# This function will enumerate the datastores on the given cluster that start with the $arrayname variable.
{
Write-Host
Write-Host Enumerating datastores on $cluster cluster, please wait.....
Write-Host $nodelist
$datastorelist = Get-Datastore -Name $arrayname -VMHost $nodelist
Write-Host Found $datastorelist.Length datastores in $cluster cluster
foreach ($i in $datastorelist) {
$i.name
}
}
Clear-Host
Write-Host
Write-Host Loading Powercli....
Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
Write-Host Connecting to $vcenter "please wait...."
connect-viserver -server $vcenter
FindNodes
QueryDatastores
What exactly do you have in $arrayname ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
$arranyname has the string of the datastore names we are filtering, our datastore names start with the arrarty name so the varibale contains "dsarray*" so that any datastores not ont he sane will get filtered out. The errors seems to be with the $nodelist variable. I suspect it has to do witht he fact that the string variable has commas in it, but I hav etied putting ' infront of the commas when formatting the string variable. The error looks like it is gettingt the sting passed to it correctly.
No, I don't think that is a problem. The VMHost parameter accepts an array, and commas separate the elements of an array.
It could be that there is a problem with OBN.
Can you try like this
$datastorelist = Get-Datastore -Name $arrayname -VMHost (Get-VMHost -Name $nodelist)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
OBN??
I will try that, but in the mean time I tried this and this works:
$datastorelist = (Get-Cluster $cluster | Get-VMHost) | Get-Datastore -Name $arrayname
I provided a link to the About OBN article in my previous answer.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Tried your suggestion
$datastorelist = Get-Datastore -Name $arrayname -VMHost (Get-VMHost -Name $nodelist)
and same error. At least I have a work around for now with
$datastorelist = (Get-Cluster $cluster | Get-VMHost) | Get-Datastore -Name $arrayname
I just tried the same thing under PowerCLI 5.1 R2, and I don't seem to have a problem.
Which PowerCLI version are you using ? Do a
Get-PowerCLIVersion
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Same version 5.1 R2
And in $nodelist you have the result from
Get-Cluster $cluster | Get-VMHost
I trust ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, but I was populating it line by line, it seems if I directly fill $nodelist with Get-Cluster $cluster | Get-VMHost it works, so somehow me constructing the comma delimited string casues it to not work. I will use the direct fill as you demonstrated instead. Thanks!