Link to home
Start Free TrialLog in
Avatar of JM D.
JM D.

asked on

Syntax -filter behin Get-View command

I would like help with VMWare Powercli syntax.
I must use Get-view command, in a script.

$VM=Get-View -ViewType VirtualMachine -Filter @{'Name'="SERVER"}
$VM_name=$VM.name
$VM_name

Here is the result:
SERVER
SERVERTEST

But How which syntax can I use behind "-Filter" to have  the server whose name is exactly "SERVER" ?

I thank you.
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

$VM=Get-View -ViewType VirtualMachine -Filter @{'Name'="SERVER"} | where $_.Name -eq "Server'
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JM D.
JM D.

ASKER

Thank you both.

Qlemo, it works perfectly with your syntax.

Sorry David, the syntax you proposed to me does not work.
I just must use a ligthly different syntax:
$VM=Get-View -ViewType VirtualMachine -Filter @{'Name'="SERVER"} | where Name -eq "SERVER"

Otherwise, that gives me the error:
Where-Object : Cannot validate argument on parameter 'Property'. The argument is null or empty. Provide an argument
that is not null or empty, and then try the command again.
At line:1 char:81
...

Best regards.
Avatar of JM D.

ASKER

Well, the command works fine.

Here is a part of a script. This could be useful for someone with a question similar to mine.
(the aim is to get the powerstate of a VM without using the get-vm command)

$servervm='SERVER'
$pattern_srv="^$servervm"+'$'


Do {
$VM=Get-View -ViewType VirtualMachine -Filter @{'Name'=$pattern_srv}
$VM_powerstate=$VM.runtime.powerstate
Start-Sleep -Seconds 5
}
while ($VM_powerstate -eq "poweredOn")



JM
You should use one of this lines:
$pattern_srv='^' + $servervm + '$'
$pattern_srv="^$servervm` $"

Open in new window

to not mix different techniques ;-). Even better, without need of another variable:
$servervm='SERVER'

Do {
  $VM=Get-View -ViewType VirtualMachine -Filter @{'Name'= "^$servervm`$}
  Start-Sleep -Seconds 5
}
while ($VM.runtime.powerstate -eq "poweredOn")

Open in new window

This script will still wait 5 seconds even if the VM is powered down already, so I would prefer
$servervm='SERVER'

while ($true)
{
  $VM=Get-View -ViewType VirtualMachine -Filter @{'Name'= "^$servervm`$}
  if ($VM.runtime.powerstate -ne "poweredOn") { break }
  Start-Sleep -Seconds 5
}

Open in new window

Avatar of JM D.

ASKER

Thanks for your advices, Qlemo . I'll try that.

JM