I've searched around for this but am not really finding anything this specific. I'm running a nightly export similar to this:
$TestWinVMList = @()
# Get all VMs | Keep only PoweredOn | Iterate over all of the results.
Get-View –viewtype VirtualMachine | Foreach-object {
# Skip if powered off
if ($_.Summary.Runtime.Powerstate -eq "poweredOff") {
return
}
If ($_.Config.GuestFullName.startswith("Microsoft")) {
$obj = "" | Select u_name, u_hostname, u_disk, u_ram, u_ip_address, u_mac_address, u_os
$obj.u_name = $_.Name
$obj.u_hostname = $_.Guest.HostName
If (!$obj.u_hostname) {$obj.u_hostname = "$($obj.u_name)" + ".phci.org"}
$diskField = $null
$_.Guest.disk | Sort DiskPath | foreach-object {
$diskField += "$($_.DiskPath) $([math]::Round($_.Capacity / 1073741824)) GB "
}
$obj.u_disk = $diskField
$obj.u_ram = [string]([int] $_.Summary.Config.MemorySizeMB / 1024) + " GB"
$obj.u_ip_address = $_.Guest.Net.IPAddress -join ", "
$obj.u_mac_address = $_.guest.net.MacAddress -join ", "
$obj.u_os = $_.Config.GuestFullName
$TestWinVMList += $obj
}
Let's say there's 3 clusters in a vCenter and I don't want to pull data from one of them. How do I tell Get-View to exclude all VMs in a specific cluster?
Ok, try like this
Get-View -ViewType ClusterComputeResource -Filter @{'Name'="^((?!$($nameToExclude)).)*$"} | Select Name
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You can use the RegEx 'or' to combine the 2 clusternames, and then follow the pointers to get the VMs.
Something like this
$clusters = Get-View -ViewType ClusterComputeResource -Property Host -Filter @{'Name'="$($clusterNames -join '|')"}
$esx = Get-View -Id $clusters.Host -Property Name,VM
Get-View -Id $esx.VM
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hmm, it's still pulling in all 3 clusters. Is there a way to exclude 1, rather than including the 2? I'm doing this in a test environment but eventually I'll be rolling it out to a prod environment with a dozen or so clusters.
This is one of those things that's really easy to do with get-cluster | get-vm, but not so easy with get-view it seems.
Seems to be working for me.
In any case, to exclude a specific cluster, try like this.
This excludes 'cluster1', but will retrieve cluster2, cluster3....
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I'm not sure what the issue is, but still not working. I tried just these two lines:
$clusterNames = 'Cluster Name 1','Cluster Name 2'
$clusters = Get-View -ViewType ClusterComputeResource -Property Host -Filter @{'Name'="$($clusterNames -join '|')"}
But when I show the output of $clusters, it's showing all 3 clusters. I do have multiple vCenter connections enabled and am pulling from 2 vCenters, and the names have spaces in them, so I'm not sure if this is screwing things up. I didn't get anywhere with trying to exclude either. I'm not very good with regex. I do appreciate the replies though!
Strange, the following is wortking for me.
It excludes the cluster specified in the variable $nameToExclude
Get-View -ViewType ClusterComputeResource -Filter @{'Name'="[^$($nameToExclude)]"} |
Select Name
As a verification, can you check what is returned with
Select Name
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I'm getting an error on the Get-View line. Could it be the spaces in the cluster name? I'm connecting to 2 vCenters, one with 2 clusters, the other with a single cluster. I started a new session without setting the powercli config to multiple vCenters and am still getting the error. Running PowerCLI 10.1.0.
Get-View : 5/15/2018 3:50:04 PM Get-View parsing "[^Cluster Name 1]" - [x-y] range in reverse order.
At line:1 char:1
+ Get-View -ViewType ClusterComputeResource -Filter @{'Name'="[^$($name ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-View], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
That is a RegEx message, and that indicates that you have the dash ('-') character in the name.
Normally a dash inside a [] construct indicates a range, if you want to have a literal dash, you have to escape it.
Like this
Get-View -ViewType ClusterComputeResource -Filter @{'Name'="[^$($nameToExclude)]"} |
Select Name
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I'm still doing something wrong here. I was trying to leave out the cluster name, but now I'm convinced the cluster name is the cause. Here's what's going on:
PS C:\> Get-View -ViewType ClusterComputeResource | Select Name
Name
----
TWB TST E5-2680 App Delivery
TWB TST E5-2680
PS C:\> Get-Cluster | select Name
Name
----
TWB TST E5-2680
TWB TST E5-2680 App Delivery
PS C:\>
PS C:\> $nameToExclude = 'TWB TST E5\-2680 App Delivery'
PS C:\>
PS C:\>
PS C:\> Get-View -ViewType ClusterComputeResource -Filter @{'Name'="[^$($nameToExclude)]"} | Select Name
PS C:\>
I'm not getting any errors, but I'm also not getting any results. This is just one vCenter, without multiple vCenter connections allowed. I know it would be easier to just include the single cluster, but I do want to move this to another vCenter that has a dozen clusters in it, so I'm really looking to get the exclusion to work. Any ideas?
The back-slash is a special character for RegEx, so you have to escape it.
Try like this
$nameToExclude = [System.Text.RegularExpressions.Regex]::Escape($nameToExclude)
Get-View -ViewType ClusterComputeResource -Filter @{'Name'="[^$($nameToExclude)]"} | Select Name
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I only put the backslash in to try and escape the dash character in the name. Now I get:
$nameToExclude = 'TWB TST E5\-2680 App Delivery'
$nameToExclude = [System.Text.RegularExpressions.Regex]::Escape($nameToExclude)
Get-View -ViewType ClusterComputeResource -Filter @{'Name'="[^$($nameToExclude)]"} | Select Name
Get-View : 5/17/2018 11:27:42 AM Get-View parsing "[^TWB\ TST\ E5\\-2680\ App\ Delivery]" - [x-y]
range in reverse
order.
At line:1 char:1
+ Get-View -ViewType ClusterComputeResource -Filter @{'Name'="[^$($name ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-View], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.
GetVIView
Or if I take the backslash out, back to where we were before:
$nameToExclude = 'TWB TST E5-2680 App Delivery'
$nameToExclude = [System.Text.RegularExpressions.Regex]::Escape($nameToExclude)
Get-View -ViewType ClusterComputeResource -Filter @{'Name'="[^$($nameToExclude)]"} | Select Name
Get-View : 5/17/2018 11:28:01 AM Get-View parsing "[^TWB\ TST\ E5-2680\ App\ Delivery]" - [x-y] ra
nge in reverse
order.
At line:1 char:1
+ Get-View -ViewType ClusterComputeResource -Filter @{'Name'="[^$($name ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-View], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.
GetVIView
Ok, that previous entry put me on the wrong foot.
Yes, you have to escape the dash.
I created a cluster with the same name in my test environment, and it works for me.
Could it be that your clustername contains some other, invisible character besides blanks.
Can you test with a dummy test cluster with a name with a dash in it?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
OK, now we're getting somewhere! I think it has to do with the fact that the first parts of the cluster names are the same. So here's the clusters now:
Get-View -ViewType ClusterComputeResource | Select Name
Name
----
Dashes Are-Annoying
TWB TST E5-2680 App Delivery
TWB TST E5-2680
If I run the same command with the new cluster name, I get the same errors. However, look at this:
$nameToExclude = 'TWB TST E5\-2680 App Delivery'
Get-View -ViewType ClusterComputeResource -Filter @{'Name'="[^$($nameToExclude)]"} | Select Name
Name
----
Dashes Are-Annoying
Notice now it only shows the single cluster name. Since both clusters contain the same "TWB TST E5-2680", I think it's having an issue with that. Aside from renaming the "App Delivery" cluster (which isn't out of the question), any ideas? Thank you so much for the help, I really do appreciate it!
Ok, try like this
Get-View -ViewType ClusterComputeResource -Filter @{'Name'="^((?!$($nameToExclude)).)*$"} | Select Name
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That did it! Thank you sir, much appreciated!