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.
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.
$VM=Get-View -ViewType VirtualMachine -Filter @{'Name'="SERVER"} | where $_.Name -eq "Server'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
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 .powerstat e
Start-Sleep -Seconds 5
}
while ($VM_powerstate -eq "poweredOn")
JM
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
Start-Sleep -Seconds 5
}
while ($VM_powerstate -eq "poweredOn")
JM
You should use one of this lines:
$pattern_srv='^' + $servervm + '$'
$pattern_srv="^$servervm` $"
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")
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
}
ASKER
Thanks for your advices, Qlemo . I'll try that.
JM
JM